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

preserveModules mess with import order in esm/system output with circular dependencies #3834

Closed
Amareis opened this issue Oct 23, 2020 · 6 comments

Comments

@Amareis
Copy link
Contributor

Amareis commented Oct 23, 2020

  • Rollup Version: from 2.28.2 (maybe and prior) to latest (fom github master)
  • Operating System (or Browser): Any ESM-compatible (node, chrome)
  • Node Version (if applicable):any ESM-compatible
  • Link to reproduction (IMPORTANT, read below): https://repl.it/repls/PlumpRadiantBetatest

Expected Behavior

After build module tree should working.

Actual Behavior

It don't works, because of wrong import order (tag.js run before data.js).

Additional: read this for rationale of such import structure. In our real project there is muuuuuuch more files instead of single tag.js, and in webpack (or simple rollup build, or in browser after simple babel transpilation) all is working good.
https://medium.com/visual-development/how-to-fix-nasty-circular-dependency-issues-once-and-for-all-in-javascript-typescript-a04c987cf0de

@Amareis
Copy link
Contributor Author

Amareis commented Oct 23, 2020

To fix this rollup shouldn't play with import chain simplifying, at least if imported file participates in some circular chains.

@lukastaegert
Copy link
Member

lukastaegert commented Oct 23, 2020

I tried running both the code in src and the code in dist in Node 14. The only entry point that worked in the src folder without immediately giving me this (typical circular dependency) error

ReferenceError: Cannot access 'data' before initialization

was tag.js. Neither src/index.js nor src/main.js were valid entries in an actual runtime.

When running dist/tag.js, I got exactly the same output as when running it in src. Could you please go into more detail how to reproduce the bug, i.e. in what situation the generated code behaves differently from the source code?

Also note that "import chain simplification" should no change relevant execution order as additional side-effect imports are generated as needed. But I am open to counter-examples.

That is not to say that there are no issues with execution order in Rollup, but all known issues are based on the fact that Rollup sometimes creates too few chunks and will never manifest if you use preserveModules.

@lukastaegert
Copy link
Member

Admittedly, TDZ errors are not tracked properly as side-effects...

@Amareis
Copy link
Contributor Author

Amareis commented Oct 23, 2020

It's really strange, since on node 14 at my computer main is working as supposed. I'll dive into it later.

@lukastaegert
Copy link
Member

Ok, I managed to fix your example and yes, there is an issue, but no, it is not about import chain simplification. Because all imports are still in place, Rollup just "fixes" the order. And by fixing I mean: Sorting them by their (correct!) execution order. But you are right, with circular dependencies this sorting is actually breaking our neck because index, even though it is the first in execution order, has other dependencies which will be executed before index if it is the first import in main. Now this requires some though...

@lukastaegert
Copy link
Member

I actually managed to set up a patch that will fix or rather replace the sorting with something that should handle circular dependencies well: #3840

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

No branches or pull requests

2 participants