From 75c26c266494f9dc2b4b5dd40d6b76f9b2245ccc Mon Sep 17 00:00:00 2001 From: zzzhizhi <77013105+zzzhizhia@users.noreply.github.com> Date: Sun, 15 Mar 2026 13:33:47 +0800 Subject: [PATCH 1/2] fix: handle `exec init` to prevent TUI on re-source When re-sourcing ~/.zshrc, the `try` shell function is already defined, so `try init` routes through the wrapper as `try exec init`. The exec handler didn't have a case for `init`, causing it to fall through to the TUI selector. Add `when 'init'` to the exec subcommand dispatch. --- spec/tests/test_03_commands.sh | 8 ++++++++ try.rb | 3 +++ 2 files changed, 11 insertions(+) diff --git a/spec/tests/test_03_commands.sh b/spec/tests/test_03_commands.sh index 36ce057..021d7b7 100644 --- a/spec/tests/test_03_commands.sh +++ b/spec/tests/test_03_commands.sh @@ -26,6 +26,14 @@ else fail "exec clone should include cd" "contains cd command" "$output" "command_line.md#clone" fi +# Test: exec init outputs shell function (regression: re-sourcing .zshrc) +output=$(try_run --path="$TEST_TRIES" exec init "$TEST_TRIES" 2>&1) +if echo "$output" | grep -q "try()"; then + pass +else + fail "exec init should output shell function" "contains 'try()'" "$output" "command_line.md#init" +fi + # Test: exec cd is equivalent to exec (default command) output1=$(try_run --path="$TEST_TRIES" --and-keys=$'\r' exec 2>/dev/null) output2=$(try_run --path="$TEST_TRIES" --and-keys=$'\r' exec cd 2>/dev/null) diff --git a/try.rb b/try.rb index d966f80..a26e6dd 100755 --- a/try.rb +++ b/try.rb @@ -1541,6 +1541,9 @@ def worktree_path(tries_path, repo_dir, custom_name) when 'clone' ARGV.shift emit_script(cmd_clone!(ARGV, tries_path)) + when 'init' + ARGV.shift + cmd_init!(ARGV, tries_path) when 'worktree' ARGV.shift repo = ARGV.shift From e0d1d53d7191c2d1c1c26a0c01c51cf8c5bd62a7 Mon Sep 17 00:00:00 2001 From: zzzhizhi <77013105+zzzhizhia@users.noreply.github.com> Date: Sun, 15 Mar 2026 19:00:35 +0800 Subject: [PATCH 2/2] fix: invoke script directly in init snippet instead of via `env ruby` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The init shell function used `/usr/bin/env ruby` to invoke the script, which could resolve to the wrong Ruby version (e.g. system Ruby 2.6 lacking Data.define support). Invoke the script path directly and let the shebang determine which Ruby to use. `script_path` is always `File.expand_path($0)` — the absolute path of the running script — so direct invocation via shebang is safe. Applied to all three shell snippets: bash/zsh, fish, and PowerShell. --- try.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/try.rb b/try.rb index a26e6dd..9b46547 100755 --- a/try.rb +++ b/try.rb @@ -1268,7 +1268,7 @@ def init_snippet(shell, script_path, explicit_path, default_path) fish_path_arg = explicit_path ? " --path '#{explicit_path}'" : " --path (if set -q TRY_PATH; echo \"$TRY_PATH\"; else; echo '#{default_path}'; end)" <<~FISH function try - set -l out (/usr/bin/env ruby '#{script_path}' exec#{fish_path_arg} $argv 2>/dev/tty | string collect) + set -l out ('#{script_path}' exec#{fish_path_arg} $argv 2>/dev/tty | string collect) if test $pipestatus[1] -eq 0 eval $out else @@ -1286,7 +1286,7 @@ def init_snippet(shell, script_path, explicit_path, default_path) function try { $tryPath = #{ps_path_expr} $tempErr = [System.IO.Path]::GetTempFileName() - $out = & ruby '#{script_path}' exec --path $tryPath @args 2>$tempErr + $out = & '#{script_path}' exec --path $tryPath @args 2>$tempErr if ($LASTEXITCODE -eq 0) { $out | Invoke-Expression } else { @@ -1301,7 +1301,7 @@ def init_snippet(shell, script_path, explicit_path, default_path) <<~SH try() { local out - out=$(/usr/bin/env ruby '#{script_path}' exec#{path_arg} "$@" 2>/dev/tty) + out=$('#{script_path}' exec#{path_arg} "$@" 2>/dev/tty) if [ $? -eq 0 ]; then eval "$out" else