Skip to content

Commit 9a2caf2

Browse files
authored
fix(builtins): produce empty JSON string for jq -Rs with empty stdin (#971)
## Summary - Fix `jq -Rs '.'` producing no output on empty stdin instead of `""` - Root cause: early return when `input.trim().is_empty()` fired before the `-Rs` code path could create an empty string value - Added `&& !(raw_input && slurp)` guard so `-Rs` with empty stdin proceeds to create `Val::from("")` ## Test plan - [x] New spec test: `jq_raw_slurp_empty_stdin` — verifies `printf '' | jq -Rs '.'` outputs `""` - [x] New spec test: `jq_raw_slurp_normal` — regression test for non-empty input - [x] All 121 existing jq spec tests pass - [x] Full spec test suite green Closes #952
1 parent 7bfefd2 commit 9a2caf2

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-1
lines changed

crates/bashkit/src/builtins/jq.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,8 @@ impl Builtin for Jq {
412412
};
413413

414414
// If no input and not null_input mode, return empty
415-
if input.trim().is_empty() && !null_input {
415+
// (but not for -Rs: raw_input+slurp should produce "" for empty stdin)
416+
if input.trim().is_empty() && !null_input && !(raw_input && slurp) {
416417
return Ok(ExecResult::ok(String::new()));
417418
}
418419

@@ -1361,6 +1362,13 @@ mod tests {
13611362
assert!(result.contains("1,2,3"));
13621363
}
13631364

1365+
#[tokio::test]
1366+
async fn test_jq_raw_input_slurp_empty_stdin() {
1367+
// -Rs on empty stdin should produce "" (empty JSON string), not nothing
1368+
let result = run_jq_with_args(&["-Rs", "."], "").await.unwrap();
1369+
assert_eq!(result.trim(), "\"\"");
1370+
}
1371+
13641372
// --- Process env pollution tests (issue #410) ---
13651373

13661374
#[tokio::test]

crates/bashkit/tests/spec_cases/jq/jq.test.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,3 +1000,17 @@ printf 'a,b\n1,2\n' | jq -Rs 'split("\n") | map(select(length>0))'
10001000
"1,2"
10011001
]
10021002
### end
1003+
1004+
### jq_raw_slurp_empty_stdin
1005+
# jq -Rs on empty stdin should produce empty JSON string
1006+
printf '' | jq -Rs '.'
1007+
### expect
1008+
""
1009+
### end
1010+
1011+
### jq_raw_slurp_normal
1012+
# jq -Rs on normal input (no regression)
1013+
printf 'hello' | jq -Rs '.'
1014+
### expect
1015+
"hello"
1016+
### end

0 commit comments

Comments
 (0)