Repository navigation
fix(read): strip IFS whitespace around the value when a name is given - #345
ferdingler wants to merge 2 commits into
Conversation
|
@strandly-the-agent Please review this PR |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested: hold this until #353 lands. The trim matches bash 5.2.15 on everything probed, but on its own this branch makes while IFS= read -r line strip indentation, because the prefix IFS= doesn't reach the builtin yet (#308). With #353 merged in locally the idiom matches bash again.
| Pass | Result |
|---|---|
| Correctness, scope, tests | 🔴 1 (inline). The new case and its .expected are faithful to bash |
| Adversarial | Same 🔴, no other regression against main |
| Test suite | cargo test --workspace --all-targets passes on af4d30d (2,733 passed, 0 failed, 0 ignored) |
Small thing: the description cites #205 for the prefix-assignment bug. That one is #308 (#205 is the merged parser PR).
What I verified
- Head af4d30d against base dbb6189 on Debian 12 aarch64. Oracle: bash 5.2.15 run as
env -i PATH=/usr/bin:/bin LC_ALL=C bash -c '…'. Every probe was run on bash, a build ofmain, and a build of this branch. - Re-recorded
read_trims_ifs_whitespace.shfrom bash in an empty directory: status, stderr and stdout equal the committed.expected. This branch matches it,maindiffers on 7 of the 10 lines. - Matches bash on this branch: one and several names, more and fewer fields than names,
-r,--,-d '',-d ,, end of input before the delimiter,read REPLY, no name, andIFSunset, empty, space only, tab only, newline only (all set withIFS=…;). Non-breaking spaces are kept, as in bash. - An independent adversarial pass ran a matrix of whitespace shapes against reader forms plus randomized three-way probes. It found the inline regression and nothing else that
maingot right. - Combination: af4d30d merged with #353 (114b758) builds,
cargo test --test differentialpasses, andIFS= read -r x,IFS=: read x,IFS= read -r -d '' fandIFS= command read -r xmatch bash. - Not run:
pytest tests/pythonandnpm test. Not tested: macOS and Windows (CI is green there).
Appendix: non-blocking (5)
- One behavior changed to match bash but has no case:
printf ' a \\\n,' | { read -d , x; echo "[$x]"; }gives[a]on bash and this branch,[ a \]onmain. It would pin the trim running after the line-continuation step. - With #353 merged in,
IFS= eval 'read -r x'andIFS= f(a function that callsread) still trim ona b, where bash andmaingive[ a b ]. Those forms are outside what #353 covers, so they belong to #308. - CRLF input:
printf ' a b \r\n' | { read x; … }gives[a b]here,[ a b ]onmain, and[a b \r]on bash. This follows from the existing CR strip atsrc/builtins/read.rs:128-130, so I'm only noting that the value changes. printf 'a\\ \n' | { read x; … }: bash keeps the escaped space ([a ]), this branch gives[a\],maingives[a\ ]. It sits on the existingread_raw_backslashknown difference.- Pre-existing, filed #362:
readreturns 0 for a last line without a newline and keeps the old value at end of input, sowhile read -r x || [ -n "$x" ]never ends.
|
@strandly-the-agent re-review please |
|
@ferdingler There's nothing new to review yet. The head is still af4d30d, the commit I reviewed on 2026-11-21, and #353 is still open, so the blocker hasn't changed: merged alone, this branch still makes The description update is good: it notes the dependency and now cites #308 instead of #205. One small mismatch: the checklist ticks "Any dependent changes have been merged and published", but #353 hasn't merged yet. Once #353 lands and you rebase with the |
Description
When
readwas given a variable name, it kept the spaces and tabs around the value that bash strips. A single variable got the line untouched, and the last of several variables kept its trailing whitespace. So something like a count read from padded output wouldn't compare equal to the plain number.read -dhad the same problem with leading whitespace.Now
readstrips leading and trailing whitespace that's part ofIFSwhenever a name is given, with or without-d.REPLYand an emptyIFSstill keep the line as is, same as bash.Related Issues
Fixes #343
Depends on #353. Until that lands, a prefix
IFS=never reachesread, so with this change alonewhile IFS= read -r linewould lose indentation. This should merge after #353.Documentation PR
None
Type of Change
Bug fix
Testing
I added a differential case recorded from bash 5.2.15 covering one and several variables,
-d,REPLY, an emptyIFS, and anIFSwith only a space. It failed on main and passes with the change, and the full test suite passes. I usedIFS=...;beforereadinstead of a prefix assignment, because prefix assignments on builtins are a separate bug (#308).cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyChecklist
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.