Files
ruflo/docs/troubleshooting/npx-cache-conflicts.md
T
lanemcandClaude 69e93300bc fix: prevent concurrent NPX cache conflicts with isolated cache directories (#215)
* fix: prevent concurrent NPX cache conflicts with lock manager

- Add NPX cache manager utility to serialize NPX operations
- Wrap all NPX commands in withLock() to ensure exclusive access
- Implement automatic retry logic with exponential backoff
- Add lock timeout protection (30s) to prevent deadlocks
- Create manual lock management CLI for debugging
- Add comprehensive troubleshooting documentation

This fix addresses the ENOTEMPTY error that occurs when multiple
claude-flow instances attempt to use NPX simultaneously. The lock
manager ensures only one NPX operation runs at a time, preventing
cache directory rename conflicts.

Fixes the issue where concurrent swarm operations fail with:
npm error code ENOTEMPTY
npm error syscall rename
npm error ENOTEMPTY: directory not empty

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: replace complex lock manager with simple per-process cache isolation

- Remove complex NPX lock manager in favor of simpler approach
- Each process now gets isolated NPX cache directory
- Maintains true parallel execution without serialization overhead
- Add comprehensive test suites to verify fix works
- Update documentation with improved solution

Benefits:
- No performance impact (no serialization)
- Simpler implementation (50 lines vs 200+ lines)
- Self-cleaning cache directories
- Cross-platform compatibility
- Zero lock contention

Test Results:
- All 6 unit tests pass
- 18/18 integration tests pass with 0 ENOTEMPTY errors
- Concurrent and stress tests successful

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* test: add Jest-compatible tests for NPX cache isolation

- Add comprehensive unit tests for npx-isolated-cache utility
- Add integration tests for init command NPX isolation
- Fix duplicate function declaration in batch-init.js
- All tests pass: 15/15 Jest tests + 6/6 integration tests
- Verify 0 ENOTEMPTY errors in concurrent scenarios

Test Coverage:
- Cache isolation functionality
- Environment variable handling
- Cleanup mechanisms
- Integration with existing code
- Concurrent NPX execution safety

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* docs: add comprehensive troubleshooting documentation for NPX cache fix

- Add troubleshooting/README.md with quick diagnosis guide
- Update main troubleshooting.md with NPX cache fix section
- Document test verification steps and version notes
- Link to detailed NPX cache conflicts guide
- Mark issue as FIXED in v2.0.0-alpha.17+

Provides clear guidance for:
- Identifying ENOTEMPTY errors
- Understanding the fix implemented
- Testing concurrent safety
- Version-specific notes

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* refactor: remove unused imports from batch-init.js

Remove unused imports that were leftover from cleanup:
- getIsolatedNpxEnv (not needed - batch-init doesn't run NPX commands)
- path (removed when duplicate initializeProject function was cleaned up)

The batch-init functionality only creates directories and files with Deno APIs,
so NPX cache isolation is not required there. NPX isolation is only used in
the main init command where create-sparc NPX command is executed.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

* fix: maintain Deno environment compatibility in NPX cache isolation

Ensure npx-isolated-cache works in both Deno and Node.js environments:
- Use Deno.env.toObject() when available (Deno environment)
- Fall back to process.env in Node.js environment
- Maintains compatibility with existing Deno-based init command

This preserves the original behavior where Deno.env.toObject() was used
in the init command's environment setup, just with added NPX cache isolation.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>

---------

Co-authored-by: Claude <noreply@anthropic.com>
2025-07-12 23:29:20 -04:00

2.9 KiB

NPX Cache Conflicts - Troubleshooting Guide

Problem

When running multiple claude-flow instances concurrently, you may encounter the following error:

npm error code ENOTEMPTY
npm error syscall rename
npm error path /private/tmp/.npm/_npx/[hash]/node_modules/[package]
npm error dest /private/tmp/.npm/_npx/[hash]/node_modules/.[package]-[random]
npm error errno -66
npm error ENOTEMPTY: directory not empty, rename '[source]' -> '[dest]'

This error occurs when multiple NPX processes attempt to access and modify the same cache directory simultaneously.

Root Cause

NPX uses a shared cache directory (/tmp/.npm/_npx/ or similar) to store downloaded packages. When multiple claude-flow processes run concurrently (e.g., in swarm mode or parallel batch operations), they compete for the same cache resources, causing directory rename conflicts.

Solution

Claude-flow v2.0.0-alpha.17+ includes automatic NPX cache isolation that prevents these conflicts by giving each process its own isolated cache directory.

How It Works

The solution uses per-process cache isolation:

  1. Unique Cache Directories: Each process gets its own NPX cache at /tmp/.npm-cache/claude-flow-[pid]-[timestamp]-[random]
  2. Automatic Cleanup: Cache directories are automatically removed when the process exits
  3. No Performance Impact: True parallel execution is maintained - no serialization needed
  4. Cross-platform: Works on Linux, macOS, and Windows

Implementation

The fix is simple and efficient:

  • Each NPX command runs with NPM_CONFIG_CACHE set to a unique directory
  • No locks or synchronization needed
  • Cache directories are cleaned up automatically on process exit

Verification

To verify the fix is working, run the included test suites:

# Comprehensive test suite
node test/npx-cache-fix-test.js

# Integration test with real NPX commands
node test/npx-isolation-integration-test.js

Both tests should pass with no ENOTEMPTY errors.

Alternative Workarounds

If you're using an older version without this fix, you can manually implement the same approach:

  1. Use isolated cache directories:

    NPM_CONFIG_CACHE=/tmp/claude-flow-cache-$$ npx --y claude-flow@alpha init --force
    
  2. Install globally to avoid NPX:

    npm install -g claude-flow@alpha
    claude-flow init --force
    
  3. Run operations sequentially:

    # In batch operations, disable parallelism
    claude-flow batch init --parallel=false
    

Implementation Details

The fix is implemented in:

  • /src/utils/npx-isolated-cache.js - Core isolation utility
  • /src/cli/simple-commands/init/index.js - Uses getIsolatedNpxEnv() for NPX commands
  • /src/cli/simple-commands/init/batch-init.js - Batch operations automatically get isolation

All NPX executions now use isolated cache directories, completely eliminating cache conflicts without sacrificing performance.