Worktrunk is a CLI for Git worktree management, designed for parallel AI agent workflows
Route errors through output system
Maximilian Roosd292f307aa40
diff --git a/src/main.rs b/src/main.rsindex a7e2863ef..ed3507cfd 100644--- a/src/main.rs+++ b/src/main.rs@@ -336,8 +336,10 @@ fn main() {if let Err(e) = result {// Error messages are already formatted with emoji and colors- // Per CLAUDE.md: worktrunk output (including errors) goes to stdout- println!("{}", e);+ // Route through output system to respect mode:+ // - Interactive mode: errors go to stdout+ // - Directive mode: errors go to stderr+ let _ = output::error(e.to_string());// Preserve exit code from child processes (especially for signals like SIGINT)let exit_code = match &e {diff --git a/src/output/directive.rs b/src/output/directive.rsindex 807db5183..ef0f13d64 100644--- a/src/output/directive.rs+++ b/src/output/directive.rs@@ -81,6 +81,12 @@ impl DirectiveOutput {io::stderr().flush()}+ pub fn error(&mut self, message: String) -> io::Result<()> {+ // Error messages go to stderr (already formatted, include ❌ emoji from GitError::Display)+ writeln!(io::stderr(), "{message}")?;+ io::stderr().flush()+ }+#[cfg(unix)]pub fn blank_line(&mut self) -> io::Result<()> {// Blank lines are only meaningful in interactive mode; no-op here to avoid empty directivesdiff --git a/src/output/global.rs b/src/output/global.rsindex 07896e105..7b97d32f9 100644--- a/src/output/global.rs+++ b/src/output/global.rs@@ -124,6 +124,23 @@ pub fn warning(message: impl Into<String>) -> io::Result<()> {})}+/// Emit an error message+///+/// Error messages are critical failures like "❌ Cannot remove main worktree"+/// The message is already formatted (includes ERROR_EMOJI from GitError::Display).+///+/// In interactive mode: goes to stdout (with other worktrunk output)+/// In directive mode: goes to stderr (with other user-facing messages)+pub fn error(message: impl Into<String>) -> io::Result<()> {+ OUTPUT_CONTEXT.with(|ctx| {+ let msg = message.into();+ match &mut *ctx.borrow_mut() {+ OutputHandler::Interactive(i) => i.error(msg),+ OutputHandler::Directive(d) => d.error(msg),+ }+ })+}+/// Insert a blank line between UI output and worktrunk messages#[cfg(unix)]pub fn blank_line() -> io::Result<()> {diff --git a/src/output/interactive.rs b/src/output/interactive.rsindex a671102da..d50ad385c 100644--- a/src/output/interactive.rs+++ b/src/output/interactive.rs@@ -57,6 +57,13 @@ impl InteractiveOutput {Ok(())}+ pub fn error(&mut self, message: String) -> io::Result<()> {+ // Error messages are already formatted (include ❌ emoji from GitError::Display)+ println!("{message}");+ stdout().flush()?;+ Ok(())+ }+#[cfg(unix)]pub fn blank_line(&mut self) -> io::Result<()> {// Ensure subsequent output starts on a fresh line after interactive UIs like skimdiff --git a/src/output/mod.rs b/src/output/mod.rsindex c038b43a8..dc0170f30 100644--- a/src/output/mod.rs+++ b/src/output/mod.rs@@ -62,8 +62,8 @@ pub mod interactive;// Re-export the public APIpub use global::{- OutputMode, change_directory, execute, flush, flush_for_stderr_prompt, gutter, hint, info,- initialize, progress, success, terminate_output, warning,+ OutputMode, change_directory, error, execute, flush, flush_for_stderr_prompt, gutter, hint,+ info, initialize, progress, success, terminate_output, warning,};// blank_line is only used by select command which is Unix-onlydiff --git a/tests/integration_tests/directives.rs b/tests/integration_tests/directives.rsindex c6f19c55b..fa683e6df 100644--- a/tests/integration_tests/directives.rs+++ b/tests/integration_tests/directives.rs@@ -27,11 +27,10 @@ fn test_switch_internal_directive() {success: falseexit_code: 1----- stdout ------ ❌ [31mFailed to create worktree for [1m[31mmy-feature[0m[0m- [40m [0m fatal: invalid reference: my-feature------ stderr -----+ ❌ [31mFailed to create worktree for [1m[31mmy-feature[0m[0m+ [40m [0m fatal: invalid reference: my-feature");});}@@ -88,12 +87,12 @@ fn test_remove_internal_directive() {success: falseexit_code: 1----- stdout ------ ❌ [31mCannot remove main worktree[0m-- 💡 [2mUse 'wt remove' from a secondary worktree, or 'wt switch' to change branches[0m----- stderr -----🔄 [36mRemoving worktree for [1m[36mmain[0m...[0m+ ❌ [31mCannot remove main worktree[0m++ 💡 [2mUse 'wt remove' from a secondary worktree, or 'wt switch' to change branches[0m");});}diff --git a/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snapindex 1587e3b9d..7ff0033a0 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_already_on_default.snap@@ -15,7 +15,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptjyi3G/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmptLKJdP/test-config.toml---success: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snapindex fc124c59e..dfb1a3d12 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_by_name_dirty_target.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpoL2sq2/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpY6EoeS/test-config.toml---success: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap b/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snapindex 349a656cb..524f832e8 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_dirty_working_tree.snap@@ -15,7 +15,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp9ZJ31J/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpIwpGmf/test-config.toml---success: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snapindex fea38ed82..2618ed88a 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_nonexistent_worktree.snap@@ -16,7 +16,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpD8gbZi/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmp98HYOs/test-config.toml---success: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap b/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snapindex c00f9c771..5421bd38f 100644--- a/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap+++ b/tests/snapshots/integration__integration_tests__remove__remove_switch_to_default.snap@@ -15,7 +15,7 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpDxn7qo/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpUF8Ywg/test-config.toml---success: falseexit_code: 1diff --git a/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap b/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snapindex 3aa08b698..f65b4db9f 100644--- a/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap+++ b/tests/snapshots/integration__integration_tests__security__branch_name_with_cd_directive_not_executed.snap@@ -18,9 +18,11 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpr1fhKK/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpg87zDD/test-config.toml---success: falseexit_code: 1----- stdout -----++----- stderr -----❌ [31mBranch [1m[31m__WORKTRUNK_CD__[PATH]diff --git a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snapindex cbd68cfde..d3c7c576d 100644--- a/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap+++ b/tests/snapshots/integration__integration_tests__security__error_message_with_directive_not_executed.snap@@ -17,13 +17,12 @@ info:LANG: CLC_ALL: CSOURCE_DATE_EPOCH: "1761609600"- WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpXCg30j/test-config.toml+ WORKTRUNK_CONFIG_PATH: /var/folders/wf/s6ycxvvs4ln8qsdbfx40hnc40000gn/T/.tmpLVz56t/test-config.toml---success: falseexit_code: 1----- stdout ------❌ [31mFailed to create worktree for [1m[31m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[0m[0m-[40m [0m fatal: invalid reference: __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6------ stderr -----+❌ [31mFailed to create worktree for [1m[31m__WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6[0m[0m+[40m [0m fatal: invalid reference: __WORKTRUNK_EXEC__echo PWNED > /tmp/hacked6