rulesync icon indicating copy to clipboard operation
rulesync copied to clipboard

Hook import: `$CLAUDE_PROJECT_DIR` is normalized only as the first token; interpreter-prefixed commands leak it into codexcli hooks

Open artgas1 opened this issue 2 months ago • 0 comments

Summary

When importing Claude Code hooks, rulesync normalizes $CLAUDE_PROJECT_DIR to the portable ./ form only when the variable begins the command. With an interpreter prefix or a fully quoted path, the variable survives into the canonical hooks and then into the codexcli output. Codex does not set CLAUDE_PROJECT_DIR for project hooks, so these hooks break there.

Reproduction (rulesync 21.0.0, Codex CLI 0.153.1)

.claude/settings.json with seven PreToolUse command hooks, then:

rulesync import --targets claudecode --features hooks
rulesync generate --targets codexcli --features hooks
Claude command generated .codex/hooks.json command
"$CLAUDE_PROJECT_DIR"/.claude/hooks/a.sh ./.claude/hooks/a.sh ok
$CLAUDE_PROJECT_DIR/.claude/hooks/c.sh ./.claude/hooks/c.sh ok
${CLAUDE_PROJECT_DIR}/.claude/hooks/d.sh ./.claude/hooks/d.sh ok
"$CLAUDE_PROJECT_DIR/.claude/hooks/b.sh" unchanged leaks
python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/e.py" unchanged leaks
python3 "$CLAUDE_PROJECT_DIR"/.claude/hooks/f.py unchanged leaks
node $CLAUDE_PROJECT_DIR/.claude/hooks/g.js unchanged leaks

What happens in Codex

  • A probe hook in a trusted project shows that CLAUDE_PROJECT_DIR is unset for Codex 0.153.1 project hooks.
  • For python3 "$CLAUDE_PROJECT_DIR/.claude/hooks/x.py", the path becomes /.claude/hooks/x.py. Python exits with code 2 on a missing script, and Codex treats exit code 2 as a block. Result:
    • every matching tool call is refused with Command blocked by PreToolUse hook: ... can't open file '/.claude/hooks/x.py';
    • the same command on UserPromptSubmit blocks every prompt, so the turn ends with zero tokens.

Interpreter-prefixed hooks are a common pattern, so a Claude Code setup that works can turn into a Codex setup that refuses everything.

Suggestion

  • On import, normalize $CLAUDE_PROJECT_DIR, ${CLAUDE_PROJECT_DIR} and their quoted forms wherever they appear in the command, not only as the first token.
  • As a fallback, warn when a tool-specific variable remains in a hook command generated for a target that does not define it.

artgas1 avatar Sep 26 '26 12:09 artgas1