{"record":{"id":"8142f8841f7297df","repo":"xai-org/grok-build","slug":"git-worktree-add-failed-8142f8","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/worktree/execute.rs","lineNumber":1460,"sourceCode":"    // checkout.workers enables parallel checkout so git populates the\n    // working tree using multiple threads.\n    let output = git::checkout::git_command()\n        .current_dir(&source_root)\n        .arg(\"-c\")\n        .arg(format!(\"checkout.workers={workers}\"))\n        .args([\n            \"worktree\",\n            \"add\",\n            \"--detach\",\n            &dest.to_string_lossy(),\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    tracing::debug!(\n        elapsed = ?start.elapsed(),\n        \"git worktree add (with checkout) complete\"\n    );\n\n    // Get the commit.\n    let commit = git::get_head_commit(dest).context(\"failed to get HEAD commit\")?;\n\n    tracing::info!(\n        elapsed = ?start.elapsed(),\n        commit = %commit,\n        method = \"git_checkout\",\n        \"worktree created via git checkout\"\n    );\n\n    Ok(CreateWorktreeResult {","sourceCodeStart":1442,"sourceCodeEnd":1478,"githubUrl":"https://github.com/xai-org/grok-build/blob/bc7f02eddd3d84085849dc19ed216f11c23b0571/crates/codegen/xai-fast-worktree/src/worktree/execute.rs#L1442-L1478","documentation":"execute_git_checkout_worktree runs `git worktree add` (with checkout) as a subprocess and, on non-zero exit status, bails with the full stderr. This surfaces git's own reason — branch conflicts, dirty/locked paths, invalid refs, missing commits, etc. — under a stable message prefix.","triggerScenarios":"Calling execute_git_checkout_worktree (projected-source path) when the target branch/commit is invalid or missing, a worktree with the same branch already exists ('already checked out'), the destination path already exists and is non-empty, or the source repo is corrupt/locked.","commonSituations":"Two worktrees created concurrently for the same branch (git forbids checking out the same branch twice); stale branch names after a force-push/rebase; leftover destination directory from a previously cancelled run; dubious-ownership repo in CI.","solutions":["Run `git worktree add <dest> <ref>` manually in the source and read git's stderr for the concrete cause.","Use a detached HEAD or a unique new branch when the branch is already checked out in another worktree.","Remove a stale/non-empty destination directory before retrying.","Run `git worktree prune` to clear stale worktree metadata, and check safe.directory/ownership config in CI."],"exampleFix":"// before: reusing the same branch for many worktrees\nlet ref = \"main\";\n// after: unique branch per worktree (or detached)\nlet ref = format!(\"wt/{}-{uuid}\", task_name); // or \"--detach HEAD\"","handlingStrategy":"try-catch","validationCode":"// pre-checks before git worktree add\nassert!(dest.parent().is_some(), \"dest parent must exist\");\nassert!(!dest.exists(), \"dest already exists: {dest:?}\");\nlet rev = std::process::Command::new(\"git\")\n    .args([\"rev-parse\", \"--verify\", \"--quiet\", &ref])\n    .current_dir(source)\n    .status()\n    .context(\"ref check failed\")?;\nassert!(rev.success(), \"ref {ref:?} not found in source\");","typeGuard":null,"tryCatchPattern":"match create_worktree(&plan) {\n    Err(e) if e.to_string().contains(\"is already used by worktree\")\n        || e.to_string().contains(\"already checked out\") => {\n        let mut plan = plan.clone();\n        plan.reference = format!(\"--detach\"); // or unique branch\n        create_worktree(&plan).context(\"retry with unique ref failed\")\n    }\n    other => other,\n}","preventionTips":["Use unique branch names or detached HEAD per worktree","Run `git worktree prune` after abnormal exits","Ensure destination paths are absent/non-empty-free before creating","Set safe.directory in CI for root-owned checkouts","Resolve refs (rev-parse) before passing them to worktree add"],"tags":["git","worktree","subprocess"],"backgroundTag":"git-worktree-add-failed","analyzedSha":"bc7f02eddd3d84085849dc19ed216f11c23b0571","analyzedAt":"2026-08-31T04:59:42.031Z","schemaVersion":2},"datasetVersion":"2026-08-31T09:17:48.483Z"}