Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: __dirname and __filename collisions #17222

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

amorriscode
Copy link

What does this PR do?

This PR resolves issues with __dirname and __filename collisions (#17188) by creating unique variables when __dirname or __filename are used in a bundle.

How did you verify your code works?

  • I included a test for the new code, or existing tests cover it
  • I ran my tests locally and they pass (bun-debug test test-file-name.test)
  • Used the debug build to test the fix

Reproduction

When multiple files utilize __dirname/__filename Bun hoists the values to the top of the module code. However, since it is a var it can be overwritten effectively making it so only __dirname/__filename values collide.

Before

// test2/test.ts
var __dirname = "/Users/anthony/dev/test/test2", __filename = "/Users/anthony/dev/test/test2/test.ts";
function test() {
  console.log(__filename);
  console.log(__dirname);
}

// index.ts
var __dirname = "/Users/anthony/dev/test", __filename = "/Users/anthony/dev/test/index.ts";
console.log(__filename);
console.log(__dirname);
test();

In this single JS output, __dirname ends up evaluating to /Users/anthony/dev/test in both places it is called because that is the last value the var is assigned to.

After

The collision no longer occurs because usages of __dirname and __filename get unique values.

// test2/test.ts
var __dirname_af38e5cdfa6876c9 = "/Users/anthony/dev/test/test2", __filename_9b03b912cc9c01cd = "/Users/anthony/dev/test/test2/test.ts";
function test() {
  console.log(__filename_9b03b912cc9c01cd);
  console.log(__dirname_af38e5cdfa6876c9);
}

// index.ts
var __dirname_58f1efdc5b67fffe = "/Users/anthony/dev/test", __filename_c508108910e49d6d = "/Users/anthony/dev/test/index.ts";
console.log(__filename_c508108910e49d6d);
console.log(__dirname_58f1efdc5b67fffe);
test();

Copy link
Collaborator

@Jarred-Sumner Jarred-Sumner left a comment

Choose a reason for hiding this comment

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

Thank you for this - the changes look like they work as expected, but I think we need to add an option to control the behavior of __dirname and __filename and similar.

This collision is intentional and important for making things like .node addons bundle successfully. The behavior of __dirname and __filename when bundled is ambiguous.

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.

2 participants