Skip to content

Detect variable reassignments in modules without side effects #5658

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

Merged
merged 1 commit into from
Sep 19, 2024

Conversation

lukastaegert
Copy link
Member

This PR contains:

  • bugfix
  • feature
  • refactor
  • documentation
  • other

Are tests included?

  • yes (bugfixes and features will not be merged without tests)
  • no

Breaking Changes?

  • yes (breaking changes will not be merged unless absolutely necessary)
  • no

List any relevant issue numbers:

Description

The bug was quite intricate:

  • a mutable export from a module is used that is marked with moduleSideEffects: false
  • it is used in a place where its initial value can be used to simplify an expression in such a way that the value is no longer needed
  • then neither the value nor the code that changes the value are included as the module is marked as having no side effects and no import is included

This took me quite some thinking to fix. The problem is that while the contract of moduleSideEffects: false means that we check the module for side-effectful code if at least one export is used, there are situations in Rollup where we "use" an export without including it in the bundle.
To mitigate this, accessing an identifier during tree-shaking will now always mark the module as "potentially included", overriding the moduleSideEffects flag, because obviously we used a variable of that module.
To not break a previous fix around TDZ detection, I needed to rework this slightly as well: Now modules are marked when they receive a proper tree-shaking pass and if we use a variable from a module that did not receive a pass, we do not perform TDZ detection for that variable as the logic would be faulty.

Copy link

vercel bot commented Sep 18, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
rollup ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 18, 2024 7:01am

Copy link

github-actions bot commented Sep 18, 2024

Thank you for your contribution! ❤️

You can try out this pull request locally by installing Rollup via

npm install rollup/rollup#gh-5650_treeshake_module_side_effects_bug

Notice: Ensure you have installed the latest stable Rust toolchain. If you haven't installed it yet, please see https://www.rust-lang.org/tools/install to learn how to download Rustup and install Rust.

or load it into the REPL:
https://rollup-laglr1f0w-rollup-js.vercel.app/repl/?pr=5658

Copy link

github-actions bot commented Sep 18, 2024

Performance report!

Rough benchmark

Command Mean [s] Min [s] Max [s] Relative
node _benchmark/previous/bin/rollup -i ./perf/entry.js -o _benchmark/result/previous.js 9.481 ± 0.092 9.380 9.559 1.01 ± 0.02
node _benchmark/current/bin/rollup -i ./perf/entry.js -o _benchmark/result/current.js 9.389 ± 0.113 9.276 9.502 1.00

Internal benchmark

  • BUILD: 8441ms, 750 MB
    • initialize: 0ms, 26 MB
    • generate module graph: 3231ms, 574 MB
      • generate ast: 1579ms, 567 MB
    • sort and bind modules: 462ms, 617 MB
    • mark included statements: 4752ms, 750 MB
      • treeshaking pass 1: 1608ms, 716 MB
      • treeshaking pass 2: 775ms, 740 MB
      • treeshaking pass 3: 304ms, 747 MB
      • treeshaking pass 4: 281ms, 747 MB
      • treeshaking pass 5: 327ms, 748 MB
      • treeshaking pass 6: 269ms, 750 MB
      • treeshaking pass 7: 252ms, 753 MB
      • treeshaking pass 8: 244ms, 753 MB
      • treeshaking pass 9: 223ms, 755 MB
      • treeshaking pass 10: 225ms, 754 MB
      • treeshaking pass 11: 222ms, 750 MB
  • GENERATE: 924ms, 1.02 GB
    • initialize render: 0ms, 914 MB
    • generate chunks: 89ms, 917 MB
      • optimize chunks: 0ms, 917 MB
    • render chunks: 810ms, 1 GB
    • transform chunks: 19ms, 1.02 GB
    • generate bundle: 0ms, 1.02 GB

Copy link

codecov bot commented Sep 18, 2024

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 99.39%. Comparing base (8fc1223) to head (da1281b).
Report is 1 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #5658   +/-   ##
=======================================
  Coverage   99.39%   99.39%           
=======================================
  Files         242      242           
  Lines        9345     9348    +3     
  Branches     2470     2470           
=======================================
+ Hits         9288     9291    +3     
  Misses         48       48           
  Partials        9        9           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@lukastaegert lukastaegert force-pushed the gh-5650_treeshake_module_side_effects_bug branch from 0d3627d to da1281b Compare September 18, 2024 06:59
@lukastaegert lukastaegert merged commit 184bc4e into master Sep 19, 2024
39 checks passed
@lukastaegert lukastaegert deleted the gh-5650_treeshake_module_side_effects_bug branch September 19, 2024 04:21
Copy link

This PR has been released as part of rollup@4.22.0. 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
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Treeshaking react-dnd is too eager Setting moduleSideEffects: false removes side effectful getter inside a exported function since 4.9.2
1 participant