Skip to content

fix(config): expand a bare tilde in CLAUDE_CONFIG_DIR - #4247

Merged
max-sixty merged 3 commits into
max-sixty:mainfrom
hiro-nikaitou:fix/expand-tilde-in-claude-config-dir
Sep 24, 2026
Merged

max-sixty merged 3 commits into
max-sixty:mainfrom
hiro-nikaitou:fix/expand-tilde-in-claude-config-dir

Conversation

@hiro-nikaitou

@hiro-nikaitou hiro-nikaitou commented Sep 23, 2026 •

Copy link
Copy Markdown
Contributor

A literal ~ (rather than ~/...) in CLAUDE_CONFIG_DIR was not expanded: str::strip_prefix("~/") misses a bare ~, so the value fell through to PathBuf::from("~") and install-statusline wrote a directory literally named ~ under the cwd.

The tilde is now expanded through the same home_dir() the ~/.claude fallback two lines below already uses, which is the rule that honors an explicit HOME/USERPROFILE. That keeps claude_config_dir on one home rule instead of two, and an unresolvable home stays a refusal — home_dir() returns None, so require_settings_path bails instead of joining onto a relative ~ path. (Routing the fallback through worktrunk::path::home_dir would settle the split in the other direction, but it would leave this module's home_dir() with no callers, and ~ is expanded by the shell from HOME.)

Verified on Windows, temp home + scratch repo, CLAUDE_CONFIG_DIR=~:

  • upstream main: HOME_SETTINGS=False CWD_TILDE_DIR=True CWD_TILDE_SETTINGS=True — a ~ directory under the cwd
  • this branch: HOME_SETTINGS=True CWD_TILDE_DIR=False — settings.json at <home>/settings.json
  • cargo test --test integration test_plugins_claude_install_statusline_expands_tilde_in_claude_config_dir passes, so the ~/custom-claude case is unchanged

On the third point: yes please, push the bare ~ case onto that test — it is the exact shape that would have caught this, and the missing ~ directory under the cwd makes the assertion explicit.

@worktrunk-bot worktrunk-bot left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

claude_config_dir() now resolves a ~ against a different home directory than the ~/.claude fallback two lines below it. worktrunk::path::expand_tilde goes through worktrunk::path::home_dir (the home crate), whose Windows home_dir_inner reads USERPROFILE and then SHGetKnownFolderPath — it never looks at HOME. The module-local home_dir() that the fallback still uses tries HOME first. So on Windows with HOME pointing somewhere other than %USERPROFILE% (MSYS2, Cygwin), CLAUDE_CONFIG_DIR=~/custom and an unset CLAUDE_CONFIG_DIR now land under different homes; before this change both followed HOME. set_temp_home_env sets both variables to the same path, so no test can see the split. #4106's item 9 called the two home rules "worth a second look rather than a fix" — this leaves home_dir() with a single remaining caller and makes one function depend on both rules, which is the harder version of that to reason about. Routing the fallback through worktrunk::path::home_dir too would settle it in the same direction the rest of the PR argues for (pi.rs and omp.rs already use it for both halves).

The same swap drops the loud failure when no home resolves at all. expand_tilde returns the path unchanged in that case, so CLAUDE_CONFIG_DIR=~/custom becomes Some("~/custom"), require_settings_path joins settings.json onto a relative path, and you get a ~ directory under the cwd plus ✓ Statusline configured — the outcome this PR set out to remove, relocated to a different trigger. The replaced code returned None there, and require_settings_path bailed with "Could not determine Claude Code config directory". An unresolvable home is an improbable environment and CLAUDE.md's Don't Defend Improbable Environments says not to carry a fallback chain for one, but it also says "Dropping a fallback still means failing with an error" — so this is worth a deliberate call rather than a side effect, whichever way it goes.

The bare ~ the PR fixes isn't pinned by a test. test_plugins_claude_install_statusline_expands_tilde_in_claude_config_dir sets CLAUDE_CONFIG_DIR=~/custom-claude, which the replaced strip_prefix("~/") already handled, and it's the only test that exercises a tilde on this path — so the regression this fixes would come back silently. Adding "~" as a second case there (assert settings.json lands at <home>/settings.json and no ~ directory appears under the cwd) covers it; happy to push that commit if you'd like.

@hiro-nikaitou
hiro-nikaitou force-pushed the fix/expand-tilde-in-claude-config-dir branch from f3a7ad2 to 7be52a3 Compare September 23, 2026 16:07
@hiro-nikaitou

Copy link
Copy Markdown
Contributor Author

Addressed in 7be52a3 — all three points, in one commit that lands on 4 added lines.

  1. One home rule. The value now goes through the home_dir() the ~/.claude fallback already used, rather than worktrunk::path::expand_tilde. So claude_config_dir no longer depends on both rules, and ~ follows the rule that honors an explicit HOME/USERPROFILE — the one a shell would have used when it expanded the ~ in the first place. Going the other way (routing the fallback through the crate rule) would have left this module's home_dir() with no callers, which is the harder version of the same split.
  2. The loud failure is back. home_dir() returns None when nothing resolves, so claude_config_dir returns None and require_settings_path bails with Could not determine Claude Code config directory — no relative ~ path, no ✓ Statusline configured. expand_tilde's unchanged-path fallback is what dropped that.
  3. The test — yes please, push it. The bare ~ case (asserting <home>/settings.json and no ~ directory under the cwd) is exactly the shape that would have caught this.

Evidence, Windows, temp home + scratch repo, CLAUDE_CONFIG_DIR=~:

  • upstream main: HOME_SETTINGS=False CWD_TILDE_DIR=True CWD_TILDE_SETTINGS=True
  • this branch: HOME_SETTINGS=True CWD_TILDE_DIR=False
  • cargo test --test integration test_plugins_claude_install_statusline_expands_tilde_in_claude_config_dir → 1 passed, so ~/custom-claude is unchanged

`test_plugins_claude_install_statusline_expands_tilde_in_claude_config_dir`
only exercised `~/custom-claude`, which the old `strip_prefix("~/")` already
handled, so the bare `~` regression could come back silently. Parametrize the
test over both values and assert the settings file lands under the temp home.

Against the pre-fix `claude_config_dir`, `case_2_bare_tilde` fails (no
`<home>/settings.json`); with the fix both cases pass.
@worktrunk-bot

Copy link
Copy Markdown
Collaborator

Pushed as 15c8280cf6bdba2d0c8c68b41ebcab7ceb3932c4 — test_plugins_claude_install_statusline_expands_tilde_in_claude_config_dir is now an rstest over two cases: case_1_tilde_slash (~/custom-claude → <home>/custom-claude/settings.json, the existing behaviour) and case_2_bare_tilde (~ → <home>/settings.json). The "no ~ directory under the cwd" assertion already in the test covers both, so it doubles as the literal-path check for the bare case.

I checked the new case actually fails on the code it pins: reverting src/commands/config/show.rs to its pre-fix state and rerunning gives case_2_bare_tilde failing on a missing <home>/settings.json while case_1_tilde_slash still passes. With the fix in place both pass, and the other seven install-statusline tests are green too.

Items 1 and 2 read as settled to me. Routing through the module-local home_dir() keeps claude_config_dir on one home rule and the HOME-first one, and home_dir() returning None restores the Could not determine Claude Code config directory bail instead of a relative ~ join — which was the substance of both review points.

@worktrunk-bot worktrunk-bot left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CLAUDE_CONFIG_DIR=~\custom still writes a literal ~ directory on Windows. str::strip_prefix("~/") only matches a forward slash and the new dir == "~" branch only matches the bare value, so a backslash form falls through to PathBuf::from(r"~\custom") and install-statusline creates <cwd>\~\custom\settings.json — the same failure this PR fixes, on the platform where a value set outside a shell is most likely to carry a backslash. The docstring now reads "A leading ~ in the value is expanded against the home directory", which states it unconditionally.

Path::strip_prefix matches whole components, so on Windows it treats \ and / alike and absorbs the bare ~ into the same branch — which is how worktrunk::path::expand_tilde matches. Suggested inline; it keeps this module on its own home_dir() (the home rule is untouched) and keeps the None that makes require_settings_path bail, and it collapses the two branches into one. I applied it locally to check: ~user/repo still doesn't match (its first component is ~user), ~/custom-claude is unchanged, and cargo check plus every test_plugins_claude_install_statusline* integration test passes.

Comment thread src/commands/config/show.rs Resolved
`str::strip_prefix("~/")` only matches a forward slash, so a Windows value
like `~\custom` was returned as a literal `~` directory. Matching on path
components accepts either separator and drops the special case.

Signed-off-by: hiro-nikaitou <[email protected]>
@max-sixty
max-sixty merged commit f88b827 into max-sixty:main Sep 24, 2026
39 checks passed
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.

3 participants