oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
74.27k stars 2.77k forks source link

Bun is breaking a file from a dependency #14588

Open julien1619 opened 1 month ago

julien1619 commented 1 month ago

What version of Bun is running?

1.1.30

What platform is your computer?

Darwin 24.0.0 arm64 arm

What steps can reproduce the bug?

In my turborepo project, when I run bun build ./src/index.ts --outdir ./dist --target bun in my final project, it breaks one of my file as follow:

Original file from my dependency @react-email/tailwind from react-email (already built and minified in node_modules).

function ZI(t, e, n) {
  t instanceof RegExp && (t = nw(t, n)), e instanceof RegExp && (e = nw(e, n));
  var a = e3(t, e, n);
  return a && {
    start: a[0],
    end: a[1],
    pre: n.slice(0, a[0]),
    body: n.slice(a[0] + t.length, a[1]),
    post: n.slice(a[1] + e.length)
  };
}

For a better readability I've found the original function in balanced-match.

/**
 * @param {string | RegExp} a
 * @param {string | RegExp} b
 * @param {string} str
 */
export default function balanced (a, b, str) {
  if (a instanceof RegExp) a = maybeMatch(a, str)
  if (b instanceof RegExp) b = maybeMatch(b, str)

  const r = range(a, b, str)

  return (
    r && {
      start: r[0],
      end: r[1],
      pre: str.slice(0, r[0]),
      body: str.slice(r[0] + a.length, r[1]),
      post: str.slice(r[1] + b.length)
    }
  )
}

This is what bun is giving me as output:

function ZI(t4, e3, n4) {
  t4 instanceof RegExp && (t4 = nw(t4, n4)), e3 instanceof RegExp && (e3 = nw(e3, n4));
  var a = e3(t4, e3, n4);
  return a && {
    start: a[0],
    end: a[1],
    pre: n4.slice(0, a[0]),
    body: n4.slice(a[0] + t4.length, a[1]),
    post: n4.slice(a[1] + e3.length)
  };
}

As you can see, the problem is on the row var a = e3(t4, e3, n4);, originally e3 was a function from outside of the scope of the function, now e3 became one of the parameters of the function.

I don't know what triggers the problem, but if I patch the built file to change e3 to another name, it works, but I cannot reproduce it outside of my (private) project.

What is the expected behavior?

I expect bun to not break my dependency.

What do you see instead?

Bun rename one of the parameters of a function to the name of a function which is outside of its scope, changing the expected value from a function to a string in the variable.

Additional information

No response

pfgithub commented 4 days ago

What is the path to the already-minified file in node_modules that has function ZI(...)? I haven't found any version of balanced-match which is already minified