Skip to content

Nx Console sets NX_WORKSPACE_ROOT_PATH globally, breaking git worktree support #3115

Description

@AlonMiz

Bug Report

Description

Nx Console sets process.env.NX_WORKSPACE_ROOT_PATH in the VSCode extension host process during activation. Because the extension host is shared across all VSCode windows, this env var leaks into terminals of other VSCode windows, causing Nx to resolve the wrong workspace root.

This is particularly problematic with git worktrees, where multiple VSCode windows open different worktrees of the same repository. The first window to activate Nx Console "wins" and all other windows get the wrong NX_WORKSPACE_ROOT_PATH.

Steps to Reproduce

  1. Clone a repo with an Nx workspace (e.g., git clone <repo> main-repo)
  2. Create a git worktree: git worktree add ../worktree-1 -b feature-branch
  3. Open the main repo in VSCode window A
  4. Open the worktree in VSCode window B
  5. In window B's terminal, run: echo $NX_WORKSPACE_ROOT_PATH
  6. Expected: Empty or path to the worktree directory
  7. Actual: Path to the main repo (window A's workspace)

Root Cause

In main.js, the FV function sets:

process.env.NX_WORKSPACE_ROOT_PATH = t

And in nxls/main.js, the VO/getNxWorkspaceConfig function also sets:

process.env.NX_WORKSPACE_ROOT_PATH = t

Since process.env is shared across the entire VSCode extension host process, this mutation affects all windows.

Impact

  • Nx resolves config (nx.json) from the wrong workspace
  • Nx daemon connects to the wrong socket
  • Cache reads/writes go to the wrong .nx/cache directory
  • Project graph may be computed from wrong source files
  • nx affected computes against wrong base

Suggested Fix

Instead of setting process.env.NX_WORKSPACE_ROOT_PATH globally, consider:

  1. Per-terminal env injection: Use vscode.workspace API to set terminal environment variables scoped to the specific workspace folder
  2. Avoid mutating process.env: Pass the workspace root through function parameters or a scoped context instead of global state
  3. Use terminal.integrated.env.*: Set the env var via VSCode's terminal environment settings API, which is workspace-scoped

Workaround

Add to .vscode/settings.json:

{
  "terminal.integrated.env.osx": {
    "NX_WORKSPACE_ROOT_PATH": "${workspaceFolder}"
  },
  "terminal.integrated.env.linux": {
    "NX_WORKSPACE_ROOT_PATH": "${workspaceFolder}"
  },
  "terminal.integrated.env.windows": {
    "NX_WORKSPACE_ROOT_PATH": "${workspaceFolder}"
  }
}

Environment

  • Nx Console version: 18.92.0
  • Nx version: 22.4.0
  • VSCode version: latest
  • OS: macOS Sequoia
  • Node manager: Volta

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions