Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 2 additions & 5 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,13 @@
version = builtins.replaceStrings ["\n"] [""] (builtins.readFile ./VERSION);

src = inputs.self;
nativeBuildInputs = [ pkgs.makeBinaryWrapper ];

installPhase = ''
mkdir -p $out/bin
cp try.rb $out/bin/try
cp -r lib $out/bin/
substituteInPlace $out/bin/try \
--replace-fail '#!/usr/bin/env ruby' '#!${ruby}/bin/ruby'
chmod +x $out/bin/try

wrapProgram $out/bin/try \
--prefix PATH : ${ruby}/bin
'';

meta = with pkgs.lib; {
Expand Down
5 changes: 3 additions & 2 deletions spec/init_spec.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ try() {

Key elements:
- Function name: `try`
- Calls the resolved `try` executable directly, relying on its shebang or package wrapper
- Captures `try exec` output to local variable
- Redirects stderr to `/dev/tty` (TUI renders to stderr)
- Exit code 0: Evaluates the output (executes cd, git clone, etc.)
Expand All @@ -56,10 +57,10 @@ end
## Path Embedding

The init output must embed:
1. The full path to the `try` binary (resolved at init time)
1. The full path to the `try` binary (resolved at init time; a canonical store path is acceptable when invoked through a symlink)
2. The default tries path (typically `~/src/tries`)

This ensures the wrapper always calls the correct binary regardless of `$PATH` changes.
This ensures the wrapper always calls the correct binary regardless of `$PATH` changes, while preserving executable shebangs and package-level launchers.

## Installation Instructions

Expand Down
14 changes: 11 additions & 3 deletions spec/tests/test_14_init_shells.sh
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,18 @@ else
fail "init with fish should emit fish function" "function try" "$output" "init_spec.md"
fi

# Test: init output contains the real, full path to try binary
# Test: init output contains a real, full path to the try binary
output=$(SHELL=/bin/bash try_run init "$TEST_TRIES" 2>&1)
if echo "$output" | grep -qF "$TRY_BIN_PATH"; then
TRY_BIN_REAL_PATH=$(realpath "$TRY_BIN_PATH" 2>/dev/null || readlink -f "$TRY_BIN_PATH" 2>/dev/null || echo "$TRY_BIN_PATH")
if echo "$output" | grep -qF "$TRY_BIN_PATH" || echo "$output" | grep -qF "$TRY_BIN_REAL_PATH"; then
pass
else
fail "init should contain real, full path to try binary" "$TRY_BIN_PATH" "$output" "init_spec.md"
fail "init should contain real, full path to try binary" "$TRY_BIN_PATH or $TRY_BIN_REAL_PATH" "$output" "init_spec.md"
fi

# Test: init output calls the binary wrapper directly, not the wrapped Ruby script
if echo "$output" | grep -qE "/usr/bin/env ruby|/ruby |\.try-wrapped"; then
fail "init should not bypass package wrappers" "no /usr/bin/env ruby and no .try-wrapped" "$output" "init_spec.md"
else
pass
fi
6 changes: 3 additions & 3 deletions try.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand All @@ -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
Expand Down