mirror of
https://github.com/max-sixty/worktrunk.git
synced 2026-11-22 16:18:23 +08:00
Since #4400 rewrote the Nushell wrapper around `try … finally`, redirecting the stdout of a `wt` command that prints nothing there aborts the wrapper. `nu --no-config-file -c 'source init.nu; cd repo; wt switch --create y o> /dev/null; print $env.PWD'` prints `repo.y` on v0.80.0, but on main it fails with `nu::shell::cant_convert ... can't convert nothing to string` at the `try {`, stops the script, and loses the cd. Nushell 0.116 fails to convert a `try … finally` that evaluates to nothing under `o>` or `o+e>`. A plain `try`, `| ignore` and `e>` are unaffected. The wrapper's `try … finally` now evaluates to a record holding the exit code and captured stdout, and the wrapper returns stdout or propagates the failure after the block, as v0.80.0 did. A failure's stdout still prints inside the block, so a failing cd directive can't drop it, and the `finally` still applies the directive and removes the temp files on Ctrl-C. Returning `""` from the empty branch would also have avoided the error, but it prints a blank line when `wt switch` is the last command of a `nu -c` script. Verified with nu 0.116.0 against v0.80.0, main and this branch: `o> /dev/null`, `o> file`, `| ignore`, `e> /dev/null`, `o+e> /dev/null`, plain and captured (`let x = (wt switch …)`) calls, a stdout-producing command redirected and piped, a failing command (exit 1 with no source trace, with and without `o>`), and `--execute` under `o>` all match v0.80.0 on this branch. Main fails the `o> /dev/null`, `o> file`, `o+e>` and `--execute` under `o>` cases. An interactive PTY session runs `wt switch` with and without `o> /dev/null` and changes directory both times. The new `test_nu_wrapper_redirected_stdout_applies_cd` fails on main with the same `cant_convert` error and passes here. The nushell, shell-wrapper, init and foreground-job integration tests (291) pass with `--features shell-integration-tests`. > _This was written by Claude Code on behalf of max-sixty_ --------- Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
162 lines
7.8 KiB
Nu
162 lines
7.8 KiB
Nu
# worktrunk shell integration for nushell 0.113+
|
|
|
|
# Tab completions: calls binary with COMPLETE=nu to get candidates.
|
|
# Note: nushell's completion engine bypasses custom completers when the current
|
|
# token starts with `-`, so flag completions (e.g. `wt switch --<TAB>`) don't
|
|
# appear. Subcommand and value completions work. (nushell/nushell#14504)
|
|
#
|
|
# Nushell 0.116 binds completer inputs by parameter name and hands `place` a
|
|
# record whose `command` field is the old spans list; a parameter still named
|
|
# `spans` works but warns as deprecated every time this file is sourced.
|
|
# Earlier versions pass the spans list positionally, whatever the name.
|
|
export def "nu-complete {{ cmd }}" [place] {
|
|
let spans = if ($place | describe | str starts-with "record") { $place.command } else { $place }
|
|
let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {
|
|
$env.WORKTRUNK_BIN
|
|
} else {
|
|
let external = (which -a {{ cmd }} | where type == "external")
|
|
if ($external | is-empty) { return [] }
|
|
($external | get 0.path)
|
|
}
|
|
|
|
let result = (do {
|
|
with-env { COMPLETE: nu } { ^$worktrunk_bin -- ...$spans }
|
|
} | complete)
|
|
if $result.exit_code != 0 { return [] }
|
|
|
|
$result.stdout | lines | each {|line|
|
|
let parts = ($line | split row "\t")
|
|
if ($parts | length) >= 2 {
|
|
{ value: ($parts | get 0), description: ($parts | get 1) }
|
|
} else {
|
|
{ value: ($parts | get 0) }
|
|
}
|
|
}
|
|
}
|
|
|
|
# Override {{ cmd }} so it can change the parent shell's directory.
|
|
# WORKTRUNK_BIN can override the binary path (for testing dev builds).
|
|
#
|
|
# Nushell pipeline workaround (nushell/nushell#12643):
|
|
#
|
|
# In POSIX shells, pipes are file-descriptor plumbing — stdout flows through
|
|
# the pipe while the shell continues executing post-command logic (directive
|
|
# processing, exit codes). In nushell, a function's "output" is its return
|
|
# value (the last expression), not bytes written to fd1. When an external
|
|
# command isn't the last expression, its stdout goes to the terminal as a
|
|
# side effect but doesn't flow through `|`.
|
|
#
|
|
# This means we can't both stream stdout through a pipe AND do post-processing
|
|
# (directives, cleanup) in the same function. We handle this by splitting into
|
|
# two paths based on the subcommand:
|
|
#
|
|
# - `list`: Direct passthrough. The binary is the last expression, so stdout
|
|
# streams to the terminal (progressive rendering) and flows through pipes
|
|
# (`{{ cmd }} list --format json | from json`). No directive processing — `list`
|
|
# never emits directives. No directive env vars are set.
|
|
#
|
|
# - Everything else: Stdout is normally redirected to a temp file via `o>`,
|
|
# then returned as the function's last expression after directive processing.
|
|
# `--execute` is the exception: wt's child needs the terminal, so stdout
|
|
# streams directly and is not available to an outer nushell pipeline.
|
|
#
|
|
# Completer wired via `@complete` attribute on an untyped `[...args]` rest.
|
|
# The parameter-level `string@"completer"` form keeps tokens literal, which
|
|
# blocks nushell's normal argument handling for `--flag="value"` (quote
|
|
# stripping) and `~` (path expansion). Untyped rest gets that handling once
|
|
# nushell/nushell#18131 ships in stable nu; until then `--flag="value"` still
|
|
# arrives quoted. See nushell/nushell#18128 for upstream tracking.
|
|
@complete "nu-complete {{ cmd }}"
|
|
export def --env --wrapped {{ cmd }} [...args] {
|
|
let worktrunk_bin = if ($env.WORKTRUNK_BIN? | is-not-empty) {
|
|
$env.WORKTRUNK_BIN
|
|
} else {
|
|
# Find the external binary, not the custom function
|
|
# `which -a` is needed because `which` alone only returns the custom command
|
|
let external = (which -a {{ cmd }} | where type == "external")
|
|
if ($external | is-empty) {
|
|
error make {
|
|
msg: "{{ cmd }} binary not found in PATH. Install with 'cargo install --path .' or set $env.WORKTRUNK_BIN"
|
|
}
|
|
}
|
|
($external | get 0.path)
|
|
}
|
|
|
|
# Unquoted wrapped arguments can be glob values. Classify their text
|
|
# while preserving the original arguments for external execution.
|
|
let arg_text = ($args | each {|arg| $arg | into string })
|
|
|
|
# `list` is the only command that benefits from streaming stdout (progressive
|
|
# table rendering). It never emits directives, so we skip directive processing
|
|
# and let the binary be the last expression — stdout flows through pipes.
|
|
# Note: global flags before `list` (e.g. `wt -C /path list`) miss this check
|
|
# and fall through to the buffered path. Output is still correct, just not
|
|
# progressively rendered.
|
|
if (not ($arg_text | is-empty)) and ($arg_text | first) == "list" {
|
|
# Direct passthrough: binary is the last expression of this branch,
|
|
# which is the last expression of the function. Stdout streams to the
|
|
# terminal and flows through nushell pipelines.
|
|
^$worktrunk_bin ...$args
|
|
} else {
|
|
let cd_file = (mktemp --tmpdir)
|
|
let stdout_file = (mktemp --tmpdir)
|
|
# The return value comes from outside `try … finally`: under an output
|
|
# redirect (`o> file`), Nushell fails converting a `try … finally`
|
|
# that evaluates to nothing ("can't convert nothing to string").
|
|
let result = (try {
|
|
let wt_args = ($arg_text | take until {|arg| $arg == "--" })
|
|
# Before `-x`, `c`, `v`, and `y` are boolean flags clap may cluster with it.
|
|
let has_execute = ($wt_args | any {|arg| $arg == "--execute" or ($arg | str starts-with "--execute=") or ($arg =~ '^-[cvy]*x') })
|
|
|
|
# Capture stdout for pipeline passthrough unless wt will launch an
|
|
# interactive child, which must inherit the terminal.
|
|
# Capture failure status without discarding buffered stdout.
|
|
# The outer `finally` applies directives and cleans up on Ctrl-C too.
|
|
let exit_code = (try {
|
|
with-env { WORKTRUNK_DIRECTIVE_CD_FILE: $cd_file } {
|
|
if $has_execute {
|
|
^$worktrunk_bin ...$args
|
|
} else {
|
|
^$worktrunk_bin ...$args o> $stdout_file
|
|
}
|
|
}
|
|
0
|
|
} catch {
|
|
# Nushell reports signal deaths as negative signal numbers. Its
|
|
# `exit` builtin needs the corresponding shell status instead.
|
|
let code = $env.LAST_EXIT_CODE
|
|
if $code < 0 { 128 - $code } else { $code }
|
|
})
|
|
|
|
let output = if $has_execute { "" } else { open $stdout_file --raw }
|
|
# A failure's stdout prints now, so a failing directive can't drop it.
|
|
if $exit_code != 0 and ($output | is-not-empty) { print -n $output }
|
|
{ exit_code: $exit_code, output: $output }
|
|
} finally {
|
|
try {
|
|
# cd file holds a raw path — no shell parsing needed
|
|
if ($cd_file | path exists) and (open $cd_file --raw | str trim | is-not-empty) {
|
|
let target_dir = open $cd_file --raw | str trim
|
|
cd $target_dir
|
|
}
|
|
} finally {
|
|
# Bypass user rm aliases on Unix; Windows uses Nushell's builtin.
|
|
# Cleanup cannot replace the command's status.
|
|
if $nu.os-info.family == "windows" {
|
|
try { rm -f $cd_file $stdout_file }
|
|
} else {
|
|
try { ^rm -f $cd_file $stdout_file }
|
|
}
|
|
}
|
|
})
|
|
|
|
# Return captured stdout or reproduce the external failure without
|
|
# a source trace. Nu itself also works on Windows, where sh is absent.
|
|
if $result.exit_code != 0 {
|
|
^$nu.current-exe --no-config-file --no-std-lib -c $"exit ($result.exit_code)"
|
|
} else if ($result.output | is-not-empty) {
|
|
$result.output
|
|
}
|
|
}
|
|
}
|