Skip to content

runtime: preserve $0 while sourcing files - #771

Merged
ewhauser merged 4 commits into
mainfrom
codex/preserve-sourced-arg0
Mar 28, 2026
Merged

ewhauser merged 4 commits into
mainfrom
codex/preserve-sourced-arg0

Conversation

@ewhauser

Copy link
Copy Markdown
Owner

Summary

Fix sourced-file $0 handling so gbash preserves the shell invocation name while BASH_SOURCE[0] tracks the sourced file.

Root cause

While a sourced file was running, gbash resolved $0 from the current parse filename instead of a stable shell argv0. That made guards like [[ "${BASH_SOURCE[0]}" == "${0}" ]] flip the wrong way under source, which caused the vendored Harness bootstrap to think it was being executed directly.

What changed

  • add a stable runner Arg0 field and seed it from execution context
  • preserve that value across runner reset and subshell cloning
  • resolve $0 from the stable arg0 instead of the current source filename
  • add runtime regressions covering sourced command-string, sourced script-path, and Harness-style source guards

Validation

  • go test ./internal/runtime -run 'TestExecInlineSourcePreservesArg0WhileUpdatingBashSource|TestExecScriptPathSourcePreservesTopLevelArg0|TestExecInlineSourceGuardReportsSourced|TestExecInlineSourceTracksCallLinesWithoutPseudoFrames|TestExecScriptPathSetsBashIntrospection|TestExecNameOnlyLeavesBashSourceUnset'
  • go test ./internal/shell/interp -run 'TestPositionalZeroDefaultsToGBash'
  • timeout 8s go run ./contrib/extras/cmd/gbash-extras --root /Users/ewhauser/.codex/worktrees/bb68/gbash/examples/harness-overlay/workspace --cwd /home/agent/project -c 'set -x; source ./bin/harness; echo after-source'

Notes

  • examples/harness-overlay is not present on main, so the example-side bug note update was intentionally not included in this PR.
  • The separate gbash-extras -s < <(...) stdin/process-substitution timeout is still reproducible for trivial stdin scripts and is not part of this fix.

@ewhauser
ewhauser marked this pull request as ready for review March 28, 2026 21:40

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fa9b3dc157

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/shell/core.go Resolved

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: d0268ca29c

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment thread internal/shell/interp/vars.go Resolved
@ewhauser ewhauser changed the title [codex] runtime: preserve $0 while sourcing files runtime: preserve $0 while sourcing files Mar 28, 2026
@ewhauser
ewhauser merged commit ccd38a3 into main Mar 28, 2026
16 checks passed
@ewhauser
ewhauser deleted the codex/preserve-sourced-arg0 branch March 28, 2026 23:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant