Skip to content

fix: Cross-platform hook compatibility for Windows#706

Open
chrisglick wants to merge 1 commit intodanielmiessler:mainfrom
chrisglick:windows-hook-compat
Open

fix: Cross-platform hook compatibility for Windows#706
chrisglick wants to merge 1 commit intodanielmiessler:mainfrom
chrisglick:windows-hook-compat

Conversation

@chrisglick
Copy link
Contributor

Summary

  • HOME fallback chain in 8 hook/lib files: process.env.HOME!(HOME || USERPROFILE || homedir()) — prevents crash when HOME is unset on Windows
  • Tilde path resolution in LoadContext: ~/.claude/...join(paiDir, ...) — tilde doesn't expand in Windows cmd.exe
  • Cross-platform utilities in paths.ts: getHome(), getTempPath(), isWindows() — reusable helpers for future hooks
  • identity.ts uses getSettingsPath() from paths.ts instead of hardcoded join(HOME, '.claude/settings.json')

Problem

Hooks crash on Windows with TypeError: Cannot read properties of undefined (reading 'replace') or similar errors because:

  1. process.env.HOME is undefined on Windows (it uses USERPROFILE instead)
  2. The non-null assertion (HOME!) crashes instead of falling back
  3. Tilde (~) is a shell feature — it doesn't expand in cmd.exe or in programmatic path construction

Zero impact on macOS/Linux

On macOS/Linux, HOME is always set (POSIX requirement). The fallback to USERPROFILE and homedir() is never reached. The join() path construction produces identical output to the tilde strings. These changes are dead code on Unix — they only activate on Windows where HOME is unset.

Files changed (10)

File Fix type
hooks/lib/paths.ts Added getHome(), getTempPath(), isWindows()
hooks/lib/identity.ts Use getSettingsPath() instead of hardcoded HOME path
hooks/lib/algorithm-state.ts HOME fallback chain
hooks/AlgorithmTracker.hook.ts HOME fallback chain
hooks/AutoWorkCreation.hook.ts HOME fallback chain
hooks/RatingCapture.hook.ts HOME fallback chain (2 locations)
hooks/SessionSummary.hook.ts HOME fallback chain
hooks/WorkCompletionLearning.hook.ts HOME fallback chain
hooks/LoadContext.hook.ts Tilde path → join(paiDir, ...)
hooks/handlers/RebuildSkill.ts HOME fallback + reuse CORE_DIR const

Relation to #704

This is a companion to PR #704 (installer + documentation). That PR handles install-time path resolution in settings.json. This PR handles runtime path resolution in hook source code. Together they provide complete Windows compatibility.

Test plan

  • Verify hooks work on macOS — no behavioral change expected
  • Verify hooks work on Windows — HOME fallback activates correctly
  • grep -r 'process.env.HOME!' hooks/ returns zero results
  • grep -r '~/.claude/' hooks/LoadContext.hook.ts returns zero results

Closes #440, #543

🤖 Generated with Claude Code

…anielmiessler#440, danielmiessler#543)

Three categories of fixes applied to 10 hook/lib files:

1. HOME environment variable fallback chain:
   `process.env.HOME!` → `(process.env.HOME || process.env.USERPROFILE || require('os').homedir())`
   On macOS/Linux, HOME is always set (POSIX requirement), so the
   fallback chain is never reached — zero behavioral change.
   On Windows, HOME may be unset in cmd.exe contexts; USERPROFILE
   is the Windows equivalent.

2. Tilde path resolution (LoadContext.hook.ts):
   `~/.claude/...` hardcoded strings → `join(paiDir, ...)`
   Tilde expansion is a shell feature that doesn't work in
   Windows cmd.exe or when paths are used programmatically.

3. Cross-platform utilities (hooks/lib/paths.ts):
   - getHome(): HOME || USERPROFILE || os.homedir()
   - getTempPath(): os.tmpdir() instead of hardcoded /tmp/
   - isWindows(): process.platform === 'win32'

Also fixed identity.ts to use getSettingsPath() from paths.ts
instead of hardcoding join(HOME, '.claude/settings.json').

Files changed:
  hooks/lib/paths.ts - Added 3 utility functions
  hooks/lib/identity.ts - Use getSettingsPath() import
  hooks/lib/algorithm-state.ts - HOME fallback
  hooks/AlgorithmTracker.hook.ts - HOME fallback
  hooks/AutoWorkCreation.hook.ts - HOME fallback
  hooks/RatingCapture.hook.ts - HOME fallback (2 locations)
  hooks/SessionSummary.hook.ts - HOME fallback
  hooks/WorkCompletionLearning.hook.ts - HOME fallback
  hooks/LoadContext.hook.ts - Tilde path → join()
  hooks/handlers/RebuildSkill.ts - HOME fallback + use CORE_DIR const

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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.

Failed install with Claude Code on Windows

1 participant