Skip to content

fix: sort dynamic entires with topological order before tree shake. - #4694

Merged
IWANABETHATGUY merged 2 commits into
mainfrom
05-27-fix_4682
May 27, 2025
Merged

IWANABETHATGUY merged 2 commits into
mainfrom
05-27-fix_4682

Conversation

@IWANABETHATGUY

@IWANABETHATGUY IWANABETHATGUY commented May 27, 2025 •

Copy link
Copy Markdown
Member

@netlify

netlify Bot commented May 27, 2025 •

Copy link
Copy Markdown

✅ Deploy Preview for rolldown-rs canceled.

Name Link
🔨 Latest commit 1667770
🔍 Latest deploy log https://app.netlify.com/projects/rolldown-rs/deploys/6835914b2fddc30008ac0ef7

IWANABETHATGUY commented May 27, 2025 •

Copy link
Copy Markdown
Member Author

@IWANABETHATGUY
IWANABETHATGUY force-pushed the 05-27-fix_4682 branch 2 times, most recently from e4336a7 to 8ad35d5 Compare May 27, 2025 09:55
@IWANABETHATGUY IWANABETHATGUY changed the title fix: 4682 fix: sort dynamic entires with topological order before tree shake. May 27, 2025
@github-actions

github-actions Bot commented May 27, 2025 •

Copy link
Copy Markdown
Contributor

Benchmarks Rust

group                                                               pr                                     target
-----                                                               --                                     ------
bundle/bundle@multi-duplicated-top-level-symbol                     1.01     80.0±3.92ms        ? ?/sec    1.00     78.9±2.19ms        ? ?/sec
bundle/bundle@multi-duplicated-top-level-symbol-minify              1.00     95.1±1.78ms        ? ?/sec    1.04     98.9±3.50ms        ? ?/sec
bundle/bundle@multi-duplicated-top-level-symbol-minify-sourcemap    1.00    111.3±2.88ms        ? ?/sec    1.00    111.8±1.40ms        ? ?/sec
bundle/bundle@multi-duplicated-top-level-symbol-sourcemap           1.00     85.7±2.25ms        ? ?/sec    1.04     89.3±2.70ms        ? ?/sec
bundle/bundle@rome-ts                                               1.00    119.2±1.80ms        ? ?/sec    1.00    118.7±1.68ms        ? ?/sec
bundle/bundle@rome-ts-minify                                        1.00    186.9±1.53ms        ? ?/sec    1.02    191.5±3.41ms        ? ?/sec
bundle/bundle@rome-ts-minify-sourcemap                              1.05    231.7±1.05ms        ? ?/sec    1.00    219.8±3.01ms        ? ?/sec
bundle/bundle@rome-ts-sourcemap                                     1.00    129.8±1.10ms        ? ?/sec    1.01    131.3±2.20ms        ? ?/sec
bundle/bundle@threejs                                               1.02     40.4±0.42ms        ? ?/sec    1.00     39.8±1.67ms        ? ?/sec
bundle/bundle@threejs-minify                                        1.13     92.2±4.26ms        ? ?/sec    1.00     81.7±0.77ms        ? ?/sec
bundle/bundle@threejs-minify-sourcemap                              1.16    112.2±2.88ms        ? ?/sec    1.00     96.5±1.55ms        ? ?/sec
bundle/bundle@threejs-sourcemap                                     1.05     49.2±1.29ms        ? ?/sec    1.00     46.9±0.84ms        ? ?/sec
bundle/bundle@threejs10x                                            1.04    442.8±8.83ms        ? ?/sec    1.00    425.7±6.21ms        ? ?/sec
bundle/bundle@threejs10x-minify                                     1.02  1042.7±14.36ms        ? ?/sec    1.00   1017.5±5.46ms        ? ?/sec
bundle/bundle@threejs10x-minify-sourcemap                           1.02   1214.6±6.18ms        ? ?/sec    1.00   1187.2±7.46ms        ? ?/sec
bundle/bundle@threejs10x-sourcemap                                  1.01    494.4±7.65ms        ? ?/sec    1.00    491.0±5.37ms        ? ?/sec
remapping/remapping                                                 1.00     26.6±0.67ms        ? ?/sec    1.03     27.3±1.13ms        ? ?/sec
remapping/render-chunk-remapping                                    1.04     69.9±5.01ms        ? ?/sec    1.00     67.5±3.24ms        ? ?/sec
scan/scan@rome-ts                                                   1.00     89.3±0.82ms        ? ?/sec    1.04     93.0±2.54ms        ? ?/sec
scan/scan@threejs                                                   1.00     29.9±0.27ms        ? ?/sec    1.03     30.8±1.39ms        ? ?/sec
scan/scan@threejs10x                                                1.00    305.5±3.20ms        ? ?/sec    1.02    310.9±3.88ms        ? ?/sec

@IWANABETHATGUY
IWANABETHATGUY marked this pull request as ready for review May 27, 2025 10:12
Copilot AI review requested due to automatic review settings May 27, 2025 10:12

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes the tree shaking issue by sorting dynamic entries using topological order with petgraph and tarjan_scc, ensuring that each dynamic entry is included before its descendant dynamic entries. Additionally, snapshot tests and dependency adjustments have been updated to support the new ordering mechanism.

  • Updated cspell.json to add new terms
  • Modified tests snapshots for dynamic/static module integration
  • Enhanced dynamic entry ordering in include_statements.rs via a new topological sort and dynamic entry graph construction
  • Added petgraph dependency in Cargo.toml and adjusted README accordingly

Reviewed Changes

Copilot reviewed 14 out of 14 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
cspell.json Added new spellings ("petgraph", "tarjan") to the list
crates/rolldown/tests/snapshots/integration_rolldown__filename_with_hash.snap Updated snapshot to reflect new dynamic entry ordering
crates/rolldown/tests/rolldown/tree_shaking/issue_4682/*.js Added additional test modules for dynamic and static tree shaking
crates/rolldown/src/stages/link_stage/tree_shaking/include_statements.rs Introduced topological sorting of dynamic entries and cycle detection
Cargo.toml and crates/rolldown/Cargo.toml Added petgraph dependency
README.md Minor documentation update by removing placeholder comments
Comments suppressed due to low confidence (1)

crates/rolldown/src/stages/link_stage/tree_shaking/include_statements.rs:95

  • The variable name 'cycled_idx' is ambiguous as it represents a set of dynamic entries involved in cycles. Consider renaming it to 'cycled_entries' for clarity.
let cycled_idx = self.get_dynamic_entries_included_order(&mut dynamic_entries);

@IWANABETHATGUY
IWANABETHATGUY requested a review from hyfdev May 27, 2025 10:17
@IWANABETHATGUY
IWANABETHATGUY added this pull request to the merge queue May 27, 2025
Merged via the queue into main with commit bc96622 May 27, 2025
@IWANABETHATGUY
IWANABETHATGUY deleted the 05-27-fix_4682 branch May 27, 2025 10:33
hyfdev added a commit that referenced this pull request Nov 19, 2026
Modules reached only by dead `import()` calls are still emitted when they form a cycle through those calls.

This PR drops them, as rolldown already does when there is no cycle.

```js
// entry.mjs
import * as SDK from './sdk.mjs';
console.log(typeof SDK.used);

// sdk.mjs
export function used() { return 1; }
export function deadA() { return import('./a.mjs'); }
export function deadB() { return import('./b.mjs'); }

// a.mjs (b.mjs is the same, with `b`)
import { used } from './sdk.mjs';
export function a() { return used(); }
```

By default, main also emits `a-*.js` and `b-*.js`, which nothing imports, and `entry.js` gets an extra export for them.

With `output: { file: 'out.js', codeSplitting: false }`, main fails with `When building multiple chunks, the "output.dir" option must be used`.

With this PR, both builds emit only the entry file, which prints `function`.

Tree-shaking keeps every dynamic import target in an `import()` cycle without checking whether an `import()` that reaches it is still included.

That exception dates from #4694, when dynamic entries were checked in a single pass.

This PR removes it.

A dynamic entry is now checked again each time tree-shaking keeps a statement with an `import()` of it.

In a cycle, a top-level `import()` whose result is unused still keeps its target, as on `main`.

Dropping it can delay when the target runs.

Rollup 4.68.2 also emits one file in both modes.

The example in the issue body already builds to one file on main.

Fixes #11001
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: no entry found for key panic at crates\rolldown\src\module_finalizers\scope_hoisting\impl_visit_mut.rs:422:85

3 participants