Repository navigation
feat(sh): run command strings with sh -c - #212
Merged
Merged
Conversation
10 tasks done
Collaborator
Author
|
@strandly-the-agent please review |
3 tasks done
strandly-the-agent
left a comment
Collaborator
There was a problem hiding this comment.
Verdict: approve once rebased. The -c handling is correct and small. I found no bugs in the new logic. The PR is CONFLICTING with main in src/exec.rs, and resolving it naively loses #248's script-name parse errors (see the inline comment).
- 🟡 Rebase: pass an origin through
run_sourced→execute_source_file. There's a suggestion inline. - 🟡 Add a test for the
-cpath through the kernel command check:tests/command_check.rs(#204, on main only) coverssh /tmp/sinnested_commands_are_checkedbut notsh -c. After the rebase,"sh -c 'rm /tmp/x'"and"sh -c '\"$0\" /tmp/x' rm"both pass. Add them so this new nesting path can't silently bypassKernel::check_commandlater. - I filed #250 for a pre-existing bug this makes easier to hit:
set -e; sh -c 'false; echo x'aborts the parent, because the child inheritsset -e/-u/-x.
The adversarial pass timed out after 900s on its first run. The re-run completed and found nothing new that this PR introduces.
✅ What I verified
- Head
caa0801:cargo test --test shell_integrationpassed 1569/1569, including all newsh_c_*/lash_c_*tests andscript_args_and_isolation_unchanged.cargo test --test differentialpasses.cargo fmt --checkis clean.cargo clippy --all-targetsadds no new warnings (the only one is the existinggrep_F_fixednaming). CI is green. - I merged into main
a064834with the inline resolution:shell_integrationpassed 1607 tests,differentialpassed,command_checkpassed 23 (with the 2 cases above added). - Matches bash:
$0/$1/$#/$@,shift,exit N/return Nand$?,--handling (sh -c -- cmd n a,sh -c cmd -- a→$0=--), the missing-argument error with status 2, nestedsh -c "sh -c … inner" outer,$(sh -c …),sh -c … | cmd, redirects,cd/export/set -ein the child not leaking to the parent,$0/args restored after returning (from top level, from inside a function, from inside a script),sh script.sh$0unchanged, and recursion bounded by the max-depth error.
Questions (non-blocking)
sh -ec/-xc/-eucstill go down the file path (-ec: no such file or directory, 126), andsh -c -e cmdruns-eas the command. Agents write these a lot. Is a follow-up planned under #207?bash -creturns 127 (bash: command not found). Is aliasingbashin scope for #207?- The PR body says
$(...)capture doesn't work yet, but on this headx=$(sh -c 'echo hi')giveshi. Worth updating the description.
Appendix: pre-existing, not from this PR (same on `sh script.sh`)
- Child inherits
set -e/-u/-x: filed #250. - Fork leakage (same root cause as #250, mentioned there): the parent's unexported vars (
X=1; sh -c 'echo $X'→1, bash prints empty), its functions (f(){ echo fn; }; sh -c f), and its aliases are all visible in the child. - An EXIT trap doesn't fire in a child: the trap only runs at
proc.depth == 0(src/exec.rs~L202 on main).trap "echo bye" EXITinsh /tmp/t.shdoesn't fire either. Not filed; it belongs with the #207 gaps. - xtrace and stderr ordering: the child's stdout and stderr are captured separately and flushed stdout-first, so
set -xlines and>&2output come after stdout. Scripts do the same. false; sh -c 'echo $?'prints1(bash prints0). A syntax error in the child exits 1 (bash exits 2).""arguments are dropped during expansion, sosh -c ''gives status 2 (bash gives 0).sh -- -copens--. Piped stdin intosh -cis empty (you already noted this; #224 addresses it).
Collaborator
Author
|
Thanks for the review. Merged main (4a5ba2a) and pushed 8aa1535.
On the questions:
Full local run on 8aa1535: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
shandlashalways treated their first argument as a script file, sosh -cdidn't work at all:Now
sh -c 'cmd' [name [args...]]runs the string the same waysh script.shruns a script: in a child shell, so variables andcddon't leak back.$0is the name you pass, orsh/lashby default, and$1...are the rest. It also skips a leading--and errors with status 2 when the string is missing, like bash. Piped stdin and$(...)capture work the same way they do for scripts. It touches the same script-running code as #199, but #199 still applies cleanly on top.I compared the cases against bash on Linux: positional args, exit status, multiple statements, redirects,
--, the missing-argument error, and nothing leaking into the parent. The new tests fail on main and pass with the change, andsh script.sh argsbehaves the same as before.Related Issues
Part of #207
Documentation PR
N/A
Type of Change
New feature
Testing
How have you tested the change? Verify that the changes do not break functionality or introduce new warnings.
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.