Repository navigation
Conversation
|
Updated 6:50 AM PT - Oct 16th, 2026
❌ @robobun, your commit e5aa9b8 has 1 failures in
🧪 To try this PR locally: bunx bun-pr 44904That installs a local version of the PR into your bun-44904 --bun |
|
Reproduced on 1.4.3-canary.1+367d939d9 and on main, beside bash 5.2.37 and dash. args "$UNSET" x # Bun: <1>[x] bash, dash: <2>[][x]
args "$(true)" x # Bun: <1>[x] bash, dash: <2>[][x]
X=; args "$X" x # Bun: <1>[x] bash, dash: <2>[][x]The fix is this PR, #44904. State: this PR waits for #38002. Do not merge it before that. In build 126553 the only test that fails on each attempt is
#38002 makes both builtins reject an empty operand. After it merges, this branch needs a rebase. Both cells are expected to pass then. |
`transition_to_exec` was taking the early-exit path (exit 0, no error) when argv[0] was an empty string. After NUL-termination an empty string is a one-byte `[0u8]`, so the `a.len() > 1` guard lumped it in with the no-args case. The Zig reference only early-exits when args[0] is the Optional-null sentinel (i.e. args was empty); an actual empty-string word flows through to which() and reports "command not found" with exit 1, matching bash.
A word with a double-quoted expansion ("$VAR", "$N", "$(cmd)") that
expands to nothing was dropped from argv. bash and dash keep it as one
empty word.
The lexer now records on the Var and VarArgv tokens that they were inside
double quotes. The parser turns them into QuotedVar and QuotedVarArgv
atoms. Cmd keeps an empty word when its atom has a quoted expansion.
A leading tilde before an empty quoted expansion stays a literal tilde,
as in bash, and never becomes $HOME.
51e3593 to
360d3b0
Compare
…ange) This branch is parked. #45490 has the `$?` and `$$` part. `${NAME}` is #45032. The two commits below this one are the earlier, larger shape. A review of them found these items. Do them before this work is proposed. Commit 1 (expansions): - `param_text_braces` goes up in any quote state and comes down only in the state of the word. `echo ${S:-"${Y%z}"} mid; echo second` prints `set` and drops the rest. Count only where the `}` arm would close the word. - A word with no `}` must be a lexer error (`Unclosed parameter expansion`), as for `$(`. Delete the rollback, the `param_unclosed` memo and the `Eof` sentinel: 896 KB of unclosed heads lex in 5.4 s. - A quoted word must stay in `CharState::Double` up to its `}`. An inner `"` only flips a bit that decides if `}` closes. `\}` is a `}` there. Today `"${X:-"$(echo p q)"}"` gives 2 arguments (dash and bash: 1), `"${X:-"it's done"}"` stays text, and `"${X:-a\}b}"` gives `a\b}` (dash: `a}b`). - `Step::EndField` next to `~`, `{a,b}` or `*`: `~/.cache${S:+ --force}` gives `[/.cache][~--force]`, `{a,b}${X:-c }` gives an extra empty argument, and `echo *.js${S:+ --flag}` fails with `no matches found: --flag`. Check the trailing break before the brace and glob passes, and apply the leading tilde to the first field. - `${X:-"" b}` loses the empty argument: keep a quoted-empty flag for each field. - `SubShellKind::ParamWord` makes `eat_subshell` hold an unreachable arm (mordant `param_wider_than_callers`): keep the word context in its own lexer field. `append_var_argv -> bool` folds a `self_exe_path()` failure into "not set" (mordant `error_collapsed_to_bool`). - `ParamHead.skip` and the `open_params` stack of the parser cost 5 instructions for each `parse_atom` call. `Expansion::next` can find the matching `ParamEnd` with a depth counter. - `if !head.op.falls_back_to_value() { out.truncate(len) }` is dead. - `ends_in_param_ref` copies the head grammar of the lexer by hand: add a generated test that a plain interpolated string and a string reference give the same non-text tokens after `$` and `${` plus each tail of up to 2 bytes of `${}?:-+a1\#!@`. `$\FOO${"BAR"}` expands `$FOOBAR` on main too. - Tests to add: `-` and `+` with a set, an empty and an unset parameter and with argument numbers; nested forms where the used and the unused word differ; the word guards (`[[`, `]]`, `#`, a digit before `>`, a quoted `}`); the table again in a script with a non-ASCII byte; a checked-in dash reference table. - With `${NAME}`, an unset `"${X}"` gives no word: `[ -d "${X}" ]` passes and `cd "${X}"` goes to `$HOME`. #44904 makes it one empty word, so it must land first. - A prefix assignment is not seen: `A=x B=${A:-y} cmd` gives `y`. Commit 2 (parse errors): no maintainer asked for it. If it returns: one line for each error, a kind on `LexError`, and the hint about `--shell=system` printed by the caller only for a package.json script. It also rejects text that runs today: `"${{ x }}"`, `"$#"`, `sh -c "echo hi $@" -- a b`.
|
Closing as stale: this PR conflicts with main, and its last commit is from 2026-10-16. The branch stays, so the work is not lost. To pick it up again, rebase the branch on main and reopen this PR. |
Problem
cmd "$UNSET",cmd "$(true)". bash and dash pass one empty word. So[ -n "$UNSET" ]is true.Token::Varfor$Xand"$X"(src/shell_parser/parse.rs:2652). Only a literal""sets the flag thatCmd::child_donereads (src/runtime/shell/states/Cmd.rs:390).Fix
VarandVarArgvtokens. The parser buildsQuotedVarandQuotedVarArgvatoms.Cmd::child_donekeeps an empty word that has one.~"$UNSET"is a literal~, as in bash, never$HOME. An empty argv[0] printsbun: command not found:(the commit of shell: empty-string argv[0] should fail with "command not found" #32280, included here).test/js/bun/shell/fail on main. Also ran the whole directory.Background
ast::Atom, a list ofSimpleAtomparts.Cmd::child_donedrops an empty word that had no quotes. fix(shell): preserve empty string arguments in Bun shell #27231 kept the literal"",''and${""}.has_quoted_emptyfor a quoted expansion. Thenrm -rf ~"$UNSET"becomesrm -rf $HOME, as~""is$HOMEtoday.Downsides
cmd "$UNSET"passes"", as sh does.rimraf "$UNSET"(rimraf 4 and 5) then deletes the cwd, and"$UNSET" cmdfails withcommand not found.rmandcpbuiltins take""for the cwd: in CI,rm -rf "$UNSET"deletes a file in the cwd andcp -R "$UNSET" outcopies the cwd intoout. Those two cells fail on the Windows lanes until then."$(printf ' ')"becomes""(bash: one space). Cost per statement with no empty word: +4 to +7 instructions of about 7700, 0 allocations..text+2304 bytes.Notes
Matrix. 12 spellings, each through
Bun.$and throughbun exec, beside bash 5.2.37 and dash:"$X"(set to the empty string),"$UNSET","$(true)","`true`","$9","$A$B","$A"$B,$A"$B","$A""$B","$A$(true)","$(true)$(true)",$(true)"$(true)". This branch: 24 of 24 rows equal bash. main: 0 of 24.$UNSET,$(true),`true`and$UNSET$(true)without quotes: 8 of 8 rows still give no word. The value can come from an assignment, fromprocess.env, or from.env().What a script sees (release builds of main e655c58 and of this branch, Linux):
args "$UNSET" x[ -n "$UNSET" ]grep -c "$UNSET" filefile"$UNSET" cmdcmdcommand not found, exit 1command not found, exit 127"$(exit 3)"command not found, exit 1command not found, exit 127~"$UNSET"~~cd "$UNSET"$HOMEexport "$UNSET"rm -rf "$UNSET"rimraf "$UNSET"(rimraf 4, 5)The last row is the same with
bun run --shell=system, npm, and every sh. Bun's shell is the default for package.json scripts on Windows.Windows and #38002. With the
cpbuiltin (the default on Windows,BUN_ENABLE_EXPERIMENTAL_SHELL_BUILTINS=1elsewhere),cp -R "$UNSET" outprints the usage on main. On this branch it copies the cwd into itself until it crashes (exit 139 on Linux with the variable set). #38002 reports thatrm -rf ""deletes the files in the cwd on Windows. Both are reachable on main through""and${""}. Therm -rf "$UNSET_VAR"andcp -R "$UNSET_VAR" outcells have no Windows skip, so the Windows lanes show when the order is safe. On the Windows lanes of build 126553 both fail, as expected before #38002. The directory holdskeep.txtandsub\inner.txt. Afterrm -rf "$UNSET_VAR",keep.txtis deleted, the exit code is 1 and stderr isrm: \sub: Invalid argument. Aftercp -R "$UNSET_VAR" out, the directory has a newoutentry.Still different from bash. Four
todocells record what bash prints."$(printf ' ')"is""(bash: one space). A quoted command substitution loses its trailing blanks. main dropped the word, so[ -n "$(printf ' ')" ]goes from true to false (bash: true).a\<newline>"$UNSET"is two words,aand""(bash:a). A backslash-newline ends the word here. main dropped the second word.""$(printf ' ')""is one empty word (bash: two)."$UNSET"~and~"$UNSET"/expand to$HOMEand$HOME/, as on main (bash:~and~/).~""is$HOME(pinned bytest/regression/issue/17294.test.ts) and~$UNSETgives no word, as on main.{"$UNSET",a}loses the empty variant (shell: keep an empty brace expansion word in every position #40709, shell: emit one argv word per brace variant, including empties #30998).echo hi > "$UNSET"saysambiguous redirect. An unquoted$VARis not field split.Not in this PR. A double-quote pair that holds only a backslash-newline still gives no word (bash: one empty word). A first version kept it. It made
~$UNSET"\<newline>"expand to$HOME, and line continuations belong to #29670.Other PRs.
Expansion::child_doneand one comment line inCmd.rs. This PR touches neither. Itstodocell forX=$(echo -e '\0'); cmd a "$X" bpasses with this change.SimpleAtom::QuotedVaragainstVaris the quote bit that field splitting of an unquoted$VARneeds.Design.
has_quoted_emptykeeps its meaning (a literal""was seen). The new fact is static, soCmd::child_doneand the tilde arm read it from the word (Atom::has_quoted_expansion) only when the result is empty.QuotedVaris a variant and not aboolonVar: theboolmakesSimpleAtom32 bytes.Measured. Release builds of e655c58 and of this branch, same toolchain, built back to back. valgrind, perf, strace and bloaty are not on this machine.
Token12 B,SimpleAtom24 B,Atom32 B (const asserts),Lexer200 B. All as on main.shellInternals.lexandparse, 18 scripts, among themecho "$HOME" hi,echo a b c,true "$A" b $Candecho "$(echo hi)" x): +0 tokens, +0 atoms.bun_shell_parserandbun_runtime::shellfunctions (bun execof 800 and of 1600 statements under qemu-user with an exec trace, the slope, the same count on each repeat):echo a b c7696 to 7700,echo "$HOME" hi7697 to 7704,echo a $UNSET b9315 to 9351 (the word check runs),echo a "$UNSET" b9570 to 9683 (the kept word).mi_freefor the kept word. Native under gdb: 20.0 to 20.0 and 15.0 to 15.0 calls, 675.84 to 675.84 and 602.84 to 602.84 requested bytes.allocated_bytes(), because a test of the crate alone does not link here. Token count, atom count and the three sizes are the same.qemu -strace): 1 to 1..text+2304 B,.rodata+0 B, file +4096 B.SimpleAtom+2 variants,Token+0, host functions +0.Suites. Release builds: all of
test/js/bun/shell/andtest/regression/issue/17294.test.ts. This branch: 1013 pass, 2 fail. main: 981 pass, 34 fail. The 2 are thelspermission cells, which fail as root. The other 32 are the new and changed cells. Debug build:bunshell,lex,parse,exec,env.positionalsand17294pass.cargo checkforx86_64-pc-windows-msvcandaarch64-apple-darwinpasses.no test proof · iteration 0 · platform-specific test(s) that do not run on this machine, deferring to CI, which covers all platforms: test/js/bun/shell/bunshell.test.ts