From 69344e2911e7728cbca51c24e0c2ec5da3b6d39f Mon Sep 17 00:00:00 2001 From: fullsend-code <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:19:43 +0000 Subject: [PATCH 1/6] fix(#9157): treat "export -n NAME" as un-exporting NAME The envmanifest shell export collector skipped every dash-prefixed export argument, so "export -n FOO" recorded FOO as exported, the opposite of what Bash does. Its result set was also add-only, so it could not retract a name exported earlier in the same script. collectFromCommand now parses export options up to "--" or the first operand, and -n (alone or combined, e.g. -pn) removes operands from the shell's export set instead of adding them. Shell-level exports are tracked separately from prefix and env assignments, which affect only their command's process and are never retracted. Each sh -c script gets its own export set, so its un-exports cannot reach the parent. Scripts passed to ExportedNames are still read in order as one shell. The "export flags are skipped" test expected the wrong result and is replaced by cases covering -n, -p, combined flags, "--", ordering, prefix assignments, and sh -c. Note: pre-commit could not fetch its remote hook repos in the sandbox (TLS verification failure); gofmt, go vet, whitespace/EOL and symlink checks were run directly and passed. Unrelated internal/cli tests (TestStartFetchService_*, TestPostScriptEnv_*) fail in the sandbox on main as well. Closes #9157 Co-Authored-By: Claude Opus 5.5 --- internal/envmanifest/shellexports.go | 61 ++++++++++++++++++----- internal/envmanifest/shellexports_test.go | 12 ++++- 2 files changed, 59 insertions(+), 14 deletions(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index c50ba695da..eaf815d328 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -21,7 +21,12 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) // // A plain assignment with no command ("NAME=value" alone, or "readonly // NAME=value") is shell-local and is not reported, nor is a prefix -// assignment on a shell builtin ("IFS= read"). Assignment-shaped text +// assignment on a shell builtin ("IFS= read"). "export -n NAME" removes +// the export attribute, so a name the scripts un-export after exporting +// it is not reported; scripts are read in order as one shell, and an +// "sh -c" script is its own shell whose un-exports do not reach its +// parent. Prefix assignments affect only their command's process, so a +// later "export -n" does not retract them. Assignment-shaped text // inside a quoted value is data, not a binding, and a command // substitution's exports stay in its subshell. The parser is a tokenizer, // not a shell: it handles single and double quotes, backslash escapes, @@ -29,8 +34,12 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) // separators, which is the subset the runner's emitters generate. func ExportedNames(scripts ...string) []string { seen := map[string]bool{} + exported := map[string]bool{} for _, script := range scripts { - collectExportedNames(script, seen, 0) + collectExportedNames(script, seen, exported, 0) + } + for name := range exported { + seen[name] = true } names := make([]string, 0, len(seen)) for name := range seen { @@ -74,17 +83,21 @@ func (w shellWord) assignmentName() string { return name } -func collectExportedNames(script string, seen map[string]bool, depth int) { +// collectExportedNames records the names script exports. seen collects +// names that reach a child process for certain (prefix and env +// assignments, and a finished child shell's exports); exported holds the +// current shell's export attributes, which a later "export -n" can clear. +func collectExportedNames(script string, seen, exported map[string]bool, depth int) { if depth > maxShellDepth { return } for _, words := range shellCommands(script) { - collectFromCommand(words, seen, depth) + collectFromCommand(words, seen, exported, depth) } } // collectFromCommand records the names one simple command exports. -func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { +func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth int) { // Leading NAME=value words are prefix assignments when a command // follows them, and shell-local assignments when nothing does. i := 0 @@ -102,14 +115,30 @@ func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { } switch cmd { case "export": + // Options precede the operands and end at "--" or the first + // operand; -n (alone or combined, as in -pn) switches the command + // to removing the export attribute. + unexport, optsDone := false, false for _, a := range args { - if strings.HasPrefix(a.text, "-") { + if !optsDone && strings.HasPrefix(a.text, "-") { + if a.text == "--" { + optsDone = true + } else if strings.Contains(a.text[1:], "n") { + unexport = true + } continue } - if name := a.assignmentName(); name != "" { - seen[name] = true - } else if envNameRe.MatchString(a.text) { - seen[a.text] = true + optsDone = true + name := a.assignmentName() + if name == "" && envNameRe.MatchString(a.text) { + name = a.text + } + switch { + case name == "": + case unexport: + delete(exported, name) + default: + exported[name] = true } } case "env": @@ -119,17 +148,23 @@ func collectFromCommand(words []shellWord, seen map[string]bool, depth int) { } name := a.assignmentName() if name == "" { - collectFromCommand(args[j:], seen, depth) + collectFromCommand(args[j:], seen, exported, depth) return } seen[name] = true } case "exec", "command", "nohup": - collectFromCommand(args, seen, depth) + collectFromCommand(args, seen, exported, depth) case "sh", "bash", "/bin/sh", "/bin/bash": for j, a := range args { if strings.HasPrefix(a.text, "-") && strings.Contains(a.text, "c") && j+1 < len(args) { - collectExportedNames(args[j+1].text, seen, depth+1) + // The child shell has its own export attributes: its + // "export -n" cannot un-export a name in this shell. + child := map[string]bool{} + collectExportedNames(args[j+1].text, seen, child, depth+1) + for name := range child { + seen[name] = true + } return } } diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index 7dce39781f..a0af63daae 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -15,7 +15,17 @@ func TestExportedNames(t *testing.T) { {"export with value", []string{"export FOO=bar"}, []string{"FOO"}}, {"bare re-export", []string{"export OPENCODE_CONFIG_CONTENT"}, []string{"OPENCODE_CONFIG_CONTENT"}}, {"several names in one export", []string{"export A=1 B C=3"}, []string{"A", "B", "C"}}, - {"export flags are skipped", []string{"export -n FOO"}, []string{"FOO"}}, + {"export -p flag is skipped", []string{"export -p BAR"}, []string{"BAR"}}, + {"export -n does not export", []string{"export -n FOO"}, []string{}}, + {"export -n retracts an earlier export", []string{"export FOO=bar; export -n FOO"}, []string{}}, + {"export -n with a value retracts", []string{"export FOO=1", "export -n FOO=2"}, []string{}}, + {"combined flags with n retract", []string{"export A B; export -pn A"}, []string{"B"}}, + {"export after export -n exports", []string{"export -n FOO; export FOO=1"}, []string{"FOO"}}, + {"export -n removes only its operands", []string{"export A B; export -n A"}, []string{"B"}}, + {"double dash ends export options", []string{"export -n -- A; export -- B"}, []string{"B"}}, + {"export -n keeps prefix assignments", []string{"FOO=1 cmd; export -n FOO"}, []string{"FOO"}}, + {"export -n inside sh -c", []string{`sh -c 'export X=1; export -n X'`}, []string{}}, + {"sh -c export -n does not reach the parent", []string{`export X=1; sh -c 'export -n X'`}, []string{"X"}}, {"plain assignment is shell-local", []string{"FOO=bar"}, []string{}}, {"readonly assignment is shell-local", []string{"readonly FOO=bar"}, []string{}}, {"dropping export drops the name", []string{"PI_OFFLINE=1 && export PI_TELEMETRY=0"}, []string{"PI_TELEMETRY"}}, From 9a1636a62b93341f59b447dad997c4f669a8b773 Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 14:43:45 +0000 Subject: [PATCH 2/6] fix(#9157): address review feedback on PR #9159 Snapshot the shell's exported names into the child-visible set when a non-builtin process launches, so a later export -n cannot retract names a child already inherited. Leave variables exported for export -f -n (function mode). Give env/exec/nohup wrapper recursion an isolated export set, since external programs cannot run the export builtin. Document the subshell limitation of the tokenizer. Addresses #9159 Co-Authored-By: Claude Sonnet 5.5 --- internal/envmanifest/shellexports.go | 44 ++++++++++++++++++----- internal/envmanifest/shellexports_test.go | 7 ++++ 2 files changed, 43 insertions(+), 8 deletions(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index eaf815d328..23f59fbb88 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -25,8 +25,13 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) // the export attribute, so a name the scripts un-export after exporting // it is not reported; scripts are read in order as one shell, and an // "sh -c" script is its own shell whose un-exports do not reach its -// parent. Prefix assignments affect only their command's process, so a -// later "export -n" does not retract them. Assignment-shaped text +// parent. Prefix assignments affect only their command's process, and a +// name exported when a process launches stays in that process's +// environment, so a later "export -n" does not retract either. "export +// -f" works on functions, so "export -fn NAME" leaves variable NAME +// exported. Parentheses are only separators, so an "export -n" inside a +// ( ... ) subshell is treated as if it ran in the parent shell. +// Assignment-shaped text // inside a quoted value is data, not a binding, and a command // substitution's exports stay in its subshell. The parser is a tokenizer, // not a shell: it handles single and double quotes, backslash escapes, @@ -57,6 +62,10 @@ var shellBuiltins = map[string]bool{ "echo": true, "eval": true, "exit": true, "false": true, "printf": true, "read": true, "return": true, "set": true, "shift": true, "test": true, "true": true, "unset": true, + "readonly": true, "local": true, "declare": true, "typeset": true, + "let": true, "pwd": true, "wait": true, "trap": true, "umask": true, + "getopts": true, "source": true, ".": true, "alias": true, + "hash": true, "type": true, "ulimit": true, } // maxShellDepth bounds sh -c recursion so a malformed input cannot loop. @@ -112,19 +121,33 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth for _, w := range words[:i] { seen[w.assignmentName()] = true } + // Launching a process hands it the shell's current export + // attributes, so a later "export -n" cannot retract them. "export" + // and "command" run inside the shell (command may run a builtin). + if cmd != "export" && cmd != "command" { + for name := range exported { + seen[name] = true + } + } } switch cmd { case "export": // Options precede the operands and end at "--" or the first // operand; -n (alone or combined, as in -pn) switches the command - // to removing the export attribute. - unexport, optsDone := false, false + // to removing the export attribute. -f selects the function + // namespace, so a function-mode "-n" leaves variables exported. + unexport, funcMode, optsDone := false, false, false for _, a := range args { if !optsDone && strings.HasPrefix(a.text, "-") { if a.text == "--" { optsDone = true - } else if strings.Contains(a.text[1:], "n") { - unexport = true + } else { + if strings.Contains(a.text[1:], "n") { + unexport = true + } + if strings.Contains(a.text[1:], "f") { + funcMode = true + } } continue } @@ -135,6 +158,7 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth } switch { case name == "": + case unexport && funcMode: case unexport: delete(exported, name) default: @@ -148,12 +172,16 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth } name := a.assignmentName() if name == "" { - collectFromCommand(args[j:], seen, exported, depth) + // env launches an external program, which cannot run the + // export builtin, so the command gets an isolated set. + collectFromCommand(args[j:], seen, map[string]bool{}, depth) return } seen[name] = true } - case "exec", "command", "nohup": + case "exec", "nohup": + collectFromCommand(args, seen, map[string]bool{}, depth) + case "command": collectFromCommand(args, seen, exported, depth) case "sh", "bash", "/bin/sh", "/bin/bash": for j, a := range args { diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index a0af63daae..8584c6e1e5 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -26,6 +26,13 @@ func TestExportedNames(t *testing.T) { {"export -n keeps prefix assignments", []string{"FOO=1 cmd; export -n FOO"}, []string{"FOO"}}, {"export -n inside sh -c", []string{`sh -c 'export X=1; export -n X'`}, []string{}}, {"sh -c export -n does not reach the parent", []string{`export X=1; sh -c 'export -n X'`}, []string{"X"}}, + {"export -n after an external command keeps the name", []string{"export X=1; cmd; export -n X"}, []string{"X"}}, + {"export -n after a child shell keeps the name", []string{"export X=1; bash -c 'printenv X'; export -n X"}, []string{"X"}}, + {"export -n after a builtin still retracts", []string{"export X=1; echo hi; readonly Y=2; export -n X"}, []string{}}, + {"export -n after command export still retracts", []string{"export X=1; command export -n X"}, []string{}}, + {"export -fn leaves the variable exported", []string{"export X=1; export -fn X"}, []string{"X"}}, + {"export -f -n leaves the variable exported", []string{"export X=1; export -f -n X"}, []string{"X"}}, + {"env cannot run the export builtin", []string{"export X=1; env export -n X"}, []string{"X"}}, {"plain assignment is shell-local", []string{"FOO=bar"}, []string{}}, {"readonly assignment is shell-local", []string{"readonly FOO=bar"}, []string{}}, {"dropping export drops the name", []string{"PI_OFFLINE=1 && export PI_TELEMETRY=0"}, []string{"PI_TELEMETRY"}}, From 9b1d49265281af3255a38ce1fcb2f54060a60d04 Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:03:20 +0000 Subject: [PATCH 3/6] fix(#9157): address review feedback on PR #9159 Reject export commands with an option other than -f, -n or -p as a no-op, as Bash does, so an invalid option containing n no longer un-exports names. Parse command options (-p, -v, -V, --) before dispatch so lookup-only modes launch nothing and option words are not mistaken for the executable. Not addressed: the info-level subshell finding is a documented tokenizer limitation. pre-commit could not run (hook repos failed TLS verification); gofmt, go vet, git diff --check and tests were run directly. Addresses #9159 Co-Authored-By: Claude Sonnet 5.5 --- internal/envmanifest/shellexports.go | 26 +++++++++++++++++++++-- internal/envmanifest/shellexports_test.go | 10 +++++++++ 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index 23f59fbb88..f609dad709 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -29,7 +29,9 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) // name exported when a process launches stays in that process's // environment, so a later "export -n" does not retract either. "export // -f" works on functions, so "export -fn NAME" leaves variable NAME -// exported. Parentheses are only separators, so an "export -n" inside a +// exported. An "export" with an option other than -f, -n or -p +// changes nothing, and "command -v" only looks a command up, so it +// launches nothing. Parentheses are only separators, so an "export -n" inside a // ( ... ) subshell is treated as if it ran in the parent shell. // Assignment-shaped text // inside a quoted value is data, not a binding, and a command @@ -142,6 +144,11 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth if a.text == "--" { optsDone = true } else { + // An option the builtin rejects makes the whole + // command a no-op, so no export state changes. + if strings.Trim(a.text[1:], "fnp") != "" { + return + } if strings.Contains(a.text[1:], "n") { unexport = true } @@ -182,7 +189,22 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth case "exec", "nohup": collectFromCommand(args, seen, map[string]bool{}, depth) case "command": - collectFromCommand(args, seen, exported, depth) + // Options end at "--" or the first non-option word. -v and -V + // only look the command up, and an option the builtin rejects + // runs nothing, so neither launches a process. + for j, a := range args { + if a.text == "--" { + collectFromCommand(args[j+1:], seen, exported, depth) + return + } + if !strings.HasPrefix(a.text, "-") || a.text == "-" { + collectFromCommand(args[j:], seen, exported, depth) + return + } + if strings.Trim(a.text[1:], "pvV") != "" || strings.ContainsAny(a.text[1:], "vV") { + return + } + } case "sh", "bash", "/bin/sh", "/bin/bash": for j, a := range args { if strings.HasPrefix(a.text, "-") && strings.Contains(a.text, "c") && j+1 < len(args) { diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index 8584c6e1e5..6cd617f7b0 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -32,6 +32,16 @@ func TestExportedNames(t *testing.T) { {"export -n after command export still retracts", []string{"export X=1; command export -n X"}, []string{}}, {"export -fn leaves the variable exported", []string{"export X=1; export -fn X"}, []string{"X"}}, {"export -f -n leaves the variable exported", []string{"export X=1; export -f -n X"}, []string{"X"}}, + {"invalid export option is a no-op", []string{"export X=1; export -notvalid X"}, []string{"X"}}, + {"invalid export option does not export", []string{"export -xp X"}, []string{}}, + {"command -v launches nothing", []string{"export X=1; command -v bash; export -n X"}, []string{}}, + {"command -V launches nothing", []string{"export X=1; command -V bash; export -n X"}, []string{}}, + {"command with invalid option launches nothing", []string{"export X=1; command -z bash; export -n X"}, []string{}}, + {"command -p runs the command", []string{"export X=1; command -p cmd; export -n X"}, []string{"X"}}, + {"command -- runs the command", []string{"export X=1; command -- cmd; export -n X"}, []string{"X"}}, + {"command -p skips option words for export", []string{"export X=1; command -p export -n X"}, []string{}}, + {"command with only options runs nothing", []string{"export X=1; command -p; export -n X"}, []string{}}, + {"command after a lone dash runs it", []string{"export X=1; command - ; export -n X"}, []string{"X"}}, {"env cannot run the export builtin", []string{"export X=1; env export -n X"}, []string{"X"}}, {"plain assignment is shell-local", []string{"FOO=bar"}, []string{}}, {"readonly assignment is shell-local", []string{"readonly FOO=bar"}, []string{}}, From 82e8f25db655e3d62ef043844da587c72e846ef6 Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:18:53 +0000 Subject: [PATCH 4/6] fix(#9157): address review feedback on PR #9159 - exec with no command launches no process, so it no longer snapshots the current export attributes. - A lone "-" ends export option parsing and is an operand. - Plain "export -f" operands are functions and never recorded as exported variables. Not changed: subshell export isolation (documented tokenizer limitation). Note: pre-commit could not fetch remote hook repos (TLS failure); ran gofmt, go vet and go test directly instead. Addresses #9159 Co-Authored-By: Claude Sonnet 5.5 --- internal/envmanifest/shellexports.go | 16 +++++++++------- internal/envmanifest/shellexports_test.go | 4 ++++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index f609dad709..bfc8ac894b 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -125,8 +125,9 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth } // Launching a process hands it the shell's current export // attributes, so a later "export -n" cannot retract them. "export" - // and "command" run inside the shell (command may run a builtin). - if cmd != "export" && cmd != "command" { + // and "command" run inside the shell (command may run a builtin), + // and "exec" with no command launches nothing. + if cmd != "export" && cmd != "command" && (cmd != "exec" || len(args) > 0) { for name := range exported { seen[name] = true } @@ -135,12 +136,13 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth switch cmd { case "export": // Options precede the operands and end at "--" or the first - // operand; -n (alone or combined, as in -pn) switches the command - // to removing the export attribute. -f selects the function - // namespace, so a function-mode "-n" leaves variables exported. + // operand (a lone "-" is an operand); -n (alone or combined, as in + // -pn) switches the command to removing the export attribute. -f + // selects the function namespace, so function-mode operands never + // change a variable's export attribute. unexport, funcMode, optsDone := false, false, false for _, a := range args { - if !optsDone && strings.HasPrefix(a.text, "-") { + if !optsDone && a.text != "-" && strings.HasPrefix(a.text, "-") { if a.text == "--" { optsDone = true } else { @@ -165,7 +167,7 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth } switch { case name == "": - case unexport && funcMode: + case funcMode: case unexport: delete(exported, name) default: diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index 6cd617f7b0..b01ab5986a 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -32,6 +32,10 @@ func TestExportedNames(t *testing.T) { {"export -n after command export still retracts", []string{"export X=1; command export -n X"}, []string{}}, {"export -fn leaves the variable exported", []string{"export X=1; export -fn X"}, []string{"X"}}, {"export -f -n leaves the variable exported", []string{"export X=1; export -f -n X"}, []string{"X"}}, + {"plain export -f does not export a variable", []string{"export -f FUNC"}, []string{}}, + {"exec with no command launches nothing", []string{"export X=1; exec; export -n X"}, []string{}}, + {"exec with a command launches a process", []string{"export X=1; exec true; export -n X"}, []string{"X"}}, + {"export lone dash is an operand", []string{"export X=1; export - -n X"}, []string{"X"}}, {"invalid export option is a no-op", []string{"export X=1; export -notvalid X"}, []string{"X"}}, {"invalid export option does not export", []string{"export -xp X"}, []string{}}, {"command -v launches nothing", []string{"export X=1; command -v bash; export -n X"}, []string{}}, From 116eb03caf95bb3729dd2ef903d1e1e9b01d9a04 Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:35:13 +0000 Subject: [PATCH 5/6] fix(#9157): address review feedback on PR #9159 Treat the builtin dispatcher as a shell builtin so it does not snapshot exported names as a process launch, and process its export/command/exec targets with builtin semantics. Adds regression tests. Note: pre-commit could not fetch hook repositories (certificate verification failure); gofmt and go vet were run directly and passed. Addresses #9159 Co-Authored-By: Claude Sonnet 5.5 --- internal/envmanifest/shellexports.go | 21 ++++++++++++++++++--- internal/envmanifest/shellexports_test.go | 6 ++++++ 2 files changed, 24 insertions(+), 3 deletions(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index bfc8ac894b..e5564e2fa5 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -30,8 +30,9 @@ var envNameRe = regexp.MustCompile(`^[A-Za-z_][A-Za-z0-9_]*$`) // environment, so a later "export -n" does not retract either. "export // -f" works on functions, so "export -fn NAME" leaves variable NAME // exported. An "export" with an option other than -f, -n or -p -// changes nothing, and "command -v" only looks a command up, so it -// launches nothing. Parentheses are only separators, so an "export -n" inside a +// changes nothing, and "command -v" only looks a command up and +// "builtin" runs only a shell builtin, so neither launches a process. +// Parentheses are only separators, so an "export -n" inside a // ( ... ) subshell is treated as if it ran in the parent shell. // Assignment-shaped text // inside a quoted value is data, not a binding, and a command @@ -67,7 +68,7 @@ var shellBuiltins = map[string]bool{ "readonly": true, "local": true, "declare": true, "typeset": true, "let": true, "pwd": true, "wait": true, "trap": true, "umask": true, "getopts": true, "source": true, ".": true, "alias": true, - "hash": true, "type": true, "ulimit": true, + "hash": true, "type": true, "ulimit": true, "builtin": true, } // maxShellDepth bounds sh -c recursion so a malformed input cannot loop. @@ -188,6 +189,20 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth } seen[name] = true } + case "builtin": + // builtin runs only a shell builtin, never an external program, so + // it launches no process itself. A target outside the shell's + // own export/command/exec/builtin dispatchers has no effect on + // export attributes, and a non-builtin target is an error. + if len(args) > 0 && args[0].text == "--" { + args = args[1:] + } + if len(args) > 0 { + switch args[0].text { + case "export", "command", "exec", "builtin": + collectFromCommand(args, seen, exported, depth) + } + } case "exec", "nohup": collectFromCommand(args, seen, map[string]bool{}, depth) case "command": diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index b01ab5986a..fe86c230b8 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -46,6 +46,12 @@ func TestExportedNames(t *testing.T) { {"command -p skips option words for export", []string{"export X=1; command -p export -n X"}, []string{}}, {"command with only options runs nothing", []string{"export X=1; command -p; export -n X"}, []string{}}, {"command after a lone dash runs it", []string{"export X=1; command - ; export -n X"}, []string{"X"}}, + {"builtin true launches nothing", []string{"export X=1; builtin true; export -n X"}, []string{}}, + {"builtin -- true launches nothing", []string{"export X=1; builtin -- true; export -n X"}, []string{}}, + {"builtin export -n retracts", []string{"export X=1; builtin export -n X"}, []string{}}, + {"builtin with a non-builtin launches nothing", []string{"export X=1; builtin cmd; export -n X"}, []string{}}, + {"builtin exec runs a process", []string{"export X=1; builtin exec cmd; export -n X"}, []string{"X"}}, + {"prefix assignment on builtin is not exported", []string{"Y=1 builtin true"}, []string{}}, {"env cannot run the export builtin", []string{"export X=1; env export -n X"}, []string{"X"}}, {"plain assignment is shell-local", []string{"FOO=bar"}, []string{}}, {"readonly assignment is shell-local", []string{"readonly FOO=bar"}, []string{}}, From 47cccc2cd1d25f755e5b55de37ca5b516bf3faef Mon Sep 17 00:00:00 2001 From: fullsend-fix <278716306+fullsend-ai-coder[bot]@users.noreply.github.com> Date: Fri, 6 Nov 2026 15:50:11 +0000 Subject: [PATCH 6/6] fix(#9157): address review feedback on PR #9159 Keep a command's prefix assignments when "builtin" dispatches to export/command/exec/builtin, so "Y=1 builtin exec prog" and "Y=1 builtin command prog" report Y while "Y=1 builtin true" still does not. Add regression tests. Note: pre-commit could not fetch hook repositories (TLS verification failure), so the hooks were not run via pre-commit. Applicable checks (gofmt, go vet, go test, whitespace/EOL, secret scan) were run directly. Addresses #9159 Co-Authored-By: Claude Sonnet 5.5 --- internal/envmanifest/shellexports.go | 5 ++++- internal/envmanifest/shellexports_test.go | 4 ++++ 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/internal/envmanifest/shellexports.go b/internal/envmanifest/shellexports.go index e5564e2fa5..7c6cde6551 100644 --- a/internal/envmanifest/shellexports.go +++ b/internal/envmanifest/shellexports.go @@ -197,10 +197,13 @@ func collectFromCommand(words []shellWord, seen, exported map[string]bool, depth if len(args) > 0 && args[0].text == "--" { args = args[1:] } + // The dispatched command keeps this command's prefix assignments, + // so "Y=1 builtin exec prog" still hands Y to prog. if len(args) > 0 { switch args[0].text { case "export", "command", "exec", "builtin": - collectFromCommand(args, seen, exported, depth) + target := append(slices.Clone(words[:i]), args...) + collectFromCommand(target, seen, exported, depth) } } case "exec", "nohup": diff --git a/internal/envmanifest/shellexports_test.go b/internal/envmanifest/shellexports_test.go index fe86c230b8..7bb4fecdfa 100644 --- a/internal/envmanifest/shellexports_test.go +++ b/internal/envmanifest/shellexports_test.go @@ -52,6 +52,10 @@ func TestExportedNames(t *testing.T) { {"builtin with a non-builtin launches nothing", []string{"export X=1; builtin cmd; export -n X"}, []string{}}, {"builtin exec runs a process", []string{"export X=1; builtin exec cmd; export -n X"}, []string{"X"}}, {"prefix assignment on builtin is not exported", []string{"Y=1 builtin true"}, []string{}}, + {"prefix assignment on builtin exec reaches the process", []string{"Y=1 builtin exec /usr/bin/env"}, []string{"Y"}}, + {"prefix assignment on builtin command reaches the process", []string{"Y=1 builtin command cmd"}, []string{"Y"}}, + {"prefix assignment on nested builtin exec reaches the process", []string{"Y=1 builtin builtin exec cmd"}, []string{"Y"}}, + {"prefix assignment on builtin -- true is not exported", []string{"Y=1 builtin -- true"}, []string{}}, {"env cannot run the export builtin", []string{"export X=1; env export -n X"}, []string{"X"}}, {"plain assignment is shell-local", []string{"FOO=bar"}, []string{}}, {"readonly assignment is shell-local", []string{"readonly FOO=bar"}, []string{}},