Repository navigation
Conversation
|
@strandly-the-agent review this change |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Changes requested: one blocker. The fix is correct for a literal "" and matches bash, but merged alone it breaks [ "$1" = "" ] when $1 is unset, because a quoted unset variable is still dropped. The inline comment has the repro and a tested two-line fix. @ferdingler
| Pass | Result |
|---|---|
| Correctness | No defect in the three changed lines; "$@" with no arguments still gives zero words |
| Adversarial | One regression: [ "$unset" = "" ] goes from true to false, and panics with the operands swapped |
| Tests | cargo test --workspace --all-targets passes on fc61f95; the new .expected matches bash 5.2 |
What I verified
- Reviewed head
fc61f95against base62a62d1, debug builds on Linux aarch64, compared with bash 5.2.15. - The #299 repro fails on base and passes on head.
cargo test --workspace --all-targetson head: 2654 passed, 0 failed (includes the 114 differential cases). I did not run the Python or Node suites.- Running
quote_empty_args.shunder bash reproduces the committed.expectedexactly. - Held against bash on head:
"$@",""$@,"$@""","$@""$@"with no arguments;""as a command name (status 127);for v in "" a;case "" in "");$unset"". - The adversarial pass also ran about 6,500 seeded field-splitting probes on base, head and bash and found no other regression. I spot-checked its findings, not every probe.
Appendix: non-blocking (5)
- PR description: the
casepattern was never the gap.case "" in "")already matches on base;case_glob_and_fallthrough.shdiffered becausefor v in … ""dropped the empty word. Removing the marker is still right. - Coverage: no case puts
""next to an unquoted expansion. A line such asshow $empty""(bash:argc=1) would cover it. - Still differs from bash, same as base and same for
'':x="a "; show $x""gives one word where bash gives two. Not filed; it needs unusual input. - Pre-existing, filed #321:
[ x = ]panics insrc/builtins/test.rs:97. - Pre-existing, filed #322: an empty path operand is treated as the current directory. With this fix a literal
""now reaches commands, sotouch ""returns 0 where base reported a missing operand.
| if parts.is_empty() { | ||
| segments.last_mut().unwrap().push("", false, false); | ||
| } |
There was a problem hiding this comment.
[ "$1" = "" ] with $1 unset now evaluates false, loops forever, or panics
A quoted unset variable still expands to no word: the WordPart::Var arm at src/exec.rs:606-614 pushes nothing when resolve_var returns None. On base, [ "$1" = "" ] with no arguments collapsed to [ = ], which is true by accident. With this change the "" survives, so the test becomes [ = "" ]:
if [ "$1" = "" ]; then usage; fisilently skips the usage branch.until [ "$1" = "" ]; do …; shift; donenever ends.[ "" = "$v" ]withvunset panics ([BUG][/testpanics when a binary operator is the last argument ([ x = ]) #321).
bash and base get all three right. In fairness, this PR fixes the mirror case: [ "$set" = "" ] panics on base.
Suggestion: keep a quoted unset variable as an empty field as well, the rule the VarOp arm already applies at line 617.
if let Some(val) = resolve_var(proc, name) {
let splittable = !quoted;
segments.last_mut().unwrap().push(&val, splittable, false);
} else if quoted {
segments.last_mut().unwrap().push("", false, false);
}Reproduction details
Head fc61f95, base 62a62d1, debug builds, Linux aarch64, bash 5.2.15. Each script run as <shell> -c '<script>' with no arguments.
| Script | bash | base | head |
|---|---|---|---|
if [ "$1" = "" ]; then echo "usage: prog FILE"; exit 2; fi; echo "processing [$1]" |
usage: prog FILE, exit 2 |
same as bash | processing [], exit 0 |
until [ "$1" = "" ]; do echo "[$1]"; shift; done; echo done |
done |
same as bash | prints [] until killed by timeout 5 |
unset v; [ "" = "$v" ] && echo empty || echo nonempty |
empty |
same as bash | panic at src/builtins/test.rs:97, exit 101 |
[ "$HOME" = "" ] && echo empty || echo nonempty |
nonempty |
panic, exit 101 | nonempty |
Root cause on both base and head: f() { echo $#; }; unset u; f x "$u" y prints 2; bash prints 3.
With the suggested change applied to head:
- The first three scripts match bash, and
f x "$u" yprints3. cargo test --workspace --all-targets: 2654 passed, 0 failed.set -ustill reports the unset variable, and"$@"with no arguments still gives zero words.- One side effect:
"$u$@"withuunset and no arguments becomes one word where bash gives zero. Set-but-empty"$e$@"already does this on base.
A differential line that would have caught this (bash prints argc=2, <>, <x>, unset-equals-empty):
unset u
show "$u" x
[ "$u" = "" ] && echo unset-equals-emptyThere was a problem hiding this comment.
Fixed in 1c9a197. Added Bash comparison tests for the unset $1 comparison and for quoted unset variables alongside "$@" when there are no positional arguments.
There was a problem hiding this comment.
Verified on f49041e: all three scripts match bash, and "$u$@" with no arguments stays at zero words, which my one-line version got wrong. This thread can be resolved; I don't have permission to resolve it myself.
|
@strandly-the-agent re-review |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Previous blocker is fixed. One new blocker, and it is merge order, not this diff. Keeping quoted unset variables is correct, but it lets rm -rf "$unset" reach the empty-path bug in #322, where it deletes root-level directories. On main that command fails safely. I'd fix #322 first, then merge this unchanged. @ferdingler
This exposure comes from the fix I suggested, and last time I listed #322 as non-blocking without testing rm. That was an underestimate.
| Item | Status |
|---|---|
Previous finding: [ "$1" = "" ] with $1 unset |
Fixed in 1c9a197; all three repros now match bash |
New: rm -rf "$unset" and mv "$unset" dst |
Blocker until #322 is fixed (inline comment) |
| Tests | cargo test --workspace --all-targets passes on f49041e; CI has not run on this head, it is waiting for approval |
What I verified
- Reviewed head
f49041eagainstmain@c28e1fb, debug builds on Linux aarch64, compared with bash 5.2.15. cargo test --workspace --all-targetson head: 2690 passed, 0 failed (includes the 119 differential cases). I did not run the Python or Node suites.- Running the updated
quote_empty_args.shunder bash reproduces the committed.expectedexactly. - 130 expansion probes on bash,
mainand head: no case wheremainmatched bash and head does not. Head differs from bash in 10, andmaindiffers in the same 10. They cover quoted unset variables alone, next to literals, globs and unquoted expansions, inside${x:-…},$(…),eval,for,case, functions, and combined with"$@"and"$*"for zero, one and several arguments. - The two-flag approach handles
"$u$@","$@$u"and"$u$@$u"with no arguments (zero words, as bash), which the one-line version I suggested did not.
Appendix: non-blocking (2)
- Same on
main, contrived: with no arguments,"$u""$@"gives zero words where bash gives one, and"$e$@"witheset but empty gives one word where bash gives zero. Not filed. - Same on
main, not filed:set -u; show "$1"with no arguments does not report an unset parameter; bash exits. This PR only changes what the command receives, from no argument to an empty one.
| } else if quoted { | ||
| segments.last_mut().unwrap().has_quoted_unset = true; |
There was a problem hiding this comment.
rm -rf "$unset" now deletes root-level directories, through #322
This line is correct and matches bash. The problem is what it now feeds. Commands treat an empty path as the current directory (#322), and before this commit an unset variable never reached them. With u unset:
rm -rf "$u"in a directory that containstmp/empties/tmp.rmlists the current directory, then removes/<name>for each entry, becausesrc/commands/rm.rs:46builds the child path as"" + "/" + name.mainreportsmissing operand; bash does nothing.mv "$u" ../xrenames the current directory and exits 0.mainand bash both fail.
main already does both for a variable that is set but empty, so the defect is #322 and not this diff. Unset is the common case, though (rm -rf "$BUILD_DIR").
Suggestion: land a fix for #322 first, then merge this as it is. Rejecting an empty path where vfs::absolute (src/vfs.rs:747) joins it to the cwd looks like the place; I have not built or tested that.
Reproduction details
Head f49041e, main @ c28e1fb, debug builds, Linux aarch64, default virtual filesystem.
strands-shell -c 'mkdir -p /tmp/keep; touch /tmp/keep/file; mkdir -p proj/tmp; cd proj; unset e; rm -rf "$e"; echo "rc=$?"; echo "/tmp: $(ls /tmp)"'| Build | Variable | Output |
|---|---|---|
main |
unset e |
rm: missing operand, rc=1, /tmp: keep |
| head | unset e |
rm: /tmp: permission denied, rc=1, /tmp: (empty) |
main |
e= |
same as head: /tmp emptied |
bash, same steps with relative paths in a scratch directory: rc=0, nothing removed.
strands-shell -c 'mkdir -p w/d; cd w; touch g; unset u; mv "$u" ../x; echo "rc=$?"; cd ..; ls'main: mv: missing operand, rc=1, w. Head: rc=0, x. bash: mv: cannot stat '', rc=1, w.
There was a problem hiding this comment.
@ferdingler, I can take #322 in a separate PR so it can land before this one. Does that sound good?
|
Hey apologies but this work got picked up elsewhere. I should have assigned the ticket to myself earlier. |
Description
Shell expansion dropped literal empty double-quoted arguments and quoted unset variables. Preserve the empty argument in both cases so comparisons such as
[ "$1" = "" ]work when no argument is supplied. When"$@"has no arguments, it still contributes no fields, including when it appears in the same word as a quoted unset variable.The Bash comparison case covers argument counts and values, missing positional parameters, unset variables, adjacent quotes and argument forwarding. The empty loop value in the existing
casefixture now reaches the pattern match, so both resolved known-difference markers are removed.Related Issues
Fixes #299
Documentation PR
Not needed; this restores existing behavior.
Type of Change
Bug fix
Testing
cargo test --workspace --all-targets,pytest tests/python,npm test)cargo fmtandcargo clippyValidated on macOS with the full Rust check, workspace doctests, and Node-feature Rust unit tests. Recorded the expanded regression fixture from Bash. Existing lint warnings remain.
Checklist
Documentation changes are not needed: this restores existing shell behavior.
By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the terms of your choice.