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
- Clone a repo with an Nx workspace (e.g.,
git clone <repo> main-repo)
- Create a git worktree:
git worktree add ../worktree-1 -b feature-branch
- Open the main repo in VSCode window A
- Open the worktree in VSCode window B
- In window B's terminal, run:
echo $NX_WORKSPACE_ROOT_PATH
- Expected: Empty or path to the worktree directory
- 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:
- Per-terminal env injection: Use
vscode.workspace API to set terminal environment variables scoped to the specific workspace folder
- Avoid mutating
process.env: Pass the workspace root through function parameters or a scoped context instead of global state
- 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
Bug Report
Description
Nx Console sets
process.env.NX_WORKSPACE_ROOT_PATHin 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
git clone <repo> main-repo)git worktree add ../worktree-1 -b feature-branchecho $NX_WORKSPACE_ROOT_PATHRoot Cause
In
main.js, theFVfunction sets:And in
nxls/main.js, theVO/getNxWorkspaceConfigfunction also sets:Since
process.envis shared across the entire VSCode extension host process, this mutation affects all windows.Impact
nx.json) from the wrong workspace.nx/cachedirectorynx affectedcomputes against wrong baseSuggested Fix
Instead of setting
process.env.NX_WORKSPACE_ROOT_PATHglobally, consider:vscode.workspaceAPI to set terminal environment variables scoped to the specific workspace folderprocess.env: Pass the workspace root through function parameters or a scoped context instead of global stateterminal.integrated.env.*: Set the env var via VSCode's terminal environment settings API, which is workspace-scopedWorkaround
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