You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
__dirname handled incorrectly when building #17188
Making references to __dirname from multiple different directories doesn't work correctly when running a js file emitted by bun build.
This shell script contains a complete repro:
#!/bin/bashif [ -d"dirname-bug" ];thenecho"dirname-bug already exists"exit 1
fi
mkdir -p dirname-bug
cd dirname-bug
cat <<EOF > apple.tsimport { orange } from "./subdir/orange";console.log(\`\${__dirname}\`);orange();EOF
mkdir subdir
cat <<EOF > subdir/orange.tsexport function orange() { console.log(\`\${__dirname}\`);}EOFecho"bun run apple.ts"
bun run apple.ts
echo"bun build apple.ts --outdir dist --target bun"
bun build apple.ts --outdir dist --target bun
echo"bun run dist/apple.js"
bun run dist/apple.js
What is the expected behavior?
Both the run version and the build version should output the same thing:
The run version is correct but the built version is wrong:
❯ ./repro.sh
bun run apple.ts
/Users/ficus/projects/bun-dirname-broken/dirname-bug
/Users/ficus/projects/bun-dirname-broken/dirname-bug/subdir
bun build apple.ts --outdir dist --target bun
./apple.js 0.28 KB
[4ms] bundle 2 modules
bun run dist/apple.js
/Users/ficus/projects/bun-dirname-broken/dirname-bug
/Users/ficus/projects/bun-dirname-broken/dirname-bug
Additional information
The problem is that the compiler is emitting a top level declaration for each reference to __dirname, so all references to __dirname evaluate to the last one in the file. Here's the output of bun build from my repro script:
Triage note: two open PRs had been linked to this issue. #35470 (runtime __dirname for every --target=bun / --target=node bundle) is closed; the maintainer feedback on #17222 was that the build-time inlining for plain bundles is intentional and a change there needs to be opt-in, so that part is still undecided. #29066 stays open but only changes bun build --compile executables (where __dirname becomes the executable's virtual /$bunfs/root directory); it does not change the plain bun build --target bun output shown in this issue, so this issue remains open and is not fixed by either PR. Still reproduces on current main.
Activity
RiskyMH commentedon Feb 8, 2025
Yeah this has been a bug/feature for a while, see #4216
(i do hope it gets fixed soon as its confusing and yeah)
RiskyMH commentedon Feb 10, 2025
Actually after re-reading this, it is kinda similar but this shares an even worse outcome
amorriscode commentedon Feb 10, 2025
Made an attempt at a fix with #17222
robobun commentedon Aug 13, 2026
Triage note: two open PRs had been linked to this issue. #35470 (runtime
__dirnamefor every--target=bun/--target=nodebundle) is closed; the maintainer feedback on #17222 was that the build-time inlining for plain bundles is intentional and a change there needs to be opt-in, so that part is still undecided. #29066 stays open but only changesbun build --compileexecutables (where__dirnamebecomes the executable's virtual/$bunfs/rootdirectory); it does not change the plainbun build --target bunoutput shown in this issue, so this issue remains open and is not fixed by either PR. Still reproduces on current main.