Files
worktrunk/templates/fish.fish
Maximilian Roos cd6732e93a fix: preserve native foreground job control (#4400)
Foreground hooks could hang when they changed terminal settings:
Worktrunk created a separate process group without giving it the
terminal, so the kernel stopped the child with SIGTTOU. Foreground
commands now stay in the caller's native job group, consistently for
single and concurrent commands. Terminal access, Ctrl-C and job
suspension follow the shell's native behavior.

### Design

- `signal_forwarder` owns foreground cancellation and child admission.
Native SIGINT/SIGTERM deaths stop later hooks and worktree steps; a
child that catches Ctrl-C keeps its ordinary exit semantics, including
exit code 130. SIGTERM addressed to wt cancels its owned work even when
a child catches it.
- `shell_exec` and concurrent output share that lifecycle. Output drains
stop on cancellation; complete buffered stdin uses an anonymous
temporary file instead of writer threads. Real process pipelines remain
pipes. Buffered stdin therefore requires temporary storage and is
seekable.
- Help and preview pagers use Git's pager selection and the same process
primitives. Interactive pager cancellation propagates silently, and
preview deadlines cover output and child waits.
- Shell wrappers apply directory directives and clean temporary files
while preserving native interrupt behavior. Nushell integration now
requires 0.113 or newer and uses native `finally` cleanup.

Nushell 0.116's `finally` itself can abort a noninteractive script after
Ctrl-C even when the child catches it and exits successfully.
Worktrunk's directory change and cleanup still complete; the same
script-continuation difference reproduces in a minimal Nushell
`try/finally` without Worktrunk.

Bash applies aborted-line cleanup through a writable `PROMPT_COMMAND`.
With a readonly prompt command or `set -a`, it applies ordinary cleanup
while preserving that configuration; Ctrl-C can leave the pending
directory directive unapplied.

The main behavioral coverage uses real children and PTYs: terminal
ownership/settings, raw and caught Ctrl-C, PID-targeted cancellation,
stopped jobs, concurrent output, pagers, and generated shell wrappers.
Module contracts describe ownership and platform differences.

### Validation

The complete project pre-merge gate passes locally, including the
real-shell/PTY suite, Clippy, formatting, locked dependencies, and
documentation checks. The change also received an independent Opus
correctness and simplicity review, with fixes reviewed through
convergence. macOS runtime validation is local; Linux and Windows run in
CI.

Closes #4389. Thanks to @user753 for the reproduction.

> _This was written by Codex on behalf of max-sixty_
2026-10-08 16:31:37 -07:00

68 lines
2.6 KiB
Fish

# worktrunk shell integration for fish
#
# This is the full function definition, output by `{{ cmd }} config shell init fish`.
# It's sourced at runtime by the wrapper in ~/.config/fish/functions/{{ cmd }}.fish.
# Override {{ cmd }} command so it can change the parent shell's directory.
# WORKTRUNK_BIN can override the binary path (for testing dev builds).
function {{ cmd }}
set -l use_source false
set -l args
for arg in $argv
if test "$arg" = "--source"; set use_source true; else; set -a args $arg; end
end
test -n "$WORKTRUNK_BIN"; or set -l WORKTRUNK_BIN (type -P {{ cmd }} 2>/dev/null)
if test -z "$WORKTRUNK_BIN"
echo "{{ cmd }}: command not found" >&2
return 127
end
set -l cd_file (mktemp)
# Older fish unwinds only the function that directly launched an interrupted
# child. This boundary lets ordinary cleanup run before its caller continues.
# Newer fish unwinds the whole input line; postexec owns cleanup in that case.
set -l cleanup_function _{{ cmd }}_cleanup_(string replace -a / _ -- "$cd_file")
set -l run_function "$cleanup_function"_run
function $run_function
command $argv
end
function $cleanup_function --on-event fish_postexec --inherit-variable cd_file --inherit-variable cleanup_function --inherit-variable run_function
set -l cd_exit 0
# cd file holds a raw path — read with fish builtin (no cat subprocess,
# safe even if CWD was removed by worktree removal).
if test -s "$cd_file"
set -l target (string trim < "$cd_file")
# `builtin cd` bypasses any user `cd` override (e.g. the zoxide.fish
# plugin replaces `cd` with a query function that mishandles the `--`
# separator). Matches the bash/zsh wrappers. (#3159)
builtin cd -- "$target"
set cd_exit $status
end
command rm -f "$cd_file"
functions -e $cleanup_function $run_function
return $cd_exit
end
# --source: use cargo run (builds from source)
if test $use_source = true
$run_function env WORKTRUNK_DIRECTIVE_CD_FILE=$cd_file \
cargo run --bin {{ cmd }} --quiet -- $args
else
$run_function env WORKTRUNK_DIRECTIVE_CD_FILE=$cd_file \
$WORKTRUNK_BIN $args
end
set -l exit_code $status
$cleanup_function
set -l cd_exit $status
if test $exit_code -eq 0
set exit_code $cd_exit
end
return $exit_code
end
# Completions are in ~/.config/fish/completions/{{ cmd }}.fish (installed by `{{ cmd }} config shell install`)