{"record":{"id":"53f8b73a85a59d00","repo":"gitbutlerapp/gitbutler","slug":"failed-to-discover-gitbutler-branch-with-but-js","errorCode":null,"errorMessage":"failed to discover GitButler branch with 'but --json status': {stderr}","messagePattern":"failed to discover GitButler branch with 'but --json status': (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/but-agentlog/src/skim.rs","lineNumber":355,"sourceCode":"}\n\n#[derive(Deserialize)]\nstruct StatusBranch {\n    name: String,\n}\n\nfn applied_gitbutler_branch(workdir: &Path) -> anyhow::Result<String> {\n    let but_path = std::env::current_exe().context(\"failed to locate current executable\")?;\n    let output = ProcessCommand::new(&but_path)\n        .arg(\"-C\")\n        .arg(workdir)\n        .args([\"--json\", \"status\"])\n        .stdin(Stdio::null())\n        .output()\n        .context(\"failed to run 'but --json status' for agentlog skim target discovery\")?;\n    if !output.status.success() {\n        let stderr = String::from_utf8_lossy(&output.stderr);\n        anyhow::bail!(\"failed to discover GitButler branch with 'but --json status': {stderr}\");\n    }\n    let status: StatusReport =\n        serde_json::from_slice(&output.stdout).context(\"failed to parse 'but --json status'\")?;\n    status\n        .stacks\n        .into_iter()\n        .flat_map(|stack| stack.branches)\n        .map(|branch| branch.name)\n        .next()\n        .context(\"no applied GitButler branch found; pass an explicit skim target\")\n}\n","sourceCodeStart":337,"sourceCodeEnd":367,"githubUrl":"https://github.com/gitbutlerapp/gitbutler/blob/58e5313667b857ef39a730e380af31816a7b1768/crates/but-agentlog/src/skim.rs#L337-L367","documentation":"This error is thrown when the agentlog skim tool runs `but --json status` as a subprocess to discover the GitButler branch, and the CLI exits with a non-zero status. The stderr from the failed CLI invocation is embedded in the message to explain why target discovery failed. It is a wrapper around any failure of the underlying `but status` command, so the real cause is in the captured stderr.","triggerScenarios":"Calling `applied_gitbutler_branch` (via `resolve_default_branch_target`) in a directory where `but --json status` fails: not inside a Git workspace, workspace not initialized with GitButler, corrupted metadata, or any CLI-side error that makes `but status` exit non-zero.","commonSituations":"Running the agentlog skim outside a git repository; running before `but init`; PATH resolving to an incompatible or broken `but` binary; lock contention or workspace corruption surfacing as stderr output.","solutions":["Read the `{stderr}` portion of the message to find the actual `but --json status` failure cause","Run `but --json status` manually in the target directory to reproduce and diagnose","Ensure the working directory is a GitButler-initialized workspace (`but init` if needed)","Verify the `but` binary on PATH is the expected version"],"exampleFix":"// before\nlet target = applied_gitbutler_branch(dir)?;\n// after\nlet target = match applied_gitbutler_branch(dir) {\n    Ok(t) => t,\n    Err(e) if e.to_string().contains(\"failed to discover GitButler branch\") => {\n        eprintln!(\"skipping agentlog skim: not in a GitButler workspace: {e}\");\n        return Ok(None);\n    }\n    Err(e) => return Err(e),\n};","handlingStrategy":"try-catch","validationCode":"const out = await run(\"but\", [\"--json\", \"status\"], { stdio: \"ignore\" });\nif (out.status !== 0) throw new Error(\"but CLI unavailable or not a GitButler workspace\");","typeGuard":"function isGitButlerWorkspace(dir: string): boolean {\n  return existsSync(join(dir, \".git\", \"gitbutler\"));\n}","tryCatchPattern":"try {\n  const target = await resolveDefaultBranchTarget(dir);\n} catch (e) {\n  if (String(e).includes(\"failed to discover GitButler branch\")) {\n    logger.warn({ stderr: String(e) }, \"agentlog skim skipped\");\n    return null;\n  }\n  throw e;\n}","preventionTips":["Verify the directory is a GitButler workspace before invoking skim","Pin/verify the `but` binary version on PATH","Call `but init` as part of setup in CI","Always log stderr from subprocess failures to expose the root cause"],"tags":["gitbutler","cli","subprocess","branch-discovery"],"backgroundTag":"git-command-failed","analyzedSha":"58e5313667b857ef39a730e380af31816a7b1768","analyzedAt":"2026-09-18T06:50:32.052Z","contentChangedAt":"2026-09-18T06:50:32.052Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}