fix: create log directory before stderr redirect in hooks#21
fix: create log directory before stderr redirect in hooks#21dmar842 wants to merge 1 commit intoDigital-Process-Tools:mainfrom
Conversation
The SessionStart and PostToolUse hook commands redirect stderr to
${CLAUDE_PROJECT_DIR}/.remember/logs/hook-errors.log, but nothing
creates the logs/ directory first. When the plugin activates in a
project where .remember/logs/ doesn't exist yet, bash emits:
/usr/bin/bash: line 1: <path>/.remember/logs/hook-errors.log:
No such file or directory
and the hook scripts never run. Prepend an idempotent `mkdir -p`
to both command strings so the directory exists before the redirect.
`2>/dev/null` on mkdir prevents its own failures from polluting the
parent shell.
There was a problem hiding this comment.
Pull request overview
Ensures Claude Code hook stderr redirection won’t fail on fresh projects by creating the .remember/logs directory before appending to hook-errors.log.
Changes:
- Prepend an idempotent
mkdir -p "${CLAUDE_PROJECT_DIR:-.}/.remember/logs"to theSessionStarthook command. - Apply the same directory-creation step to the
PostToolUsehook command.
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
Also hit this on Linux today in a project where Confirmed the bash behavior locally: See also #23, which is an independent report of the same bug filed ~9 hours after this PR went up. The fix looks right. One small thing worth considering: — Written by Claude Code (Opus 4.7) on behalf of @0reo |
|
+1 — hitting this on remember v0.5.0 inside a Claude Code worktree ( The |
|
+1 from Windows 11 + Git Bash + Claude Code 2.1.116. Error seen verbatim on every Ran Also filed independently as #23 by another reporter. |
|
Heads-up — PR #28 (Windows bootstrap fix + TMPDIR portability + README prereqs) lands the same Maintainer's call on which to take:
Either way, only one should land. No harm caused by this PR — just flagging the overlap so the maintainer can pick a path. (Drive-by comment from a downstream user who hit the same race on macOS — appreciate the fix.) |
|
Closing in favor of #33 which takes a more architectural approach — centralizing directory creation in Thank you @dmar842 for the clean diagnosis and fix! Your PR description nailed the root cause (bash opens the 2>> target before the script runs) and directly inspired our solution. The community benefits from people who write clear bug reports with reproduction steps. Much appreciated! |
… redirect The SessionStart and PostToolUse hook commands in hooks.json redirected stderr to .remember/logs/hook-errors.log via 2>>. Bash opens the redirect target before running the script, so on fresh projects where .remember/logs/ does not exist, the redirect fails and the hook script never executes. Four independent users reported this (issues Digital-Process-Tools#23, Digital-Process-Tools#27, Digital-Process-Tools#31, Digital-Process-Tools#32). Fix: introduce scripts/bootstrap-dirs.sh as the single source of truth for the .remember/ directory layout. Every hook script sources it after resolve-paths.sh. It creates tmp/, logs/, logs/autonomous/, .gitignore, and redirects stderr via exec 2>> (guarded by -d check for read-only filesystems). hooks.json commands are now clean one-liners with no inline redirects. - New: scripts/bootstrap-dirs.sh (dir creation + stderr redirect) - session-start-hook.sh: source bootstrap-dirs.sh, remove duplicate mkdir - post-tool-hook.sh: source bootstrap-dirs.sh - hooks.json: remove 2>> redirects from both commands - 17 new tests covering: bug reproduction, fresh project, partial state, spaces/unicode in paths, read-only filesystem, idempotency, git worktrees, concurrent sessions, source ordering Closes Digital-Process-Tools#23, Digital-Process-Tools#27, Digital-Process-Tools#31, Digital-Process-Tools#32 Supersedes Digital-Process-Tools#21 Co-Authored-By: Max <noreply>
Problem
The
SessionStartandPostToolUsehook commands inhooks/hooks.jsonredirect stderr to${CLAUDE_PROJECT_DIR}/.remember/logs/hook-errors.log, but nothing guarantees.remember/logs/exists first.In any Claude Code project where the plugin has not yet run, bash emits a hook error like:
The hook scripts (
session-start-hook.sh,post-tool-hook.sh) never run. I hit this today on Windows but the same race exists on Linux/macOS — any fresh project activation triggers it.Fix
Prepend an idempotent
mkdir -pto both hook command strings, with2>/dev/nullso any mkdir failure (e.g. read-only filesystem) is silent and doesn't itself fail the redirect it's trying to enable.Diff
Same change applied to both
SessionStartandPostToolUsecommands.Test
Reproduced the bug in a Claude Code project dir without
.remember/, then applied the patch manually and confirmed the hook error disappears on the nextPostToolUsefire andhook-errors.logis created empty as expected.