Repository navigation
fix(prune): wrap multi-line git errors with context to silence debug_assert - #2567
Merged
Merged
Conversation
…assert `step_prune`'s foreground path propagated raw multi-line git stderr from `Repository::run_command`/`WorkingTree::run_command` via `?` without context. That produced a top-level anyhow message with embedded newlines and an empty chain — exactly the case `print_command_error`'s `debug_assert!(false, "Multiline error without context")` panics on. Surfaced as the Windows flake of `test_prune_skips_unmerged` (#2564), where git failed to read `.git/config` mid-prune; the multi-line stderr tripped the assert and `cargo test` exited 101 instead of rendering the error. Wrapping at the helper would also "fix" the assert but break callers that extract `e.to_string()` for structured display (e.g. rebase conflicts at `step_commands.rs:610` embedding raw stderr in `GitError::RebaseConflict`). Targeting the prune call sites keeps that path intact while routing prune errors through `print_command_error`'s chain branch — context as header, git stderr in the gutter. Adds a regression test that constructs the post-context anyhow shape and verifies `print_command_error` doesn't panic. Co-Authored-By: Claude <[email protected]>
worktrunk-bot
approved these changes
May 4, 2026
2 of 3 tasks
max-sixty
added a commit
that referenced
this pull request
May 4, 2026
…ailures (#2580) ## Summary Builds on #2567 by replacing the leaf bail at the buffered command boundary with a typed error. `Repository::run_command` and `WorkingTree::run_command` now return `Err(CommandError { program, args, stderr, stdout, exit_code })` instead of `bail!(\"{stderr}\")`. The structured form lets the renderer separate a single-line summary (`Display`) from the multi-line captured payload (`combined_output()`), and lets callers that embed raw stderr in higher-level errors (`RebaseConflict`, `WorktreeRemovalFailed`, `PushFailed`, list-task errors) read it directly instead of round-tripping through `e.to_string()`. The `debug_assert!` that triggered #2564 in debug builds is now a meaningful invariant: it flags only multi-line bails that aren't typed as `CommandError`, instead of acting as a proxy for \"developer remembered `.context(...)`.\" ### What's in the diff - `worktrunk::git::CommandError` (new) — typed leaf with `combined_output()` and a single-line `Display`. - `worktrunk::git::find_command_error` / `display_message` — chain walkers for renderers and embedders. - `print_command_error` refactored to `format_command_error -> String` (testable) with a downcast branch that preserves intermediate `.context(...)` entries above the captured body. - `Repository::extract_failed_command` extended to recognize `CommandError` alongside `StreamCommandError`. - 5 user-facing serialization sites migrated to `display_message` so wrappers like `WorktreeRemovalFailed.error` and `PushFailed.error` keep showing git's real stderr (rather than the new single-line summary). ### Reviewer orientation - `src/git/error.rs` — `CommandError`, `find_command_error`, `display_message`. - `src/git/repository/mod.rs`, `src/git/repository/working_tree.rs` — leaf cutover. - `src/main.rs` — renderer; the new branch and the trim-then-anstream `print_command_error` were both flagged in iteration. - `src/commands/{step_commands,worktree/push,list/collect/{mod,tasks}}.rs`, `src/output/handlers.rs` — call-site migrations. ### Notes - `.context(...)` calls added in #2567 are no longer load-bearing for correctness — they remain as semantic UX. Pruning the mechanical ones is a follow-up. - Method is `combined_output()` rather than `output()` because the `no-direct-cmd-output` pre-commit hook regex flags `.output()` to keep callers off `std::process::Command::output`. ## Test plan - [x] `cargo run -- hook pre-merge --yes` — 3480 tests pass, all lints, all docs - [x] Codex iteration: P2 (preserve raw stderr in typed wrappers) and P3 (preserve context chain in renderer) addressed; tests added for both - [ ] CI: linux/macos/windows test jobs green > _This was written by Claude Code on behalf of @max-sixty_ --------- Co-authored-by: Claude <[email protected]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The Windows flake of `test_prune_skips_unmerged` (#2564) revealed a latent reporting bug: when git failed to read `.git/config` mid-prune, the multi-line stderr propagated up as a top-level anyhow message with an empty chain — exactly the case `print_command_error`'s `debug_assert!(false, "Multiline error without context")` is designed to nag on. Debug builds (including `cargo test`) exited 101 instead of rendering the error.
The offending call sites are in `step_prune`'s foreground path: `capture_refs()`, `list_worktrees()`, `all_branches()`, `integration_reason` channel results, the `is_linked`/`git_dir`/`root` probes, and `try_remove`. Each used `?` without a `.context(...)` wrapper, so any failing git subprocess inside them surfaced as a bare multi-line bail.
Wrapping at the helper (`Repository::run_command` itself) would silence the assert globally but breaks callers that read `e.to_string()` to extract raw git stderr — e.g. `step_commands.rs:610` embeds the rebase output in `GitError::RebaseConflict { git_output }`. That change regressed `merge::test_merge_rebase_conflict` and the `list::test_list_*` snapshots when I tried it. Targeted call-site context keeps the structured paths intact while routing prune errors through `print_command_error`'s chain branch — context as header, git stderr in the gutter.
The Windows ACL race (#2564) is left to that issue — this PR is just the reporting fix.
Test plan