Repository navigation
Fix BuildPluginArgv dropping empty args in the shell fallback path - #219
Conversation
When a task's args contain an empty string alongside other args, BuildPluginArgv falls into its `sh -c` wrapper and joins args with a plain space. A bare empty element leaves no token for sh -c's own word-splitting to recover, so it silently vanishes and every later argument shifts one position to the left. Represent an empty element as a literal "" token before joining so it survives both rounds of shell re-parsing (the field-splitting layer and sh -c itself). A lone empty arg (len(args) == 1) is left untouched, matching the already-pinned no-op behavior for that case. Signed-off-by: Siddhardha Nanda <[email protected]>
There was a problem hiding this comment.
Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.
An organization owner can choose the repository organization as the billing source for reviews requested by members with a paid Copilot license. Learn about Copilot code review billing.
| // A naive space-join silently drops an empty-string element: joined | ||
| // into bare whitespace, it leaves no token for the shell that later | ||
| // parses the line (sh -c's own argument splitting) to recover, so | ||
| // the argument vanishes instead of becoming an empty positional | ||
| // parameter. Represent it as an explicit empty-string literal so it | ||
| // survives that re-parse. A lone empty arg (len(args) == 1) is left | ||
| // alone, since Join trivially returns it with no adjacent token to | ||
| // be swallowed by, matching the "single empty arg" case already | ||
| // pinned by TestBuildPluginArgv. |
There was a problem hiding this comment.
This comment is longer than the code it explains, and it justifies the len(args) > 1 guard by pointing at a test. comments should be reserved for non-obvious whys. The real reason for the guard is that sh -c '""' would try to run a command with an empty name. Something like:
| // A naive space-join silently drops an empty-string element: joined | |
| // into bare whitespace, it leaves no token for the shell that later | |
| // parses the line (sh -c's own argument splitting) to recover, so | |
| // the argument vanishes instead of becoming an empty positional | |
| // parameter. Represent it as an explicit empty-string literal so it | |
| // survives that re-parse. A lone empty arg (len(args) == 1) is left | |
| // alone, since Join trivially returns it with no adjacent token to | |
| // be swallowed by, matching the "single empty arg" case already | |
| // pinned by TestBuildPluginArgv. | |
| // Join renders an empty arg as bare whitespace, which sh -c's word | |
| // splitting drops. Spell it "" so it stays a positional parameter. | |
| // A lone empty arg is left as is: `sh -c '""'` would try to run a | |
| // command with an empty name. |
The change also belongs in the doc comment on L27–35 (outside this diff), because it changes the contract. Args are still shell text ("a b" splits into two words, " " disappears), and "" is now the one exception. For example:
// Args are shell text rather than literal argv: an arg containing
// whitespace splits into several words. An empty arg is the exception
// and is kept as an empty parameter, unless it is the only arg.There was a problem hiding this comment.
You're right, that comment was pointing at the wrong thing. I rewrote it to explain the empty-named-command problem instead of citing the test, and gave BuildPluginArgv's doc comment the shell-text contract you described too, since the guard it was justifying is gone now.
| // be swallowed by, matching the "single empty arg" case already | ||
| // pinned by TestBuildPluginArgv. | ||
| parts := args | ||
| if len(args) > 1 { |
There was a problem hiding this comment.
The guard makes the edge cases inconsistent. [""] is still a no-op, but ["", ""] now becomes sh -c '"" ""' and ["", "foo"] becomes sh -c '"" foo'. Both fail with "command not found", where before they ran nothing or foo.
Neither is reachable today (idetask rejects an empty Cmd at manager.go:476, and :! passes raw args through), so this isn't blocking. Either document it in the doc comment, or drop the guard and update the pinned single empty arg case so the rule is simply "every empty arg becomes """.
There was a problem hiding this comment.
I dropped the guard instead of documenting the asymmetry. Every empty arg gets spelled "" now regardless of position, so ["", ""] and ["", "foo"] get the same treatment as a lone [""]. Felt cleaner to remove the special case than to explain it.
| // TestBuildPluginArgvPreservesEmptyArgPosition reproduces a data-loss | ||
| // bug: an empty-string arg that shares the line with other args used | ||
| // to vanish instead of surviving as an empty positional parameter. | ||
| // | ||
| // The produced argv goes through two independent rounds of shell | ||
| // word-splitting before the target program ever runs: once when | ||
| // vte.Component.startCommand re-tokenises the joined argv (mirrored by | ||
| // the first shell.Fields call below), and again when `sh -c` parses | ||
| // its own script argument (mirrored by the second). A plain | ||
| // strings.Join of the args can only represent an empty element as | ||
| // bare whitespace, which the second round's IFS splitting collapses | ||
| // away, shifting every later argument one position to the left. |
There was a problem hiding this comment.
Please no comments narrating what a test accomplishes.
There was a problem hiding this comment.
I pulled the standalone test out and folded your exact example into the table below, as "consecutive empty args."
| // strings.Join of the args can only represent an empty element as | ||
| // bare whitespace, which the second round's IFS splitting collapses | ||
| // away, shifting every later argument one position to the left. | ||
| func TestBuildPluginArgvPreservesEmptyArgPosition(t *testing.T) { |
There was a problem hiding this comment.
We prefer table-driven tests and these test cases fit in TestBuildPluginArgv with exact expected output. I think this was split out because the round-trip check there (L215–222) compares against strings.Join(tc.args, " "), which no longer holds once an empty arg is in the input.
Could you add a wantScript field (defaulting to strings.Join(tc.args, " ") when unset) and cases like these? I checked these outputs against this branch:
{
name: "trailing empty arg",
args: []string{"echo", ""},
wantArgv: []string{"sh", "-c", `'echo ""'`},
wantScript: `echo ""`,
},
{
name: "empty arg before pipe",
args: []string{"echo", "", "|", "cat"},
wantArgv: []string{"sh", "-c", `'echo "" | cat'`},
wantScript: `echo "" | cat`,
},
{
name: "consecutive empty args",
args: []string{"printf", "%s", "", "", "tail"},
wantArgv: []string{"sh", "-c", `'printf %s "" "" tail'`},
wantScript: `printf %s "" "" tail`,
},
{
name: "empty arg next to single quotes",
args: []string{"echo", "'a'", ""},
wantArgv: []string{"sh", "-c", `"echo 'a' \"\""`},
wantScript: `echo 'a' ""`,
},There was a problem hiding this comment.
I added wantScript, it defaults to strings.Join when unset, and all four of your cases to TestBuildPluginArgv. Had to adjust one though: your "empty arg next to single quotes" case can't round-trip through shell.Fields either, same problem as the pipe case, since the literal single quotes in the value get read as shell quoting once wrapped. I pinned both of those to wantArgv alone instead of the round-trip.
| script := fields[2] | ||
|
|
||
| // Second round: sh -c parses the script itself. | ||
| finalArgs, err := shell.Fields(script, nil) |
There was a problem hiding this comment.
shell.Fields stands in for sh -c here, but it rejects shell operators (`|` is not a valid word). So this check only works when the empty arg is the only reason the line gets wrapped. The more likely task-config case is an empty arg next to a pipe or redirect (e.g. grep "" file | wc -l), and that can't be covered this way. Pinning the exact wantArgv in the table (see the comment above) covers both.
There was a problem hiding this comment.
Agreed, and it's actually one case wider than just pipes. The single-quote case above hits the same shell.Fields limit, since literal quote characters in a value are real shell syntax too, not just operators. I added a recoverable field to the table, true only when neither shows up, so the pipe and single-quote cases just rely on the pinned wantArgv, like you suggested.
ernestrc
left a comment
There was a problem hiding this comment.
Thanks, this addresses everything from the last round. All five new table cases fail against main's argv.go and pass here, and go test -race, go vet and gofmt are clean for internal/text/cmdenv.
One blocker before merge: the DCO check fails because 8625e44 has no Signed-off-by trailer (the first commit has one). Please run git commit -s --amend --no-edit and force-push.
| wantArgv: []string{"sh", "-c", "''"}, | ||
| name: "single empty arg", | ||
| args: []string{""}, | ||
| wantArgv: []string{"sh", "-c", `'""'`}, |
There was a problem hiding this comment.
Dropping the guard is the uniform rule I suggested, but it does change behaviour here: a lone [""] used to become sh -c '' (a no-op) and now becomes sh -c '""', which fails with exit 127 (command not found). No current caller can produce this (idetask rejects an empty Cmd, and :! with no args opens the companion terminal), so it's fine as is. Please mention it in the PR description, since that becomes the squashed commit message.
The len(args) > 1 guard made a lone empty arg behave differently from every other position: [""] produced a silent no-op (sh -c ''), while ["", "x"] or ["", ""] would try to run a command literally named "". Drop the guard so every empty arg is always spelled "", matching the rest of the args. Move the empty-arg cases into TestBuildPluginArgv's table with a wantScript field, since the round-trip check there compared against strings.Join(tc.args, " "), which no longer holds once an empty arg is in the input. Only run the second round-trip (recovering tc.args from wantScript) on cases with no real shell operators or literal quote characters in their values, since shell.Fields cannot parse either. Signed-off-by: Siddhardha Nanda <[email protected]>
8625e44 to
86d13b5
Compare
|
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
Summary
BuildPluginArgv(internal/text/cmdenv/argv.go) silently drops anempty-string task argument whenever it falls into its
sh -cfallbackalongside other args. Joining args with a plain space can't represent
an empty element: it collapses into bare whitespace, which
sh -c'sown word-splitting then discards with no resulting field, shifting
every later argument one position to the left.
This is reachable from ordinary task config:
internal/ide/idetaskbuilds a task's command directly from user-authored
Args []stringwith no guard against an embedded empty string.
Example:
BuildPluginArgv([]string{"printf", "%s", "", "tail"})previously produced a
sh -cscript whose own parsing recovers["printf", "%s", "tail"]instead of the original four arguments.Fix
Before joining, every empty-string element is replaced with a literal
""token so it survives both rounds of shell re-parsing the pipelineperforms (the executor's own re-tokenisation, then
sh -c's parsing ofthe script itself). This now applies uniformly, including a lone empty
arg:
BuildPluginArgv([]string{""})changes fromsh -c ''(a no-op)to
sh -c '""'(exit 127, command not found). No current caller canproduce a bare
[""]:idetaskrejects an emptyCmd, and:!withno args opens the companion terminal instead of calling this function.
Test plan
TestBuildPluginArgv's table(a
wantScriptfield covers what a lonestrings.Joincan'trepresent), including the lone-empty-arg case above and four
more from review: a trailing empty arg, one before a pipe,
consecutive empty args, and one next to literal single quotes.
Verified each fails against the pre-fix code and passes against
the fix.
go test -race ./internal/text/cmdenv/...passes.gofmt/go vetclean on the changed files.