fix(init): nested hook schema + honor CLAUDE_CONFIG_DIR#6
Open
DamieMoon wants to merge 1 commit into
Open
Conversation
`ctx init` reported `Hooks ✓` while writing hooks that never fire. Two
independent defects combined:
1. Flat hook schema — `stepHooks()` appended `{"type":"command","command":…}`
directly into the event array. Current Claude Code requires the nested
matcher-group form `{"hooks":[{"type":"command","command":…}]}` for every
event (incl. matcher-less SubagentStart/SubagentStop) and silently ignores
the flat form. Added `newHookGroup()` and emit it from `stepHooks()`.
2. Hardcoded path — `claudeSettingsPath()` always wrote `~/.claude/settings.json`
and ignored `CLAUDE_CONFIG_DIR`, so on installs that relocate the config dir
it wrote a file Claude Code never reads (this also defeated the statusline
step via the shared resolver). Now: `$CLAUDE_CONFIG_DIR/settings.json` if
set, else `~/.claude/settings.json`.
Also: `hasHookEntry()` now recurses into a nested `hooks[]` (in addition to the
legacy flat form), so re-running `init` is idempotent against both shapes and
no longer appends duplicates.
Docs/tests: nest the hook examples in README §3 and the `brief.go` help; update
`init_test.go` fixtures to the nested form and add coverage for
`CLAUDE_CONFIG_DIR` resolution and nested-entry detection.
Per maintainer review, the originally-proposed `~/.config/claude-code`
fallback was dropped — the resolver is exactly CLAUDE_CONFIG_DIR-or-~/.claude.
gofmt/vet/build clean; `internal/cli` package tests pass.
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #5.
ctx initreportedHooks ✓while writing hooks that never execute. As verified in the issue, two independent defects combined (flat hook schema that current Claude Code silently ignores; hardcoded~/.claude/settings.jsonthat ignoresCLAUDE_CONFIG_DIR), plus a non-idempotenthasHookEntry.Changes
go/internal/cli/init.goclaudeSettingsPath()now honorsCLAUDE_CONFIG_DIR: returns$CLAUDE_CONFIG_DIR/settings.jsonwhen set, else~/.claude/settings.json(the Windows%USERPROFILE%branch is retained). Per your review, the originally-proposed~/.config/claude-codefallback was dropped — the resolver is exactlyCLAUDE_CONFIG_DIR-or-~/.claude. This also fixes the statusline step (shared resolver).newHookGroup(cmd)helper emits the nested matcher-group form{"hooks":[{"type":"command","command":cmd}]};stepHooks()now writes that instead of the silently-ignored flat form.hasHookEntry()recurses into a nestedhooks[](in addition to the legacy flat form), so re-runninginitis idempotent against both shapes and no longer appends duplicates.Docs — nested the hook examples in
README.md§3 and thebrief.go--hookhelp. (ThestatusLineexamples were already the correct shape and are left unchanged.)Tests (
go/internal/cli/init_test.go)TestHasHookEntrykeeps the legacy-flat cases and adds nested-form detection.TestNewHookGroup.TestClaudeSettingsPathnow covers both branches (unset →~/.claude; set →$CLAUDE_CONFIG_DIR/settings.json).TestLoadSettings_ExistingFileandTestSettingsSave_PreservesExistingfixtures to the nested form.Verification
gofmt -lclean on the changed files;go vet ./...andgo build ./...clean.go test ./internal/cli/passes, including the new/updated tests.🤖 Generated with Claude Code