Skip to content

[Bug]: modules that are sideeffect free but relies on the execution order is not wrapped #5303

@sapphi-red

Description

@sapphi-red

Reproduction link or steps

  1. Open https://stackblitz.com/edit/rolldown-rolldown-starter-stackblitz-kahhkybh?file=rolldown.config.mjs
  2. pnpm i runs automatically
  3. pnpm build runs automatically
  4. node src/entry.js does not throw an error
  5. node dist/entry.js throws an error

What is expected?

No error happens after build.

What is actually happening?

The following error happens after build:

ReferenceError: globalValue is not defined

System Info

StackBlitz

  System:
    OS: Linux 5.0 undefined
    CPU: (8) x64 Intel(R) Core(TM) i9-9880H CPU @ 2.30GHz
    Memory: 0 Bytes / 0 Bytes
    Shell: 1.0 - /bin/jsh
  Binaries:
    Node: 20.19.1 - /usr/local/bin/node
    Yarn: 1.22.19 - /usr/local/bin/yarn
    npm: 10.8.2 - /usr/local/bin/npm
    pnpm: 8.15.6 - /usr/local/bin/pnpm
  npmPackages:
    rolldown: latest => 1.0.0-beta.27

Any additional comments?

#__PURE__ only means "the code can be safely removed if not used". It does not mean "the code does not rely on the execution order".

Specifically, this line should be checking whether this module relies on the execution order (e.g. reading a global variable), not whether it has a side effect.

if !module.meta.contains(EcmaViewMeta::HAS_ANALYZED_SIDE_EFFECT)

This is because even if a module doesn't have a side effect (e.g. writing a global variable, console logging), it can rely on the side effect that is caused by other modules. So those modules should be wrapped to keep the execution order.

That said, in most cases "the code can be safely removed if not used" and "the code does not rely on the execution order" are equivalent. So this is an edge case. If someone encounters this in real world code, please comment on this issue.

related: #5240, #4684

Metadata

Metadata

Type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions