Incorrect order of declarations after bundling (ReferenceError: Cannot access 'X' before initialization)
Rollup Version
4.46.2
Operating System (or Browser)
Ubuntu
Node Version (if applicable)
18.19.1
Link To Reproduction
Expected Behaviour
Rollup 4.46.2 struggles to properly order declarations. I think it's a bundler's job to correctly resolve the order of dependencies, that's why I decided to open this issue. Here's the simplest example I could come up with:
Contents of entry.js:
import { start } from "./declare"
start()Contents of declare.js:
import { num } from "./use"
export class Num {
constructor(value) {}
}
export function start() {
console.log(num)
}Contents of use.js:
import { Num } from "./declare"
export const num = new Num(0)The correct order could be:
class Num {
constructor(value) {}
}
const num = new Num(0);
function start() {
console.log(num);
}
start();Actual Behaviour
Rollup's output:
$ pnpx rollup -c
entry.js → stdout...
const num = new Num(0);
class Num {
constructor(value) {}
}
function start() {
console.log(num);
}
start();
(!) Circular dependency
declare.js -> use.js -> declare.jsTo which browser throws an exception: Uncaught (in promise) ReferenceError: Cannot access 'Num' before initialization.
Surprisingly, I had used Rollup in a 40K LoC monorepo with lots of circular-dependencies and has never had any issues in 1 year of using Rollup. So it's the first time I had caught such a case of incorrectly ordered dependencies. Maybe it's a regression in newer versions? Or maybe I just kept getting lucky all this time.
If it's not a regression then how difficult would it be to implement a fix for cases like that?
Source: rollup/rollup