Repository navigation
fix(exec): run command NAME without the shell function NAME - #324
gautamsirdeshmukh merged 1 commit into
Conversation
`command NAME args` ran a shell function named NAME, as a plain call
does. A wrapper such as `grep() { command grep "$@"; }` then called
itself until the process stack overflowed. Bash runs the builtin or the
program instead.
The `command` builtin now marks the next pipeline to skip a function
of that name, in the run, the executable lookup, and the fallback check.
The differential case command_skips_function pins it against bash; the
suite aborts with a stack overflow without the change.
|
@strandly-the-agent review this pr |
strandly-the-agent
left a comment
There was a problem hiding this comment.
Approve. command NAME now skips the shell function NAME and matched bash on every command probe I ran; I found no regression against base. One non-blocking test gap is inline: nothing covers the gate that lets command foo reach a PATH executable past a function foo.
@ferdingler, this is ready for your look.
What I verified
Head 8b9c75e against merge-base 4b0bdbc, aarch64 Linux, debug builds in separate target directories, bash 5.2.15 as the reference.
- Suite at head:
cargo test --workspace --all-targetsgives 2664 passed, 0 failed, exit 0.cargo fmt --checkis clean. - New tests fail on base: with head's test files on base,
command_checkgives 29 passed and 2 failed (the two new tests), anddifferentialaborts with a stack overflow. .expectedmatches bash: re-recordedcommand_skips_functionunder the README's environment; identical.- Mark lifecycle (source): between the set at
src/exec.rs:3222and the take at:3410there is no await, early return or other caller.execute_pipeline_captureand the compound callers never see it set. All threeProcessconstructors start itfalse, andProcesshas noClone. - Gate coverage (mutation): removing the gate at
:3321,:3731or:3876fails a new test. Removing the one at:3509does not (inline). - 77 scripts under bash, base and head: wrappers, including the Claude Code
function grepfrom #323, in pipeline stages,$(…), loops, an EXIT trap,evaland a background job;command "$name", aliases, env prefixes, heredocs, failed redirects; a function shadowingsh,lash, registered commands and PATH scripts; no leak into a later pipeline or a substitution in the arguments. Head matched bash on allcommandbehaviour in scope. - 51 kernel-hook cases, each with and without the shadowing function: identical events and output at head, and every
check_commandgot exactly one finished or dropped. Covered:check_commandandcheck_fallbackrefusals, deadline, cancellation, output limit, failed redirect. - Not run: Python, Node and wasm bindings, release builds, other platforms.
Question (non-blocking)
Would a skip_function argument on execute_pipeline and run_pipeline be simpler than a field on Process? The other transient flags there (pending_exit, refused_array, deadline_stop) carry a result out of a pipeline; this is the first that carries an input into the next one. command in a later stage (#323) will need a per-stage marker in any case, so the field may not outlive that change. It is sound as written.
Appendix: pre-existing, same on base and head (3)
command ""returns 0 where bash returns 127, and"$UNSET"is dropped the same way, so[ -n "$UNSET" ]is true. #299 covers the dropped empty word.command command NAMEandcommand eval …printcommand not found(127). #323 listscommand -p,command --and the pipeline-stage form, but not these.xargs NAMEruns the shell function NAME; bash'sxargscannot see functions.
Description
Context.
command NAME argsruns NAME as a builtin, a registered command, or a program, and never as a shell function. Wrappers depend on that:grep() { command grep "$@"; }adds behavior and then calls the real grep. Claude Code 2.1.328 writes such wrappers forgrepandfindinto the shell it runs commands in.Before. The
commandbuiltin ran NAME through the normal pipeline, which looks up shell functions first. So the wrapper called itself until the stack overflowed. In this repository's differential suite, the new case aborts the test process with a stack overflow, beforemax_depthrefuses the recursion. An embedder that checks each command sees each level as a new request.After. The
commandbuiltin marks the pipeline that it runs to skip a function of that name. The mark applies to the function dispatch, the executable lookup, and the command-fallback check, socommand NAMEresolves NAME and checks it the same way as when no function exists.run_pipelinetakes the mark first, so it cannot reach a later pipeline or a command substitution inside the arguments.Verification.
command_skips_function, recorded from bash: a function that wrapsgrepwithcommand grep,command catpast acatfunction, andcommand ffor a name that only a function has (exit 127, as in bash), thenfagain, which runs the function.command toolasks the command fallback once and runs it, and a refused fallback runs nothing (exit 126). Both fail without the change. Without the change, the suite aborts with a stack overflow. With it, the suite passes.cargo test: every suite passes.cargo fmt --checkpasses.cargo clippy --all-targetsshows 15 warnings, the same count as onmain.commandin a later stage of a pipeline (echo hi | command grep hi) still printscommand: command not found. #323 tracks it, with the other forms that Claude Code's wrappers use.Related Issues
Part of #323.
Documentation PR
None.
commandnow behaves as POSIX specifies.Type of Change
Bug fix
Testing
cargo test; no binding changed)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.