Skip to content

Give a script found through PATH its path as $0 - #32

Merged
hex2f merged 1 commit into
mainfrom
leah/path-lookup-dollar0
Nov 17, 2026
Merged

hex2f merged 1 commit into
mainfrom
leah/path-lookup-dollar0

Conversation

@hex2f

@hex2f hex2f commented Nov 17, 2026

Copy link
Copy Markdown
Owner

Problem

When gash ran a shell script it found by searching PATH, $0 was the bare name as typed (probe), so dirname "$0" came out as .. bash, dash and zsh set $0 to the path they found, because they execve() that path.

Every npm/pnpm/yarn POSIX shim in node_modules/.bin finds its package with dirname "$0". With gash as npm's script-shell, those shims resolved their target from the current directory. For example, pnpm i failed with Cannot find module '...\packages\@reflect\machine-cache\bin\reuse-build.cjs'.

Fix

  • When find_in_path finds a file, it now records the PATH entry joined with the name of the file found (Resolved::found_as). Examples: ./node_modules/.bin/probe, node_modules/.bin/probe, /abs/dir/probe, or ./probe for an empty entry. Under PATHEXT lookup it is the file actually found.
  • Scripts run in-process use that path for $0 and BASH_SOURCE. When gash starts a separate shell for a script, it passes that path as the script argument.
  • A name typed with a slash keeps $0 exactly as typed. bash script.sh is unchanged.
  • Hits in a relative PATH entry are no longer stored in the hash table, because they depend on the current directory.

The output matches Git Bash for relative, absolute, trailing-slash and empty PATH entries, a PATH=... cmd prefix, pipelines, background jobs, and GASH_NO_INPROC=1.

Tests

  • New compat test tests/scripts/075_path_lookup_dollar0.sh. It covers relative and absolute PATH entries, an npm-style shim run from another directory, and the cases that must not change. The expected output was generated from Git Bash. The test fails on main (0=probe, dirname=.).
  • cargo test --release passes, including all 75 compat scripts.
  • Checked by hand: in the monorepo that hit this, pnpm i now runs the electron-webauthn and passkey install scripts (reuse-build ...) with gash as script-shell.

🤖 Generated with Claude Code

When a shell script was found by a PATH search, gash set $0 to the bare
name as typed (`probe`), so `dirname "$0"` was `.`. bash, dash and zsh
execve() the path they found, which becomes $0. The npm/pnpm/yarn shims
in node_modules/.bin locate their package with `dirname "$0"`, so with
gash as npm's script-shell they resolved targets from the current
directory instead (pnpm i failing with "Cannot find module").

A PATH hit now records the PATH entry joined with the name of the file
found (`./node_modules/.bin/probe`, or the absolute entry as given), and
that becomes $0 and BASH_SOURCE for scripts run in-process and the
script argument when a separate gash is started. Names typed with a
slash and `bash script.sh` are unchanged. Hits in relative PATH entries
are no longer hashed, since they depend on the current directory.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hex2f
hex2f merged commit c3481cb into main Nov 17, 2026
1 check passed
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