Skip to content

bug(init): global Claude init fails when ~/.claude is missing #2519

Description

@elite133713

Bug report

Summary

rtk init -g fails on a fresh user profile when the Claude config directory does not already exist.

The failure occurs before RTK can create RTK.md because the atomic write implementation creates a temporary file inside the target file's parent directory, and that parent directory is missing.

Steps to reproduce

Run global init on a machine or temp home where ~/.claude does not exist:

rm -rf ~/.claude
rtk init -g

Equivalent isolated reproduction:

tmp=$(mktemp -d)
HOME="$tmp/home" CLAUDE_CONFIG_DIR="$tmp/.claude" rtk init -g --auto-patch

Actual behavior

rtk: Failed to write RTK.md: /Users/rm-rf/.claude/RTK.md: Failed to create temp file in /Users/rm-rf/.claude: No such file or directory (os error 2)

Expected behavior

rtk init -g should create the missing Claude config directory and then write:

  • ~/.claude/RTK.md
  • ~/.claude/CLAUDE.md
  • ~/.claude/settings.json when patching is enabled

Dry-run mode should continue to report intended writes without creating the directory.

Cause

atomic_write() uses NamedTempFile::new_in(parent), so callers that write into global config paths must ensure the parent directory exists before attempting the atomic write.

Fix PR

Proposed fix: #2518

Related but separate: #1840 addresses the Cursor-only rtk init -g --agent cursor path and missing ~/.cursor; this issue is for plain/global Claude init with missing ~/.claude.

Validation in PR #2518

  • Added regression tests for missing Claude config dir in default global init, hook-only global init, and dry-run.
  • cargo fmt --all --check
  • cargo test missing_claude_dir -- --nocapture
  • cargo test --all (2212 passed, 8 ignored)
  • cargo clippy --all-targets -- -D warnings
  • Manual temp-home check: ./target/debug/rtk init -g --auto-patch

Activity

  1. pszymkowiak commented on Jun 25, 2026

    @pszymkowiak
    Collaborator

    wshm · Automated triage by AI

    🔍 Automated Triage

    🐛 Category bug
    🟡 Priority medium
    🎯 Confidence 97%

    Summary

    rtk init -g crashes on fresh profiles where ~/.claude doesn't exist; fix PR #2521 is already open and should be reviewed/merged.

    This looks like a simple fix that could be auto-resolved. Repo maintainers can use /wshm fix to attempt it.

    📁 Relevant files
    • src/init/mod.rs
    • src/util/fs.rs

    Triaged automatically by wshm · This is an automated analysis, not a human review.

  2. KuSh commented on Sep 17, 2026

    @KuSh
    Collaborator

    Still reproduces on develop (d402152f, and current 5e0f92cd), and it's now blocking a PR.

    $ HOME=$(mktemp -d) rtk init -g --auto-patch
    rtk: Failed to write RTK.md: $HOME/.claude/RTK.md: Failed to create temp file
    in $HOME/.claude: No such file or directory (os error 2)
    EXIT=1
    

    Two things worth adding to this issue:

    1. It fails on CI, not just fresh user profiles. #4052 added an integration test that exercises rtk init -g, and it fails on windows-latest for exactly this reason — dirs::home_dir() ignores $HOME on Windows, so the test's temp home is bypassed and rtk writes into the runner's real C:\Users\runneradmin\.claude, which doesn't exist:

    rtk: Failed to write RTK.md: C:\Users\runneradmin\.claude\RTK.md: Failed to create temp file
    in C:\Users\runneradmin\.claude: The system cannot find the path specified. (os error 3)
    

    2. The blast radius is about to grow. #4052 makes --opencode additive, which routes it through this same path. On develop, rtk init -g --opencode works on a machine with no ~/.claude (it skips Claude entirely); with that PR it exits 1 and installs nothing, not even the OpenCode plugin — because RTK.md is written at src/hooks/init.rs:1459, before ensure_opencode_plugin_installed at :1464. OpenCode-only users are precisely the ones without ~/.claude.

    Fix, built and verified here (fmt + clippy clean, 3613 lib tests + all integration binaries green):

    let claude_dir = resolve_claude_dir()?;
    if !dry_run {
        fs::create_dir_all(&claude_dir).with_context(|| {
            format!(
                "Failed to create Claude config directory: {}",
                claude_dir.display()
            )
        })?;
    }

    at src/hooks/init.rs:1451 (run_default_mode) and :1249 (the settings.json patch path). run_claude_md_mode_with already does this at :1899, which is why --claude-md is the one global mode that survives today. With the fix, rtk init -g on a bare HOME exits 0 and writes RTK.md + settings.json, and --dry-run still creates nothing.

  3. elite133713 commented on Sep 23, 2026

    @elite133713
    ContributorAuthor

    #2521 is fixed, tested, and ready for review and merge.

    It is updated onto current develop. Global init still creates the missing Claude config directory before the atomic write, and dry-run still creates nothing. Verified locally with cargo test missing_claude (3 passed), plus the develop tests that overlapped the merge (utf8_bom, cwd_guard).

  4. elite133713 commented on Sep 23, 2026

    @elite133713
    ContributorAuthor

    @KuSh this is the failure #2521 fixes.

    write_if_changed and the settings.json patch create the Claude config directory before the atomic write, so rtk init -g on a bare home writes RTK.md and settings.json instead of exiting on the missing parent. Dry-run still creates nothing. --opencode no longer stops before the plugin install: test_global_opencode_installs_when_claude_dir_missing checks that a missing Claude dir still gets RTK.md and ~/.config/opencode/plugins/rtk.ts.

    That OpenCode test is Unix-only. dirs::home_dir() follows $HOME on Unix and ignores it on Windows, so the plugin path cannot be pinned with $HOME on windows-latest. The Claude path tests set CLAUDE_CONFIG_DIR, which resolve_claude_dir honors on every OS.

    Verified locally: cargo test test_global_opencode_installs_when_claude_dir_missing passed.

  5. added a commit that references this issue on Sep 28, 2026
    0237bb1
  6. added a commit that references this issue on Oct 3, 2026
    9c69150
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions