Skip to content

Fix: preserve user working directory when spawning Claude instances#12

Closed
jrevillard wants to merge 1 commit intoruvnet:mainfrom
jrevillard:fix/claude-working-directory
Closed

Fix: preserve user working directory when spawning Claude instances#12
jrevillard wants to merge 1 commit intoruvnet:mainfrom
jrevillard:fix/claude-working-directory

Conversation

@jrevillard
Copy link
Copy Markdown

Problem

When running claude-flow claude spawn from a specific directory, Claude was incorrectly started with the claude-flow installation directory (/usr/local/share/npm-global/lib/node_modules/claude-flow) as its working directory instead of the user's current working directory.

Root Cause

The issue occurred because:

  1. bin/claude-flow sets cwd: path.join(__dirname, '..') when spawning the Deno process
  2. src/cli/simple-cli.js did not specify a working directory when spawning Claude, inheriting the claude-flow installation directory

Solution

Implemented a comprehensive fix that preserves the user's original working directory:

Changes Made

  1. bin/claude-flow - Added CLAUDE_FLOW_ORIGINAL_CWD environment variable to capture user's original working directory

  2. src/cli/simple-cli.js - Multiple improvements:

    • Set cwd parameter to use original working directory when spawning Claude process
    • Added CLAUDE_FLOW_CWD environment variable passed to Claude
    • Added working directory to configuration display in enhanced task
    • Added explicit instructions to Claude about correct working directory

How It Works

  1. Capture: bin/claude-flow captures process.cwd() in CLAUDE_FLOW_ORIGINAL_CWD
  2. Process CWD: Claude subprocess spawns with original directory as working directory
  3. Environment: Claude receives CLAUDE_FLOW_CWD environment variable
  4. Instructions: Claude gets explicit guidance about correct working directory
  5. Configuration: Working directory is displayed in task configuration

Testing

  • Tested with dry-run mode to verify correct working directory is used
  • Verified environment variables are properly set and passed

Result

After this fix, when users run claude-flow claude spawn from their project directory, Claude will:

  • Start with the correct working directory as its process CWD
  • Receive explicit instructions about the working directory
  • Show the correct directory in its welcome message
  • Perform all file operations relative to the user's project directory

Fixes issue where Claude was working in claude-flow installation directory instead of user's project directory.

When running `claude-flow claude spawn` from a specific directory, Claude was
incorrectly started with the claude-flow installation directory instead of
the user's current working directory.

This fix implements a comprehensive solution:

- Capture original working directory in bin/claude-flow wrapper
- Pass original directory via CLAUDE_FLOW_ORIGINAL_CWD environment variable
- Set correct cwd when spawning Claude subprocess in simple-cli.js
- Provide explicit working directory instructions to Claude
- Display working directory in task configuration

Now Claude will start in the user's project directory and perform all
file operations relative to that directory, not the claude-flow installation.

Resolves issue where Claude showed installation directory instead of project
directory in its working context.
@ruvnet
Copy link
Copy Markdown
Owner

ruvnet commented Jun 12, 2025

take a look at my update. i think i resolved this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants