Skip to content

Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows

Improve anyhow error formatting with context + gutter (#228)

Maximilian Roos43a1e790e8b3
diff --git a/src/main.rs b/src/main.rs
index 66aaadc31..7412ecd47 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -1193,7 +1193,7 @@ fn main() {
// If user declines, skip hooks but continue with worktree creation
let approved = if create && verify {
let repo = Repository::current();
- let repo_root = repo.worktree_base()?;
+ let repo_root = repo.worktree_base().context("Failed to switch worktree")?;
// Compute worktree path for template expansion in approval prompt
let worktree_path = compute_worktree_path(&repo, &branch, &config)?;
let ctx = CommandContext::new(
@@ -1237,7 +1237,7 @@ fn main() {
// Hooks only run if --no-hooks wasn't passed and approval was granted (or --force used)
if !skip_hooks && let SwitchResult::Created { path, .. } = &result {
let repo = Repository::current();
- let repo_root = repo.worktree_base()?;
+ let repo_root = repo.worktree_base().context("Failed to switch worktree")?;
let ctx = CommandContext::new(
&repo,
&config,
@@ -1285,9 +1285,10 @@ fn main() {
// Create context for template expansion in approval prompt
let worktree_path =
std::env::current_dir().context("Failed to get current directory")?;
- let repo_root = repo.worktree_base()?;
+ let repo_root = repo.worktree_base().context("Failed to remove worktree")?;
// Keep as Option so detached HEAD maps to None -> "HEAD" via branch_or_head()
- let current_branch = repo.current_branch()?;
+ let current_branch =
+ repo.current_branch().context("Failed to remove worktree")?;
let ctx = CommandContext::new(
&repo,
&config,
@@ -1311,7 +1312,8 @@ fn main() {
if worktrees.is_empty() {
// No worktrees specified, remove current worktree
// Uses path-based removal to handle detached HEAD state
- let result = handle_remove_current(!delete_branch, force_delete, background)?;
+ let result = handle_remove_current(!delete_branch, force_delete, background)
+ .context("Failed to remove worktree")?;
// Approval was handled at the gate
handle_remove_output(&result, None, background, verify)
} else {
@@ -1515,12 +1517,18 @@ fn main() {
} else if let Some(err) = e.downcast_ref::<worktrunk::git::HookErrorWithHint>() {
let _ = output::print(err.to_string());
} else {
- // Anyhow error - format with emoji, multi-line root cause gets gutter
+ // Anyhow error formatting:
+ // - With context: show context as header, root cause in gutter
+ // - Simple error: inline with emoji
let msg = e.to_string();
let root_cause = e.root_cause().to_string();
- let _ = output::print(cformat!("{ERROR_EMOJI} <red>{msg}</>"));
- if msg != root_cause && root_cause.contains('\n') {
+ if msg != root_cause {
+ // Has context: msg is context, root_cause is underlying error
+ let _ = output::print(error_message(&msg));
let _ = output::gutter(format_with_gutter(&root_cause, "", None));
+ } else {
+ // Simple error: inline with emoji
+ let _ = output::print(cformat!("{ERROR_EMOJI} <red>{msg}</>"));
}
}
diff --git a/tests/integration_tests/hook_show.rs b/tests/integration_tests/hook_show.rs
index 6cd5e255c..58b40ee49 100644
--- a/tests/integration_tests/hook_show.rs
+++ b/tests/integration_tests/hook_show.rs
@@ -153,6 +153,22 @@ test = "cargo test"
});
}
+/// Test error formatting with context (error message + gutter for multi-line)
+#[rstest]
+fn test_error_with_context_formatting(temp_home: TempDir) {
+ let temp_dir = tempfile::tempdir().unwrap();
+
+ // Run wt remove outside a git repo - should show "Failed to remove worktree" context
+ let settings = setup_home_snapshot_settings(&temp_home);
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ cmd.arg("remove").current_dir(temp_dir.path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+
+ assert_cmd_snapshot!(cmd);
+ });
+}
+
/// Test `wt hook show` outside git repo
#[rstest]
fn test_hook_show_outside_git_repo(temp_home: TempDir) {
diff --git a/tests/integration_tests/switch.rs b/tests/integration_tests/switch.rs
index 0c95ec734..dc0a0f7ae 100644
--- a/tests/integration_tests/switch.rs
+++ b/tests/integration_tests/switch.rs
@@ -1,9 +1,11 @@
use crate::common::{
- TestRepo, make_snapshot_cmd_with_global_flags, repo, repo_with_remote, setup_snapshot_settings,
+ TestRepo, make_snapshot_cmd_with_global_flags, repo, repo_with_remote, set_temp_home_env,
+ setup_home_snapshot_settings, setup_snapshot_settings, temp_home, wt_command,
};
use insta_cmd::assert_cmd_snapshot;
use rstest::rstest;
use std::path::Path;
+use tempfile::TempDir;
// Snapshot helpers
@@ -785,3 +787,24 @@ fn test_switch_execute_stdin_inheritance(repo: TestRepo) {
String::from_utf8_lossy(&output.stderr)
);
}
+
+// Error context tests
+
+/// Test `wt switch` outside git repo shows error with context
+#[rstest]
+fn test_switch_outside_git_repo(temp_home: TempDir) {
+ let temp_dir = tempfile::tempdir().unwrap();
+
+ // Run wt switch --create outside a git repo - should show "Failed to switch worktree" context
+ let settings = setup_home_snapshot_settings(&temp_home);
+ settings.bind(|| {
+ let mut cmd = wt_command();
+ cmd.arg("switch")
+ .arg("--create")
+ .arg("feature")
+ .current_dir(temp_dir.path());
+ set_temp_home_env(&mut cmd, temp_home.path());
+
+ assert_cmd_snapshot!(cmd);
+ });
+}
diff --git a/tests/snapshots/integration__integration_tests__hook_show__error_with_context_formatting.snap b/tests/snapshots/integration__integration_tests__hook_show__error_with_context_formatting.snap
new file mode 100644
index 000000000..5af2f15b0
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__hook_show__error_with_context_formatting.snap
@@ -0,0 +1,24 @@
+---
+source: tests/integration_tests/hook_show.rs
+info:
+ program: wt
+ args:
+ - remove
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ GIT_EDITOR: ""
+ HOME: "[TEST_HOME]"
+ SOURCE_DATE_EPOCH: "1735776000"
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: false
+exit_code: 1
+----- stdout -----
+
+----- stderr -----
+❌ Failed to remove worktree
+  fatal: not a git repository (or any of the parent directories): .git
diff --git a/tests/snapshots/integration__integration_tests__switch__switch_outside_git_repo.snap b/tests/snapshots/integration__integration_tests__switch__switch_outside_git_repo.snap
new file mode 100644
index 000000000..6d5931c9c
--- /dev/null
+++ b/tests/snapshots/integration__integration_tests__switch__switch_outside_git_repo.snap
@@ -0,0 +1,26 @@
+---
+source: tests/integration_tests/switch.rs
+info:
+ program: wt
+ args:
+ - switch
+ - "--create"
+ - feature
+ env:
+ APPDATA: "[TEST_CONFIG_HOME]"
+ CLICOLOR_FORCE: "1"
+ COLUMNS: "150"
+ GIT_EDITOR: ""
+ HOME: "[TEST_HOME]"
+ SOURCE_DATE_EPOCH: "1735776000"
+ USERPROFILE: "[TEST_HOME]"
+ WORKTRUNK_CONFIG_PATH: "[TEST_CONFIG]"
+ XDG_CONFIG_HOME: "[TEST_CONFIG_HOME]"
+---
+success: false
+exit_code: 1
+----- stdout -----
+
+----- stderr -----
+❌ Failed to switch worktree
+  fatal: not a git repository (or any of the parent directories): .git