Repository navigation
fix(terminal): expand environment variables in Windows PATH - #11987
Conversation
|
Caution Review failedThe pull request is closed. ℹ️ Recent review info⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: 📥 CommitsReviewing files that changed from the base of the PR and between 12d74259c7febc24738022432005183cb64dc48e and dfb84e9. 📒 Files selected for processing (7)
📝 WalkthroughWalkthroughThe change adds shared Windows environment-variable expansion for 🚥 Pre-merge checks | ✅ 3 | ❌ 2❌ Failed checks (2 warnings)
✅ Passed checks (3 passed)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
Greptile SummaryAdds centralized Windows environment variable expansion for PTY PATH values, fixing terminal command lookup when PATH entries contain
Confidence Score: 5/5Safe to merge — the change is Windows-only, well-tested at unit and integration level, and macOS/Linux PTY paths are entirely unaffected. The expansion logic is correct: it snapshots the env before mutating, expands only Path/PATH keys, preserves unknown %VAR% tokens verbatim, and is guarded by a platform === 'win32' check at every call site. All three PTY spawn boundaries (local, daemon, relay) are covered by dedicated tests. The only finding is a minor test hygiene issue in the E2E spec where temp files are created at module load time rather than inside beforeAll. Files Needing Attention: tests/e2e/terminal-windows-path-expansion.spec.ts — module-level temp dir setup could leak files in non-run scenarios.
|
| Filename | Overview |
|---|---|
| src/shared/windows-environment-expansion.ts | New shared module; correct case-insensitive lookup, snapshot prevents in-place mutation interference, non-recursive expansion matches Windows behavior, graceful unknown-variable pass-through. |
| src/main/pty/windows-environment-path.ts | Removed local duplicate; added expandWindowsPathEnvironmentVariables call at top of mergeWindowsPathSegments so segment deduplication compares fully-resolved paths. |
| src/main/cli/cli-installer.ts | Removed local expandWindowsEnvironmentVariables duplicate; now imports from shared module — pure refactor, behavior identical on all call sites. |
| src/main/daemon/pty-subprocess.ts | Single expandWindowsPathEnvironmentVariables(env) call added before PTY spawn; correctly placed after all env scrubbing/merging steps. |
| src/main/providers/local-pty-provider.ts | Expansion added after buildSpawnEnv; idempotent when buildPtyHostEnv has already expanded via mergeWindowsPathSegments. |
| src/relay/pty-handler.ts | Expansion added at end of env-assembly in buildSpawnEnv, covering the SSH relay PTY path. |
| src/shared/browser-window-close-policy.ts | Sentinel URL now conditionally includes C: drive letter on Windows to avoid fileURLToPath crash; platform check uses typeof process guard for renderer safety. |
| tests/e2e/terminal-windows-path-expansion.spec.ts | End-to-end test for Windows path expansion; temp dir and .cmd probe file are created at module load time rather than inside beforeAll, which can leave orphaned files if the module is loaded without running tests. |
| src/relay/pty-handler.test.ts | Added platform save/restore in beforeEach/afterEach, mocked forceKillPosixPtyProcessGroups and processHasChildren to fix test isolation, plus new Windows expansion assertion. |
| src/shared/windows-environment-expansion.test.ts | New unit tests cover case-insensitive lookup, empty-value expansion, multi-casing PATH keys, and non-Windows no-op — good coverage of the shared module. |
Flowchart
%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[PTY spawn request with env containing %VAR% in PATH] --> B{Spawn path}
B --> C[local-pty-provider.ts]
B --> D[pty-subprocess.ts]
B --> E[relay/pty-handler.ts]
C --> F[buildPtyHostEnv]
F --> G[mergePersistedWindowsPath]
G --> H[mergeWindowsPathSegments]
H --> I[expandWindowsPathEnvironmentVariables]
C --> M[expandWindowsPathEnvironmentVariables idempotent second pass]
D --> I
E --> I
I --> N{platform === win32?}
N -->|No| O[no-op: macOS/Linux unchanged]
N -->|Yes| P[snapshot env, expand Path/PATH keys only]
P --> Q[%VAR% resolved or left verbatim if unknown]
Reviews (2): Last reviewed commit: "fix(browser): make close-policy sentinel..." | Re-trigger Greptile
| ): string { | ||
| return value.replace(/%([^%]+)%/g, (match, name: string) => { | ||
| return getEnvironmentVariable(env, name) ?? match | ||
| }) | ||
| } |
There was a problem hiding this comment.
Silent behavior change: empty environment variables are now expanded
The two previous expandWindowsEnvironmentVariables implementations both used a truthiness guard — env[envKey] ? env[envKey] : match — which meant a variable set to an empty string was NOT expanded (the %VAR% token was left verbatim). The new implementation uses ?? match, where typeof '' === 'string' is true, so an empty-string variable now collapses its token to "". For PATH segments this is harmless, but callers that relied on the old behavior — e.g. cli-installer.ts used the function in a normalizeWindowsPath helper — will silently get different output. Worth confirming this change is intentional everywhere the function was previously called.
There was a problem hiding this comment.
That context is helpful — glad the fileURLToPath crash has a fix, and moving platform-specific normalization into the main process would be a cleaner boundary.
To close the loop on my original comment: the empty-string expansion behavior change (?? match vs. the old truthiness guard) is a separate, narrower concern. If you're confident none of the prior call sites relied on empty variables being left unexpanded, then the current implementation is fine. The ?? match fallback is actually the more correct behavior semantically — it just differs from what callers may have depended on historically.
Tip: You can customize Greptile's behavior for this repo with .greptile/rules.md and .greptile/config.json.
There was a problem hiding this comment.
The C: sentinel change fixes a separate Windows fileURLToPath startup crash discovered while validating this PR. We will consider moving platform-specific normalization into the main process so the shared marker remains platform-neutral.
There was a problem hiding this comment.
Silent behavior change: empty environment variables are now expanded
The two previous
expandWindowsEnvironmentVariablesimplementations both used a truthiness guard —env[envKey] ? env[envKey] : match— which meant a variable set to an empty string was NOT expanded (the%VAR%token was left verbatim). The new implementation uses?? match, wheretypeof '' === 'string'istrue, so an empty-string variable now collapses its token to"". For PATH segments this is harmless, but callers that relied on the old behavior — e.g.cli-installer.tsused the function in anormalizeWindowsPathhelper — will silently get different output. Worth confirming this change is intentional everywhere the function was previously called.
Intentional. Defined empty variables expand to an empty string, while unknown variables remain unchanged. This behavior is covered by the added regression test.
12d7425 to
dfb84e9
Compare
…#11987) * fix(terminal): expand variables in Windows PATH * fix(terminal): preserve expanded Windows PATH at spawn --------- Co-authored-by: OrcaWin <[email protected]>
Summary
Fixes Windows terminal command lookup when
PATHcontains variables such as%LOCALAPPDATA%,%USERPROFILE%, or%NVM_HOME%.Expansion now applies at the final environment assembly boundary for local, daemon, and Windows SSH relay PTYs. Agent Teams PATH promotion also normalizes its requested shim path so it cannot reintroduce an unexpanded segment after expansion.
Closes #11788.
Screenshots
No visual change.
Testing
terminal-windows-path-expansion.spec.ts(1 passed)pnpm lintpassed code-quality, type-aware, reliability, max-lines, and bundled-guide checks, then stopped on pre-existing stale generated skill manifests unrelated to this diff.Adversarial Review
Reviewed all PATH mutation points after environment assembly. The original implementation expanded too early in local and daemon PTYs, allowing Agent Teams shim promotion to prepend the raw
%VAR%segment again. The takeover moves expansion to the last PATH mutation boundary and expands the promoted requested segment before deduplication.The relay fresh-spawn and revive paths have no later Windows PATH mutation. Non-Windows paths remain unchanged. Windows SSH relays expand on the remote Windows host; Linux/macOS relays do not apply Windows syntax.
Security Audit
The implementation only substitutes
%NAME%references from the existing environment before PTY spawning. Unknown variables remain unchanged. It adds no command execution, IPC, authentication, secret handling, or dependencies.