oven-sh / bun

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

reference collision issue with `Bun.build` #14585

Open aralroca opened 4 weeks ago

aralroca commented 4 weeks ago

What version of Bun is running?

1.1.31-canary.53+5532e1af1

What platform is your computer?

Darwin 23.6.0 arm64 arm

What steps can reproduce the bug?

Reproduce it: way 1

I am getting this error when parsing this code with meriyah 6.0.2, but the problem only comes after build all the client code with Bun.build, using meriyah without build, is working fine.

import { type ESTree, parseScript } from 'meriyah';

const code = `import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
export default function Counter({ name }, { state }) {
    const count = state(0);
    return /*#__PURE__*/ _jsxs("p", {
        children: [
            /*#__PURE__*/ _jsx("button", {
                onClick: ()=>count.value++,
                children: "+"
            }),
            /*#__PURE__*/ _jsxs("span", {
                children: [
                    " ",
                    name,
                    " ",
                    count.value,
                    " "
                ]
            }),
            /*#__PURE__*/ _jsx("button", {
                onClick: ()=>count.value--,
                children: "-"
            })
        ]
    });
}`;

parseScript(code, {
  jsx: true,
  module: true,
  next: true,
});

Error from meriyah library after build with this code:

Error TypeError: c2 is not a function
Screenshot 2024-10-14 at 23 09 29

Reproduce it: way 2

This code is used in our Playground, so it is used in the client. So it is easy to reproduce it with these steps:

What is the expected behavior?

that the meriyah library would work after the build process.

What do you see instead?

Library error (but comes after build with Bun)

Error TypeError: c2 is not a function

Additional information

At first, I thought it was a bug in our parser (meriyah) and reported it here: https://github.com/meriyah/meriyah/issues/344 . Then we realized that it happened after the meriyah build process by Bun build.

3cp commented 4 weeks ago

c2 is a ParserState in meriyah, not a function.

bun 1.1.30 doesn't have this bug.

Comparing to output of bun 1.1.30:

case 67174411:
      return function(e2, n2, o3, r2, a3, i3, s2, l3, c3) {
        e2.flags = 128 ^ (128 | e2.flags);
        const { tokenIndex: u3, tokenLine: d3, tokenColumn: g3 } = e2;
        J(e2, 67117056 | n2);
        const k2 = 16 & n2 ? ue({ parent: undefined, type: 2 }, 1024) : undefined;
        if (n2 = 33554432 ^ (33554432 | n2), Q(e2, n2, 16))
          return yn(e2, n2, k2, o3, [], r2, 0, s2, l3, c3);

Note it uses e2 here, and the function call is yn(..., not e2(....

yn is a function defined in outer (global) scope.

function yn(e, n, o2, r2, a2, i2, s, l2, c2, u2) {
  i2 || t(e, 57);
  for (let n2 = 0;n2 < a2.length; ++n2)
    ne(e, a2[n2]);
  return Cn(e, n, o2, r2, a2, s, l2, c2, u2);
}

So this is a reference collision issue. The latest bun bundler somehow didn't realise reference c2 is already defined in outer scope, and it re-defined c2 in that local function scope.