Skip to content

__dirname handled incorrectly when building #17188

Open

Description

@ficusk

What version of Bun is running?

1.2.0+b0c5a7655

What platform is your computer?

Darwin 24.3.0 arm64 arm

What steps can reproduce the bug?

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/bash

if [ -d "dirname-bug" ]; then
    echo "dirname-bug already exists"
    exit 1
fi

mkdir -p dirname-bug
cd dirname-bug

cat << EOF > apple.ts
import { orange } from "./subdir/orange";

console.log(\`\${__dirname}\`);
orange();
EOF

mkdir subdir

cat << EOF > subdir/orange.ts
export function orange() {
    console.log(\`\${__dirname}\`);
}
EOF

echo "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:

/Users/ficus/projects/bun-dirname-broken/dirname-bug
/Users/ficus/projects/bun-dirname-broken/dirname-bug/subdir

What do you see instead?

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:

// @bun
// subdir/orange.ts
var __dirname = "/Users/ficus/projects/bun-dirname-broken/dirname-bug/subdir";
function orange() {
  console.log(`${__dirname}`);
}

// apple.ts
var __dirname = "/Users/ficus/projects/bun-dirname-broken/dirname-bug";
console.log(`${__dirname}`);
orange();

Activity

  1. RiskyMH commented on Feb 8, 2025

    @RiskyMH
    Contributor

    Yeah this has been a bug/feature for a while, see #4216

    (i do hope it gets fixed soon as its confusing and yeah)

  2. RiskyMH commented on Feb 10, 2025

    @RiskyMH
    Contributor

    Actually after re-reading this, it is kinda similar but this shares an even worse outcome

  3. added
    bundlerSomething to do with the bundler
    and removed on Feb 10, 2025
  4. amorriscode commented on Feb 10, 2025

    @amorriscode

    Made an attempt at a fix with #17222

  5. robobun commented on Aug 13, 2026

    @robobun
    Collaborator

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingbundlerSomething to do with the bundler

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions