fix(cli): use sys.executable for hook commands instead of hardcoded /usr/bin/python3#37
Closed
FunLay123 wants to merge 1 commit into
Closed
fix(cli): use sys.executable for hook commands instead of hardcoded /usr/bin/python3#37FunLay123 wants to merge 1 commit into
FunLay123 wants to merge 1 commit into
Conversation
…usr/bin/python3
Problem
-------
Every shipped hook config (claude, codex, cursor, gemini) hardcodes the
hook command as
"command": "/usr/bin/python3 {{TS_HOOKS_DIR}}/<hook>.py"
The {{TS_HOOKS_DIR}} placeholder is substituted at install time, but the
interpreter path is not -- it lands verbatim in the user's settings file.
The /usr/bin/python3 assumption breaks on every platform where the system
Python is not at that exact path:
- Windows: no /usr/bin/python3 exists at all. Every Bash tool invocation
fires PreToolUse + PostToolUse hooks that both fail with
`/usr/bin/python3: No such file or directory`. tool_capture and the
bash rewriter silently no-op.
- macOS Homebrew / pyenv / conda users: /usr/bin/python3 is the legacy
Apple-shipped Python, not the interpreter that installed token-savior.
Best case the hooks run on the wrong interpreter and cannot import
`token_savior`; worst case it does not exist on newer macOS.
- Linux distros where python3 lives at /usr/local/bin/python3 or inside
a venv: same failure mode.
Fix
---
Introduce a {{TS_PYTHON}} placeholder alongside {{TS_HOOKS_DIR}} and
substitute it at install time with `sys.executable` -- i.e. the exact
interpreter that ran `ts init`. That interpreter is guaranteed to exist
and is the one that has `token_savior` (and therefore the hook scripts'
dependencies) installed.
Both placeholders are now passed through a small `_as_json_string` helper
(`json.dumps(value)[1:-1]`) so values containing backslashes or quotes
remain valid JSON-string content after substitution. This is required on
Windows, where `sys.executable` and the hooks dir both contain `\`.
POSIX behavior is unchanged for paths without characters that need
escaping (the helper is a no-op). On platforms where the prior hardcoded
path happened to be correct (Linux systems with /usr/bin/python3), users
running `ts init` again after this change will see the command value
update to the absolute interpreter path -- existing entries that still
reference /usr/bin/python3 may need to be removed manually since the
merger dedup keys include the full command string.
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.
Summary
Cross-platform fix for
ts init. Currently every shipped hook config hardcodes/usr/bin/python3as the interpreter, which breaks on Windows entirely and on any non-default Python install on macOS / Linux. This PR substitutes the interpreter at install time the same way the install path is already substituted.Problem
Every shipped hook config (
hooks/tool-capture-hooks-config.json,hooks/bash-rewriter-config.json,hooks/tool-capture-codex.json,hooks/tool-capture-cursor.json,hooks/tool-capture-gemini.json) hardcodes:{{TS_HOOKS_DIR}}is substituted at install time./usr/bin/python3is not — it lands verbatim in the user's~/.claude/settings.json(and equivalents). This breaks every platform where the system Python lives elsewhere:/usr/bin/python3does not exist. EveryBashtool invocation fires PreToolUse + PostToolUse hooks, both fail with/usr/bin/python3: No such file or directory.tool_captureandbash_rewritersilently no-op. Observed in the wild — see hook-error spam below./usr/bin/python3is the legacy Apple Python, not the interpreter that rants init. Best case the hooks run on the wrong interpreter and can't importtoken_savior; worst case the path doesn't exist on newer macOS.python3lives at/usr/local/bin/python3or inside a venv: same failure mode.Observed error on Windows (Claude Code):
Fix
Introduce a
{{TS_PYTHON}}placeholder alongside{{TS_HOOKS_DIR}}in all 5 shipped hook configs. Substitute it at install time withsys.executable— the exact interpreter that rants init. That interpreter:token_savior(and the hook scripts' dependencies) installed,Both placeholders go through a small
_as_json_stringhelper (json.dumps(value)[1:-1]) so values with backslashes or quotes remain valid JSON-string content after the textual.replace. Required on Windows wheresys.executableand the hooks dir both contain\. POSIX behavior is unchanged — the helper is a no-op for strings that don't need escaping.Files changed
hooks/tool-capture-hooks-config.json(claude)hooks/bash-rewriter-config.json(claude)hooks/tool-capture-codex.jsonhooks/tool-capture-cursor.jsonhooks/tool-capture-gemini.jsonsrc/token_savior/cli_init/__init__.pyMigration note
Users who already ran
ts initon a system where/usr/bin/python3happened to be a valid path will, on nextts init, see the command value change to the absolutesys.executablepath. Because the merger dedup keys include the full command string, the old/usr/bin/python3 ...entry won't be deduplicated against the new one — a second entry is added. Users on that path should remove the stale/usr/bin/python3entry manually. (Cleaner migration could be a follow-up.)Test plan
_load_hook_bundlessubstitutessys.executablecorrectly, the resulting command's python binary actually exists on disk.pytest tests/test_cli_init.py— existing tests (test_load_hook_bundles_claude_real_repo,test_apply_bundles_combines_post_and_pre, e2etest_run_*) exercise this code path and should continue to pass; on POSIX with/usr/bin/python3assys.executablethe merged settings change only in the command's python prefix value, not in structure.Bashtool → no morepython3: No such file or directory).Relation to #36
Independent of #36 but solves a related layer of the same Windows-incompatibility story:
ts initworks on Windows #36 fixes theJSONDecodeError: Invalid \escaperaised whents inittries to install the hooks on Windows.ts initworks on Windows #36 or via manual editing), they still fail because/usr/bin/python3doesn't exist on Windows / non-default Python installs.Both PRs reuse the same
json.dumps(value)[1:-1]escape technique; this PR factors it out into_as_json_stringfor clarity. Either PR can merge first; they don't conflict.