{"record":{"id":"c540e2528b0afb12","repo":"xai-org/grok-build","slug":"git-worktree-add-failed","errorCode":null,"errorMessage":"git worktree add failed: {}","messagePattern":"git worktree add failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/codegen/xai-fast-worktree/src/git/worktree.rs","lineNumber":26,"sourceCode":"\n/// Create a git worktree with `--no-checkout`. Blocking.\npub(crate) fn worktree_add_no_checkout(source: &Path, dest: &str, git_ref: &str) -> Result<()> {\n    let output = git_command()\n        .current_dir(source)\n        .args([\n            \"worktree\",\n            \"add\",\n            \"--detach\",\n            \"--no-checkout\",\n            dest,\n            git_ref,\n        ])\n        .output()\n        .context(\"failed to run git worktree add\")?;\n\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"git worktree add failed: {}\", stderr);\n    }\n\n    Ok(())\n}\n\n/// Which stale registrations [`remove_stale_worktree_registrations`] removes.\n#[derive(Clone, Copy, Debug)]\nenum StaleWorktreeMatch<'a> {\n    /// Exactly the registration whose recorded worktree path is this path.\n    Path(&'a Path),\n    /// Every registration whose recorded worktree path is under this prefix\n    /// (e.g. a tool-owned base directory, proving ownership of the entries).\n    UnderPrefix(&'a Path),\n}\n\n/// Remove stale `.git/worktrees/<id>` registrations matching `match_rule`.\n/// Best-effort; returns the count removed.\n///","sourceCodeStart":8,"sourceCodeEnd":44,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/git/worktree.rs#L8-L44","documentation":"worktree_add_no_checkout shells out to `git worktree add --no-checkout` and bails with this message when git exits non-zero, appending git's stderr. It indicates the requested worktree could not be registered or created by git — a ref conflict, an existing path, or a repo-level git error.","triggerScenarios":"Creating a pooled worktree when the destination path already exists; a worktree with the same branch/ref is already checked out elsewhere (`fatal: 'ref' is already checked out`); the commit/ref doesn't exist; the main repo is bare and misconfigured, or the destination is on a read-only/failed mount.","commonSituations":"Re-running a create after a partial failure left the path behind; two agents creating worktrees from the same branch simultaneously; a pruned repo where the target commit was garbage-collected; NFS/Grove destination mount down mid-create.","solutions":["Read the embedded stderr: if it says 'already checked out', add `--detach` or use a unique branch (`git worktree add -b <new-branch> <path> <ref>`).","If the destination exists, remove the leftover directory or registration (`git worktree prune`, then delete the dir) before retrying.","Verify the target ref exists in the source repo with `git rev-parse <ref>` before creating.","Retry after resolving the underlying git error — worktree_add_no_checkout does not clean up partial state on its own."],"exampleFix":"// before: reusing a branch that may already be checked out\nworktree_add_no_checkout(repo, &dest, Some(\"main\"))?;\n// after: detach to allow multiple worktrees from the same ref\nworktree_add_no_checkout(repo, &dest, None)?; // detached HEAD, no checkout conflict","handlingStrategy":"validation","validationCode":"use std::process::Command;\nfn can_add_worktree(repo: &Path, dest: &Path, ref_name: Option<&str>) -> Result<(), String> {\n    if dest.exists() { return Err(format!(\"destination {} already exists\", dest.display())); }\n    if let Some(r) = ref_name {\n        let ok = Command::new(\"git\").args([\"rev-parse\", \"--verify\", r]).current_dir(repo).output()\n            .map(|o| o.status.success()).unwrap_or(false);\n        if !ok { return Err(format!(\"ref {r} not found in repo\")); }\n    }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match worktree_add_no_checkout(repo, &dest, ref_name) {\n    Err(e) if e.to_string().contains(\"already checked out\") => {\n        // fall back to a detached worktree for the same ref\n        worktree_add_no_checkout(repo, &dest, None)?;\n    }\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        remove_leftover(&dest)?;\n        worktree_add_no_checkout(repo, &dest, ref_name)?;\n    }\n    Err(e) => return Err(e),\n    Ok(()) => {},\n}","preventionTips":["Delete leftover destination directories before re-running create.","Use unique branches or --detach when multiple worktrees need the same ref.","Rev-parse the target ref before creating the worktree.","Confirm the destination filesystem is writable and mounted."],"tags":["git","worktree","subprocess-failed","rust"],"backgroundTag":"git-worktree-add-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}