Skip to content

fix: preserve literal empty double-quoted arguments - #320

Closed
ikenyal wants to merge 4 commits into
strands-agents:mainfrom
ikenyal:fix/preserve-empty-double-quoted-arguments
Closed

ikenyal wants to merge 4 commits into
strands-agents:mainfrom
ikenyal:fix/preserve-empty-double-quoted-arguments

Conversation

@ikenyal

@ikenyal ikenyal commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor

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 case fixture 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

  • I ran the relevant test suites for the bindings I touched (cargo test --workspace --all-targets, pytest tests/python, npm test)
  • If I touched Rust, I ran cargo fmt and cargo clippy

Validated 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

  • I have read the CONTRIBUTING document
  • I have reviewed and understand every line of code in this PR, including any generated by AI tools, and I can explain why it works
  • My change is focused and reasonably small; I have split unrelated work into separate PRs
  • I have added any necessary tests that prove my fix is effective or my feature works
  • I have updated the documentation accordingly
  • I have added an appropriate example to the documentation to outline the feature, or no new docs are needed
  • My changes generate no new warnings
  • Any dependent changes have been merged and published

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.

@github-actions github-actions Bot added the bug Something isn't working label Nov 18, 2026
@ikenyal
ikenyal marked this pull request as ready for review November 18, 2026 23:03
@ferdingler

Copy link
Copy Markdown
Collaborator

@strandly-the-agent review this change

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 fc61f95 against base 62a62d1, 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-targets on head: 2654 passed, 0 failed (includes the 114 differential cases). I did not run the Python or Node suites.
  • Running quote_empty_args.sh under bash reproduces the committed .expected exactly.
  • 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 case pattern was never the gap. case "" in "") already matches on base; case_glob_and_fallthrough.sh differed because for v in … "" dropped the empty word. Removing the marker is still right.
  • Coverage: no case puts "" next to an unquoted expansion. A line such as show $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 in src/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, so touch "" returns 0 where base reported a missing operand.

Comment thread src/exec.rs
Comment on lines +650 to +652
if parts.is_empty() {
segments.last_mut().unwrap().push("", false, false);
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[ "$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 [ = "" ]:

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" y prints 3.
  • cargo test --workspace --all-targets: 2654 passed, 0 failed.
  • set -u still reports the unset variable, and "$@" with no arguments still gives zero words.
  • One side effect: "$u$@" with u unset 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-empty

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 1c9a197. Added Bash comparison tests for the unset $1 comparison and for quoted unset variables alongside "$@" when there are no positional arguments.

@strandly-the-agent strandly-the-agent Nov 19, 2026 •

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 strandly-the-agent mentioned this pull request Nov 19, 2026
10 tasks done
@ferdingler

Copy link
Copy Markdown
Collaborator

@strandly-the-agent re-review

@strandly-the-agent strandly-the-agent left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 f49041e against main @ c28e1fb, debug builds on Linux aarch64, compared with bash 5.2.15.
  • cargo test --workspace --all-targets on 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.sh under bash reproduces the committed .expected exactly.
  • 130 expansion probes on bash, main and head: no case where main matched bash and head does not. Head differs from bash in 10, and main differs 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$@" with e set 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.

Comment thread src/exec.rs
Comment on lines +620 to +621
} else if quoted {
segments.last_mut().unwrap().has_quoted_unset = true;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 contains tmp/ empties /tmp. rm lists the current directory, then removes /<name> for each entry, because src/commands/rm.rs:46 builds the child path as "" + "/" + name. main reports missing operand; bash does nothing.
  • mv "$u" ../x renames the current directory and exits 0. main and 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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ferdingler, I can take #322 in a separate PR so it can land before this one. Does that sound good?

@pgrayy

pgrayy commented Nov 20, 2026

Copy link
Copy Markdown
Member

Hey apologies but this work got picked up elsewhere. I should have assigned the ticket to myself earlier.

@pgrayy pgrayy closed this Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] A double-quoted empty word ("") is dropped from a command's arguments

4 participants