Skip to content

Commit ba6feef

Browse files
chaliyclaude
andauthored
fix(interpreter): forward pipeline stdin to user-defined functions (#294)
## Summary - Set `self.pipeline_stdin` before executing function body, restore after - Same pattern already used for compound commands in pipelines (line ~2583) ## Test plan - [x] `issue_274_pipeline_stdin_to_function` — `echo hello | to_upper` works - [x] `issue_274_pipeline_stdin_to_sourced_function` — sourced function receives stdin Closes #274 Co-authored-by: Claude <noreply@anthropic.com>
1 parent 2e06425 commit ba6feef

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

crates/bashkit/src/interpreter/mod.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3087,9 +3087,16 @@ impl Interpreter {
30873087
.collect();
30883088
let prev_funcname = self.arrays.insert("FUNCNAME".to_string(), funcname_arr);
30893089

3090+
// Forward pipeline stdin to function body
3091+
let prev_pipeline_stdin = self.pipeline_stdin.take();
3092+
self.pipeline_stdin = stdin;
3093+
30903094
// Execute function body
30913095
let mut result = self.execute_command(&func_def.body).await?;
30923096

3097+
// Restore previous pipeline stdin
3098+
self.pipeline_stdin = prev_pipeline_stdin;
3099+
30933100
// Pop call frame and function counter
30943101
self.call_stack.pop();
30953102
self.counters.pop_function();
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! Regression test for #274: Pipeline stdin not forwarded to user-defined functions
2+
3+
use bashkit::Bash;
4+
use std::path::Path;
5+
6+
#[tokio::test]
7+
async fn issue_274_pipeline_stdin_to_function() {
8+
let mut bash = Bash::new();
9+
let r = bash
10+
.exec("to_upper() { tr '[:lower:]' '[:upper:]'; }\necho hello | to_upper")
11+
.await
12+
.unwrap();
13+
assert_eq!(r.stdout.trim(), "HELLO");
14+
}
15+
16+
#[tokio::test]
17+
async fn issue_274_pipeline_stdin_to_sourced_function() {
18+
let mut bash = Bash::new();
19+
let fs = bash.fs();
20+
fs.write_file(
21+
Path::new("/lib.sh"),
22+
b"to_upper() { tr '[:lower:]' '[:upper:]'; }",
23+
)
24+
.await
25+
.unwrap();
26+
let r = bash
27+
.exec("source /lib.sh\necho hello world | to_upper")
28+
.await
29+
.unwrap();
30+
assert_eq!(r.stdout.trim(), "HELLO WORLD");
31+
}

0 commit comments

Comments
 (0)