{"record":{"id":"8e51fecc84f70b74","repo":"affaan-m/ECC","slug":"git-worktree-add-failed-stderr","errorCode":null,"errorMessage":"git worktree add failed: {stderr}","messagePattern":"git worktree add failed: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"ecc2/src/worktree/mod.rs","lineNumber":126,"sourceCode":"\n    // Get current branch as base\n    let base = get_current_branch(repo_root)?;\n\n    std::fs::create_dir_all(&cfg.worktree_root)\n        .context(\"Failed to create worktree root directory\")?;\n\n    let output = Command::new(\"git\")\n        .arg(\"-C\")\n        .arg(repo_root)\n        .args([\"worktree\", \"add\", \"-b\", &branch])\n        .arg(&path)\n        .arg(\"HEAD\")\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::info!(\n        \"Created worktree at {} on branch {}\",\n        path.display(),\n        branch\n    );\n\n    let info = WorktreeInfo {\n        path,\n        branch,\n        base_branch: base,\n    };\n\n    if let Err(error) = sync_shared_dependency_dirs_in_repo(&info, repo_root) {\n        tracing::warn!(\n            \"Shared dependency cache sync warning for {}: {error}\",\n            info.path.display()","sourceCodeStart":108,"sourceCodeEnd":144,"githubUrl":"https://github.com/affaan-m/ECC/blob/8321021c54d670126ce3b2969d5deb880b4b0c2a/ecc2/src/worktree/mod.rs#L108-L144","documentation":"create_for_session_in_repo shells out to `git worktree add <path> -b <branch> HEAD`. If the git process exits non-zero, the raw stderr is surfaced as this error so the caller knows why the worktree could not be created.","triggerScenarios":"Calling create_for_session when the target worktree path already exists, the derived branch name already exists, HEAD is unborn (empty repo), or git cannot write to the parent directory.","commonSituations":"A stale worktree directory left behind by a crashed session; a previous run already created the branch with the same prefix/session-id; running inside a repo with no commits yet; read-only or full filesystem.","solutions":["Remove the stale worktree (`git worktree remove <path>`) and delete the stale branch (`git branch -D <branch>`) before retrying","Read the stderr in the error message — 'already exists', 'invalid reference', or 'not a git repository' each point to a different cause","Commit at least one commit in the repo (worktree add HEAD requires a valid HEAD)","Ensure the parent directory for the worktree path is writable"],"exampleFix":"// before\ncreate_for_session(&session_id, &cfg).await?;\n// after\nlet path = worktree_path_for(&session_id);\nif Path::new(&path).exists() {\n    Command::new(\"git\").args([\"worktree\", \"remove\", path_str]).output()?;\n}\ncreate_for_session(&session_id, &cfg).await?;","handlingStrategy":"try-catch","validationCode":"// Check prerequisites before calling create_for_session\nif !repo_root.join(\".git\").exists() { bail!(\"not a git repository\"); }\nlet branch = format!(\"{prefix}/{session_id}\");\nlet exists = Command::new(\"git\").args([\"rev-parse\", \"--verify\", &branch]).output()?.status.success();\nif exists { bail!(\"branch {branch} already exists\"); }","typeGuard":null,"tryCatchPattern":"match create_for_session(&session_id, &cfg).await {\n    Ok(wt) => wt,\n    Err(e) if e.to_string().contains(\"already exists\") => {\n        cleanup_stale_worktree(&session_id)?;\n        create_for_session(&session_id, &cfg).await?\n    }\n    Err(e) => return Err(e),\n}","preventionTips":["Clean up worktrees and their branches when sessions end or crash","Require at least one commit before creating worktrees (HEAD must resolve)","Keep worktree_branch_prefix unique per installation to avoid branch collisions","Read the git stderr embedded in the error to pick the right fix"],"tags":["git","worktree","rust","process"],"backgroundTag":"git-command-failed","analyzedSha":"8321021c54d670126ce3b2969d5deb880b4b0c2a","analyzedAt":"2026-09-16T10:08:13.343Z","contentChangedAt":"2026-09-16T10:08:13.343Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}