fix(scripts): add os.homedir() fallback for Windows compatibility#977
Conversation
On Windows (native cmd/PowerShell), process.env.HOME is undefined. Seven CLI entry points and two library files pass process.env.HOME directly as homeDir without a cross-platform fallback, causing all path resolutions to silently fail (resolving to "undefined/.claude/..."). Node.js os.homedir() correctly handles all platforms (HOME on Unix, USERPROFILE on Windows, OS-level fallback). The project already uses this pattern in scripts/lib/state-store/index.js and has a getHomeDir() utility in scripts/lib/utils.js, but it was not applied consistently. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com> Signed-off-by: Lidang-Jiang <lidangjiang@gmail.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (9)
📝 WalkthroughWalkthroughThe PR adds fallback home directory resolution across nine scripts. When the Changes
Poem
Estimated code review effort🎯 1 (Trivial) | ⏱️ ~8 minutes 🚥 Pre-merge checks | ✅ 2 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
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 SummaryThis PR fixes a Windows compatibility bug where Key changes:
Confidence Score: 5/5Safe to merge — minimal, targeted bug fix with no behavioural change on Unix and a correct cross-platform fix on Windows All changes are additive fallbacks ( No files require special attention Important Files Changed
Flowchart%%{init: {'theme': 'neutral'}}%%
flowchart TD
A[CLI Entry Point\ndoctor / repair / uninstall / etc.] --> B{process.env.HOME\ndefined?}
B -- Yes --> C[Use process.env.HOME]
B -- No\nWindows / unset --> D[os.homedir\nfallback]
C --> E[homeDir resolved]
D --> E
E --> F[Library function\ninstall-lifecycle / install-executor]
F --> G{options.homeDir\ndefined?}
G -- Yes --> H[Use options.homeDir]
G -- No --> I{process.env.HOME\ndefined?}
I -- Yes --> J[Use process.env.HOME]
I -- No --> K[os.homedir\nfallback]
H --> L[path resolved correctly\n~/.claude/ecc/...]
J --> L
K --> L
Reviews (1): Last reviewed commit: "fix(scripts): add os.homedir() fallback ..." | Re-trigger Greptile |
| @@ -1,5 +1,6 @@ | |||
| #!/usr/bin/env node | |||
|
|
|||
| const os = require('os'); | |||
There was a problem hiding this comment.
Consider using existing
getHomeDir() utility
The project already exports a getHomeDir() function from scripts/lib/utils.js (line 27) that returns os.homedir(). Since os.homedir() itself already respects the HOME environment variable on Unix platforms (per Node.js docs: "uses the HOME environment variable if defined, otherwise the passwd database entry"), the process.env.HOME || prefix is effectively redundant.
Rather than adding require('os') to each of the 7 CLI entry points, using the shared getHomeDir() utility would avoid spreading the pattern:
// In doctor.js, install-apply.js, repair.js, etc.
const { getHomeDir } = require('./lib/utils');
// ...
homeDir: process.env.HOME || getHomeDir(),Or more directly, since getHomeDir() already resolves the correct value cross-platform, homeDir: getHomeDir() would suffice (with caller-provided options.homeDir taking precedence in the library functions).
This same pattern also applies to scripts/install-apply.js, scripts/repair.js, scripts/uninstall.js, scripts/list-installed.js, scripts/sessions-cli.js, and scripts/status.js. Not a blocker — the chosen approach is consistent with state-store/index.js — just noting the existing utility goes unused.
Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!
…dows-fallback fix(scripts): add os.homedir() fallback for Windows compatibility
Summary
On Windows (native cmd/PowerShell),
process.env.HOMEisundefined. Seven CLI entry points and two library files passprocess.env.HOMEdirectly ashomeDirwithout a cross-platform fallback, causing all path resolutions to silently resolve toundefined/.claude/...— which doesn't exist, so commands likedoctor,status,list-installed,repair, anduninstallsilently return empty results instead of operating on the correct home directory.This PR adds
os.homedir()as a fallback to all 9 affected locations. Node.jsos.homedir()correctly handles all platforms (HOMEon Unix,USERPROFILEon Windows, OS-level fallback).Note: The project already uses this correct pattern in
scripts/lib/state-store/index.js:22and has agetHomeDir()utility inscripts/lib/utils.js:27— this fix brings the remaining CLI entry points and library functions into consistency.Affected Files
7 CLI entry points (each: +
require('os'), changeprocess.env.HOME→process.env.HOME || os.homedir()):scripts/doctor.jsscripts/install-apply.jsscripts/repair.jsscripts/uninstall.jsscripts/list-installed.jsscripts/sessions-cli.jsscripts/status.js2 library files (each: +
require('os'), add|| os.homedir()to existing fallback chain):scripts/lib/install-lifecycle.js(3 occurrences)scripts/lib/install-executor.js(1 occurrence)Type
Testing
All 1621 tests pass after the change. Additionally, verified the fix by simulating a Windows-like environment where
HOMEis unset:Before Fix: homeDir resolves to undefined when HOME is unset
After Fix: homeDir correctly resolves via os.homedir()
Before Fix: doctor.js (HOME unset) — runs but operates on wrong path
After Fix: doctor.js (HOME unset) — correctly resolves home directory
Before Fix: status.js (HOME unset)
After Fix: status.js (HOME unset)
Before Fix: list-installed.js (HOME unset)
After Fix: list-installed.js (HOME unset)
Before Fix: sessions-cli.js (HOME unset)
After Fix: sessions-cli.js (HOME unset)
Before Fix: repair.js (HOME unset)
After Fix: repair.js (HOME unset)
Before Fix: uninstall.js --dry-run (HOME unset)
After Fix: uninstall.js --dry-run (HOME unset)
Before Fix: install-apply.js --dry-run (HOME unset)
After Fix: install-apply.js --dry-run (HOME unset)
(This error is expected —
install-apply.jsrequires explicit install arguments. The fix ensureshomeDirresolves correctly when those arguments are provided.)After Fix: Full test suite (1621/1621 pass)
Checklist
fix(scripts):prefix)|| os.homedir()fallback)state-store/index.js,utils.js)Summary by cubic
Fixes Windows CLI behavior by falling back to
os.homedir()whenprocess.env.HOMEis missing, so path resolution uses the real home directory instead ofundefined/.claude/.... Affectsdoctor,status,list-installed,repair,uninstall,sessions-cli, andinstall-apply.process.env.HOME || os.homedir()to computehomeDirin 7 CLI entry points and 2 library functions.scripts/lib/state-storeandscripts/lib/utils#getHomeDir().Written for commit ae21a8d. Summary will update on new commits.
Summary by CodeRabbit