From e4e5e64ab69b70b6728629ce313d940a3356c932 Mon Sep 17 00:00:00 2001 From: Mykhailo Chalyi Date: Thu, 2 Apr 2026 05:52:38 +0000 Subject: [PATCH] fix(builtins): produce empty JSON string for jq -Rs with empty stdin When -R (raw input) and -s (slurp) are combined, empty stdin should produce "" (empty JSON string), not no output. Removed early return that skipped output when input was empty. Closes #952 --- crates/bashkit/src/builtins/jq.rs | 10 +++++++++- crates/bashkit/tests/spec_cases/jq/jq.test.sh | 14 ++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/crates/bashkit/src/builtins/jq.rs b/crates/bashkit/src/builtins/jq.rs index b0a0528a..c8aabd0c 100644 --- a/crates/bashkit/src/builtins/jq.rs +++ b/crates/bashkit/src/builtins/jq.rs @@ -412,7 +412,8 @@ impl Builtin for Jq { }; // If no input and not null_input mode, return empty - if input.trim().is_empty() && !null_input { + // (but not for -Rs: raw_input+slurp should produce "" for empty stdin) + if input.trim().is_empty() && !null_input && !(raw_input && slurp) { return Ok(ExecResult::ok(String::new())); } @@ -1361,6 +1362,13 @@ mod tests { assert!(result.contains("1,2,3")); } + #[tokio::test] + async fn test_jq_raw_input_slurp_empty_stdin() { + // -Rs on empty stdin should produce "" (empty JSON string), not nothing + let result = run_jq_with_args(&["-Rs", "."], "").await.unwrap(); + assert_eq!(result.trim(), "\"\""); + } + // --- Process env pollution tests (issue #410) --- #[tokio::test] diff --git a/crates/bashkit/tests/spec_cases/jq/jq.test.sh b/crates/bashkit/tests/spec_cases/jq/jq.test.sh index 2795cdd4..57688530 100644 --- a/crates/bashkit/tests/spec_cases/jq/jq.test.sh +++ b/crates/bashkit/tests/spec_cases/jq/jq.test.sh @@ -1000,3 +1000,17 @@ printf 'a,b\n1,2\n' | jq -Rs 'split("\n") | map(select(length>0))' "1,2" ] ### end + +### jq_raw_slurp_empty_stdin +# jq -Rs on empty stdin should produce empty JSON string +printf '' | jq -Rs '.' +### expect +"" +### end + +### jq_raw_slurp_normal +# jq -Rs on normal input (no regression) +printf 'hello' | jq -Rs '.' +### expect +"hello" +### end