Repository navigation
fix: honor redirects on command -v and command -V - #346
Conversation
|
@strandly-the-agent Please review this PR |
There was a problem hiding this comment.
Approve. The fix works: at e16b5fc both repros from #294 match bash, and they fail on dbb6189. No regressions found on forms the shell supports.
Two things to know, neither blocking:
- 🟡
command -v X >&/dev/nullnow takes the wrong branch. The shell can't apply>&file:src/exec.rs:1772-1774reads the word after>&as an fd number (target.parse().map_err(|_| format!("{target}: bad file descriptor"))). So the command now fails with status 1, astype X >&/dev/nullalready does. On main it ignored the redirect, printed the name and returned 0. The cause is that parse, not this PR; I added it to #369. - The redirect is still ignored when an embedder installed a stdout writer (
shell.proc.set_channel_writer). That is a gap in the sharedrun_redirectedhelper (src/exec.rs:3334-3339), the same foreval,sourceand function calls on main. Filed as #379 with failing tests. CLI and MCP are fine.
Reproduction details: >&file
Debug builds of base dbb6189 and head e16b5fc, run with strands-shell -c, compared with bash 5.2.15 on Linux aarch64.
if command -v cd >&/dev/null; then echo found; else echo missing; fi| Shell | stdout | stderr |
|---|---|---|
| bash | found |
empty |
base dbb6189 |
cd, found |
empty |
head e16b5fc |
missing |
strands-shell: /dev/null: bad file descriptor |
Control, same result on base and head: if type cd >&/dev/null; then echo t-found; else echo t-missing; fi prints t-missing with the same bad file descriptor message; bash prints t-found.
The code at src/exec.rs:1772-1774 (head):
let src_fd: u32 = target
.parse()
.map_err(|_| format!("{target}: bad file descriptor"))?;What I verified, and non-blocking notes
Verified at e16b5fc against base dbb6189 (Linux aarch64, bash 5.2.15)
cargo test --workspace --all-targets: 2733 passed, 0 failed, 0 ignored (2062 of them inshell_integration;differentialok).cargo fmt --checkclean.cargo clippy --workspace --all-targetsexits 0 with the same set of warnings as base, none new.- The new case run by hand: head and bash give identical output; base differs on stdout. Every line of it differs on base except
missing 1 []. - About 530 scripted cases and 950 random redirect combinations through
-cand the MCPshelltool, compared with bash. These hold:>,>>,>|,2>&1,>&2, both orders of1>&2 2>/dev/null,<,<>, here-docs, fds 3-9,>&-; failed redirects (status 1, message, script continues);set -e;set -uin args or target; functions, subshells, groups,$(…), loops piped tocat; stdin intact afterwards; 100k iterations with flat memory and fd count. command -v/-Vwithout a redirect: 21 scripts byte-identical between base and head.- Writer mode:
if command -v cd >/dev/null 2>&1; then echo have; fiunderexpect_writers!gives the writercd\nhave\non head. Tests and base results are in #379. - Not built or run: the Python, Node.js and WASM bindings.
Behavior that changed besides the fix
- A redirect that fails now gives status 1 and a message, where main ignored it. This matches bash for
> /nonexistent-dir/x. It differs from bash only for forms the shell can't apply anywhere:>&file(above),> /dev/stdout,2>/dev/stderr,< /dev/stdin,3>&1 1>&3-. set -u; command -v cd >$UNSETnow exits 2, like other commands.
Appendix: non-blocking (4)
- ⚪
tests/differential/cases/command_v_redirects.sh:11:command -v cd 3>&1 >/dev/nullshows nothing line 1 doesn't, because nothing writes to fd 3.command -v cd 3>f3 >&3; echo "[$(cat f3)]"prints[cd]on head and bash and[]on base. - ⚪ No case covers a failed redirect (
command -v cd > /nonexistent-dir/x; echo $?→ 1). It needs its own case file, since stderr presence is compared per case. - ⚪
src/exec.rs:3147: the variantSpecial::CommandLookuphas the same name as theCommandLookupenum matched inside its own arm (src/exec.rs:3163-3182).Special::CommandVwould read easier. - ⚪ Output through a redirect is capped at
max_output(1 MiB), socommand -vwith about 350k names is truncated. Same limitevaland functions already have.
Pre-existing, same on main, not filed: command -v a b returns 1 if any name is missing (bash: 0 if any is found); command -V nosuch prints nothing; command -v cd >> log 2>> log loses the output (so does echo); PATH=… command -v x ignores the prefix; the CLI drops stderr written inside $(…).
Description
command -vandcommand -Vignored redirects on the same command. So the usual tool probecommand -v ls >/dev/null 2>&1still printedls, andcommand -v cd > fileprinted to the terminal and never created the file. The exit status was right, soif command -v Xbranched correctly, but the extra output leaked into everything agents ran.Now
command -v/-Vhonor their redirects the same wayevalandsourcealready do. Small change in the executor, plus a test.Related Issues
Fixes #294
Documentation PR
None.
Type of Change
Bug fix
Testing
I added a differential case recorded against bash that covers
>/dev/null 2>&1, writing to a file, appending,-V, and a name that isn't found. On main it failed (the names went to stdout and the files were never written); with the change it passes, and the full test suite passes too.command -vin a pipeline (command -v ls | cat) still fails with "command not found". That's the same gap #323 tracks forcommand, so I left it out.cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.