Skip to content

fix(activate): unset inherited hook session vars in IDE env resolution#660

Open
Copilot wants to merge 2 commits intomainfrom
copilot/fix-vscode-node-activation-issue
Open

fix(activate): unset inherited hook session vars in IDE env resolution#660
Copilot wants to merge 2 commits intomainfrom
copilot/fix-vscode-node-activation-issue

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 22, 2026

In VSCode integrated terminals, project-scope SDK versions still aren't activated on 1.0.10 — vfox current reports the project version but node -v resolves to the global one.

Root cause

VSCode resolves the shell environment by spawning a login shell with VSCODE_RESOLVING_ENVIRONMENT=1, inheriting its own process.env into that shell. When VSCode is launched from a terminal where vfox was already activated, __VFOX_PID / __VFOX_SHELL / __VFOX_CURTMPPATH (and on PowerShell, __VFOX_INITIALIZED) ride along.

renderActivateScript's IDE-resolution branch only omitted these vars from its output — it never unset them. So:

  1. The resolution shell exits with the inherited vars still set.
  2. VSCode captures them and propagates them to every integrated terminal.
  3. In the integrated terminal, vfox activate sees __VFOX_PID, treats it as a live hook session, and env.GetPid() returns the parent shell's PID.
  4. SessionSdkDir and env-state.json are therefore the parent shell's — the precmd hook returns the parent's cached PATH (typically global-only) instead of recomputing for the project.
  5. On PowerShell specifically, a leaked __VFOX_INITIALIZED additionally causes integrated terminals to skip prompt-hook registration entirely.

This explains the asymmetry: vfox current reads .vfox.toml directly, while node resolves through the leaked cached PATH.

Changes

  • internal/env/flag.go: add InitializedFlag constant for __VFOX_INITIALIZED so the hook session keys live in one place.
  • cmd/commands/activate.go: in the IsIDEEnvironmentResolution() branch of renderActivateScript, explicitly emit nil (→ unset) for env.HookFlag, env.PidFlag, env.InitializedFlag, and pathmeta.HookCurTmpPath instead of just dropping them from the map. The resolution shell exits with a sanitized environment, so each integrated terminal starts a fresh hook session bound to its own PID and session dir, and PowerShell hook registration runs as expected.
  • cmd/commands/activate_test.go: tighten TestRenderActivateScriptForIDEEnvironmentResolutionSkipsHookSessionState to assert unset KEY; is present (and the keys are never re-exported) for all four hook session vars rather than merely absent.
// IDE resolution mode now emits, in addition to PATH and tool vars:
unset __VFOX_SHELL;
unset __VFOX_PID;
unset __VFOX_INITIALIZED;
unset __VFOX_CURTMPPATH;

@bytemain bytemain marked this pull request as ready for review April 22, 2026 17:58
Copilot AI review requested due to automatic review settings April 22, 2026 17:58
@codecov
Copy link
Copy Markdown

codecov Bot commented Apr 22, 2026

Codecov Report

❌ Patch coverage is 80.00000% with 1 line in your changes missing coverage. Please review.
✅ Project coverage is 24.13%. Comparing base (9522202) to head (16f24b0).
⚠️ Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
cmd/commands/activate.go 80.00% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #660      +/-   ##
==========================================
- Coverage   25.03%   24.13%   -0.91%     
==========================================
  Files          82       82              
  Lines        5667     7088    +1421     
==========================================
+ Hits         1419     1711     +292     
- Misses       4072     5201    +1129     
  Partials      176      176              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes a VS Code/IDE integrated-terminal activation bug caused by hook session variables leaking into IDE environment resolution shells, leading to reuse of a parent shell’s cached PATH/session state instead of recomputing per-project activation.

Changes:

  • In IDE environment resolution mode, explicitly emit unsets for __VFOX_SHELL, __VFOX_PID, and __VFOX_CURTMPPATH (via nil values in env.Vars) instead of merely omitting them.
  • Update the IDE-resolution activation test to assert unset KEY; is present and that the keys are not re-exported.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.

File Description
cmd/commands/activate.go Ensures IDE env-resolution output explicitly unsets inherited hook session variables before returning export-only script.
cmd/commands/activate_test.go Tightens assertions to require explicit unset statements and prevent re-export of hook session vars.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread cmd/commands/activate.go
Copilot AI requested a review from bytemain April 23, 2026 03:29
@bytemain
Copy link
Copy Markdown
Member

@aooohan 我验证了这个修复没有问题

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.

3 participants