Skip to content

fix(windows): restore shell environment variables - #588

Open
tianrking wants to merge 2 commits into
wonderwhy-er:mainfrom
tianrking:agent/repair-windows-shell-environment
Open

fix(windows): restore shell environment variables#588
tianrking wants to merge 2 commits into
wonderwhy-er:mainfrom
tianrking:agent/repair-windows-shell-environment

Conversation

@tianrking

@tianrking tianrking commented Jul 16, 2026

Copy link
Copy Markdown

Summary

  • repair WINDIR and SystemRoot for Windows child shells when the host process supplies an empty or missing value
  • keep the existing PATHEXT repair on the same child-environment path
  • add a focused regression test for the MSIX-like environment

Fixes #480

Root cause

TerminalManager.executeCommand() copied the inherited process environment directly into every child shell. Under the MSIX-packaged Claude Desktop, WINDIR can be empty, which prevents PowerShell from loading CIM/CDXML modules even after PATHEXT has been repaired.

Validation

  • npm run build
  • node test/test-windows-shell-environment.js
  • git diff --check

Scope

This does not alter command policy or process execution. On Windows it preserves a non-empty configured WINDIR, otherwise falls back to SystemRoot and only then C:\Windows; non-Windows environments are unchanged.

Summary by CodeRabbit

  • Bug Fixes
    • Improved Windows shell startup reliability by repairing critical environment variables before running commands.
    • Automatically normalizes and completes command extension settings, and sets canonical WINDIR/SystemRoot with sensible defaults.
    • Avoids mutating the original environment and preserves unrelated environment values.
    • Keeps PATHEXT behavior unchanged on non-Windows systems.
  • Tests
    • Added automated coverage for Windows and non-Windows environment repair and defaulting behavior.

@coderabbitai

coderabbitai Bot commented Jul 16, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: 0268ce3d-4cf6-4e46-9237-8a9502e728f6

📥 Commits

Reviewing files that changed from the base of the PR and between 3157214 and ad678e5.

📒 Files selected for processing (2)
  • src/terminal-manager.ts
  • test/test-windows-shell-environment.js
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/test-windows-shell-environment.js

📝 Walkthrough

Walkthrough

Windows shell environment repair is centralized in an exported helper, applied during shell spawning, and covered by tests for repaired, default, and non-Windows environments.

Changes

Windows shell environment repair

Layer / File(s) Summary
Environment repair helper
src/terminal-manager.ts
getRepairedPathExt accepts an environment value, while getRepairedWindowsShellEnvironment repairs PATHEXT, WINDIR, and SystemRoot on Windows.
Spawn integration and validation
src/terminal-manager.ts, test/test-windows-shell-environment.js
executeCommand applies the helper when building shell environments, preserves the exported terminalManager, and tests Windows repair, defaults, passthrough behavior, and input preservation.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Sequence Diagram(s)

sequenceDiagram
  participant executeCommand
  participant getRepairedWindowsShellEnvironment
  participant SpawnedShell
  executeCommand->>getRepairedWindowsShellEnvironment: build repaired shell environment
  getRepairedWindowsShellEnvironment->>getRepairedWindowsShellEnvironment: repair PATHEXT and Windows variables
  getRepairedWindowsShellEnvironment-->>executeCommand: return environment copy
  executeCommand->>SpawnedShell: spawn with repaired environment
Loading

Possibly related PRs

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly and concisely describes the main Windows shell environment repair.
Linked Issues check ✅ Passed The changes restore PATHEXT and WINDIR for spawned Windows shells and add regression coverage, matching issue #480.
Out of Scope Changes check ✅ Passed The diff stays focused on Windows shell environment repair and related tests, with no clear unrelated changes.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@tianrking
tianrking marked this pull request as ready for review July 16, 2026 04:24

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@src/terminal-manager.ts`:
- Around line 49-60: Update getRepairedWindowsShellEnvironment() to resolve
WINDIR, SystemRoot, and PATHEXT from environment using case-insensitive key
matching before repairing them. Use those inherited values when present, then
assign the canonical keys and remove or normalize duplicate case variants so the
returned environment contains no conflicting duplicates.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: defaults

Review profile: CHILL

Plan: Pro

Run ID: a503f0ff-18d2-42bb-bd98-f461185f6c72

📥 Commits

Reviewing files that changed from the base of the PR and between 3147456 and 3157214.

📒 Files selected for processing (2)
  • src/terminal-manager.ts
  • test/test-windows-shell-environment.js

Comment thread src/terminal-manager.ts
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.

Spawned shells inherit stripped PATHEXT (.CPL) and empty WINDIR under MSIX-packaged Claude Desktop on Windows

1 participant