Skip to content

fix: honor redirects on function calls - #226

Merged
ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/function-call-redirects
Nov 11, 2026
Merged

ferdingler merged 4 commits into
strands-agents:mainfrom
ferdingler:fix/function-call-redirects

Conversation

@ferdingler

@ferdingler ferdingler commented Nov 6, 2026 •

Copy link
Copy Markdown
Collaborator

Description

Redirects on a function call were ignored when the function ran as a plain command, so the output went to the terminal and the file was never created:

$ strands-shell -c 'f(){ echo x; }; f > a; cat a'
x
strands-shell: cat: no such file or directory: /home/lash/a

The same went for 2> file and < file. Inside a function, read failed because there was no stdin. Functions already worked in pipelines.

Now the call applies its redirects the same way eval and source already do, and the function still runs in the current shell, so cd, variables, return and exit behave as before. A function called without redirects takes exactly the same path as before. A redirected function's output is buffered until it returns, so with 2>&1 all of stdout comes before stderr, the same as { ...; } > file and eval today. Keeping the order is a follow-up. I left VAR=value f prefixes for a separate PR.

I compared each case against bash: >, >>, 2>, <, heredocs, 2>&1 >file, nested calls, return 3, a target that can't be opened (the function doesn't run, status 1) and set -e inside the function. The new tests fail on main and pass with the change. I also checked that this merges cleanly with #206.

Related Issues

Part of #207

Documentation PR

Type of Change

Bug fix

Testing

How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.

  • 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
ferdingler marked this pull request as ready for review November 9, 2026 23:52
@ferdingler
ferdingler requested a review from a team as a code owner November 9, 2026 23:52
@ferdingler

Copy link
Copy Markdown
Collaborator Author

@strandly-the-agent review

@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. Redirects on function calls now match bash for >, >>, 2>, <, heredocs, 2>&1 >f, return, set -e and unopenable targets, and eval/source behave exactly as before. Two 🟡 regressions come from routing functions through the buffering helper. Fix them here or track them with the ordering follow-up:

  • 🟡 Redirected function output now counts against max_output (1 MiB). Over it, the output is silently dropped or the call fails.
  • 🟡 Output from & jobs started inside a redirected function is lost unless the function waits.

Verified at 7abe01e: cargo test --workspace --all-targets passed 2079, 0 failed. CI is green. I couldn't run cargo fmt or clippy locally because the components weren't installed, so CI is the only check for those.

Details: what I verified, questions, appendix

✅ Verified

  • About 40 function-redirect cases, compared across main, this PR and bash, all match bash. That includes nested calls, cd/vars kept, local/$@ restored, break/continue, trap, recursion, $(f > o), and stdin in while read … done < in.
  • eval/source scripts give byte-identical output on main and this PR.
  • 11 of the 14 new tests fail on main. The other 3 are regression guards.

Questions (non-blocking)

  • Would it make sense to stream output to the redirect targets instead of buffering? That one change would fix both 🟡s and the 2>&1 ordering follow-up.
  • Edition 2024 allows F: AsyncFnOnce(&mut Process) -> Result<i32, ControlFlow> instead of the HRTB Pin<Box<dyn Future>> bound. Optional; I didn't compile it.

Reading order: src/exec.rs:2604 (run_redirected), then :3211 (the call site).

Appendix: non-blocking / pre-existing (5)

  • Pre-existing, from #196: write errors on the redirect target are dropped (exec.rs:2667, let _ = writer.write_all(…)). With max_file_size set, f >> g returns 0 and leaves the file unchanged. This was raised on #196 and is still open.
  • Pre-existing: exit inside a function doesn't exit the shell, with or without a redirect. #222 fixes that; if both merge, check the Exit arm at :3235.
  • Pre-existing: { cat big; } 2>/dev/null on its own, and eval … 2>/dev/null in a pipeline, also produce empty output on main. The { …; } case has the same root cause as the first 🟡.
  • ⚪ A body that reads its own redirect target doesn't see its own writes (f >> log where f runs tail log). eval does the same.
  • Out of scope: a redirect on a function definition (f(){ …; } > e) is still ignored, same as on main.

Comment thread src/exec.rs Resolved
Comment thread src/exec.rs Resolved
@ferdingler
ferdingler enabled auto-merge (squash) November 11, 2026 03:07
@ferdingler
ferdingler disabled auto-merge November 11, 2026 03:07
@ferdingler
ferdingler merged commit 4c16ba1 into strands-agents:main Nov 11, 2026
30 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants