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

bug: ReferenceError: Cannot access 'main$1' before initialization #4325

Closed
dnalborczyk opened this issue Dec 27, 2021 · 4 comments · Fixed by #4991
Closed

bug: ReferenceError: Cannot access 'main$1' before initialization #4325

dnalborczyk opened this issue Dec 27, 2021 · 4 comments · Fixed by #4991

Comments

@dnalborczyk
Copy link
Contributor

dnalborczyk commented Dec 27, 2021

Rollup Version

2.62.0

Operating System (or Browser)

n/a

Node Version (if applicable)

n/a

Link To Reproduction

https://rollupjs.org/repl/?version=2.62.0&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmltcG9ydCUyMGZvbyUyMGZyb20lMjAnLiUyRm1haW4uanMnJTNCJTVDbmV4cG9ydCUyMColMjBhcyUyMG5zJTIwZnJvbSUyMCcuJTJGbWFpbi5qcyclM0IlNUNuZXhwb3J0JTIwZGVmYXVsdCUyMCdmb28nJTNCJTIyJTJDJTIyaXNFbnRyeSUyMiUzQXRydWUlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyZXMlMjIlMkMlMjJuYW1lJTIyJTNBJTIybXlCdW5kbGUlMjIlMkMlMjJhbWQlMjIlM0ElN0IlMjJpZCUyMiUzQSUyMiUyMiU3RCUyQyUyMmdsb2JhbHMlMjIlM0ElN0IlN0QlN0QlMkMlMjJleGFtcGxlJTIyJTNBbnVsbCU3RA==

the above link works. this is only a problem when build with generatedCode: 'es2015', which can't [yet] be replicated with the online bundle.

the above is then bundled to:

const main = 'foo';

// notice "const" here:
const main$1 = /*#__PURE__*/Object.freeze({
	__proto__: null,
	'default': main,
	ns: main$1
});

export { main as default, main$1 as ns };

which throws an error:

ReferenceError: Cannot access 'main$1' before initialization
@lukastaegert
Copy link
Member

Definitely a bug, even without es2015 output, as ns would be undefined instead of an object. The best solution I could think of here would be to detect these cases and use a getter for ns. As a matter of fact, this is already implemented for slightly related cases where you have a circularity across different modules, maybe the check triggering this just needs to be extended: https://rollupjs.org/repl/?version=2.62.0&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmV4cG9ydCUyMColMjBhcyUyMG5zJTIwZnJvbSUyMCcuJTJGZm9vLmpzJyUzQiUyMiUyQyUyMmlzRW50cnklMjIlM0F0cnVlJTdEJTJDJTdCJTIybmFtZSUyMiUzQSUyMmZvby5qcyUyMiUyQyUyMmNvZGUlMjIlM0ElMjJpbXBvcnQlMjAqJTIwYXMlMjBucyUyMGZyb20lMjAnLiUyRm1haW4uanMnJTNCJTVDbmV4cG9ydCUyMGNvbnN0JTIwZm9vJTIwJTNEJTIwJ2ZvbyclM0IlNUNuY29uc29sZS5sb2cobnMpJTNCJTIyJTdEJTVEJTJDJTIyb3B0aW9ucyUyMiUzQSU3QiUyMmZvcm1hdCUyMiUzQSUyMmVzJTIyJTJDJTIybmFtZSUyMiUzQSUyMm15QnVuZGxlJTIyJTJDJTIyYW1kJTIyJTNBJTdCJTIyaWQlMjIlM0ElMjIlMjIlN0QlMkMlMjJnbG9iYWxzJTIyJTNBJTdCJTdEJTdEJTJDJTIyZXhhbXBsZSUyMiUzQW51bGwlN0Q=

@dnalborczyk
Copy link
Contributor Author

as ns would be undefined instead of an object.

dang, good point! didn't even notice! 😄

@lukastaegert
Copy link
Member

Fix at #4991

@rollup-bot
Copy link
Collaborator

This issue has been resolved via #4991 as part of rollup@3.21.8. You can test it via npm install rollup.

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

Successfully merging a pull request may close this issue.

3 participants