Skip to content

fix: honor redirects on command -v and command -V - #346

Merged
ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/command-v-redirects
Nov 26, 2026
Merged

ferdingler merged 2 commits into
strands-agents:mainfrom
ferdingler:fix/command-v-redirects

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 21, 2026 •

Copy link
Copy Markdown
Collaborator

Description

command -v and command -V ignored redirects on the same command. So the usual tool probe command -v ls >/dev/null 2>&1 still printed ls, and command -v cd > file printed to the terminal and never created the file. The exit status was right, so if command -v X branched correctly, but the extra output leaked into everything agents ran.

Now command -v/-V honor their redirects the same way eval and source already 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 -v in a pipeline (command -v ls | cat) still fails with "command not found". That's the same gap #323 tracks for command, so I left it out.

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Checklist

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.

@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent Please review this PR

@strandly-the-agent strandly-the-agent left a comment •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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/null now takes the wrong branch. The shell can't apply >&file: src/exec.rs:1772-1774 reads the word after >& as an fd number (target.parse().map_err(|_| format!("{target}: bad file descriptor"))). So the command now fails with status 1, as type X >&/dev/null already 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 shared run_redirected helper (src/exec.rs:3334-3339), the same for eval, source and 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 in shell_integration; differential ok). cargo fmt --check clean. cargo clippy --workspace --all-targets exits 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 -c and the MCP shell tool, compared with bash. These hold: >, >>, >|, 2>&1, >&2, both orders of 1>&2 2>/dev/null, <, <>, here-docs, fds 3-9, >&-; failed redirects (status 1, message, script continues); set -e; set -u in args or target; functions, subshells, groups, $(…), loops piped to cat; stdin intact afterwards; 100k iterations with flat memory and fd count.
  • command -v/-V without a redirect: 21 scripts byte-identical between base and head.
  • Writer mode: if command -v cd >/dev/null 2>&1; then echo have; fi under expect_writers! gives the writer cd\nhave\n on 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 >$UNSET now exits 2, like other commands.

Appendix: non-blocking (4)

  • ⚪ tests/differential/cases/command_v_redirects.sh:11: command -v cd 3>&1 >/dev/null shows 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 variant Special::CommandLookup has the same name as the CommandLookup enum matched inside its own arm (src/exec.rs:3163-3182). Special::CommandV would read easier.
  • ⚪ Output through a redirect is capped at max_output (1 MiB), so command -v with about 350k names is truncated. Same limit eval and 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 $(…).

@ferdingler
ferdingler merged commit 80cc57d into strands-agents:main Nov 26, 2026
37 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] command -v ignores redirects, so command -v X >/dev/null still prints X

4 participants