Skip to content

fix(rstest): prevent the re-exports optimization for mocked modules#13262

Merged
9aoy merged 7 commits intomainfrom
mock-with-re-export
Mar 12, 2026
Merged

fix(rstest): prevent the re-exports optimization for mocked modules#13262
9aoy merged 7 commits intomainfrom
mock-with-re-export

Conversation

@9aoy
Copy link
Copy Markdown
Contributor

@9aoy 9aoy commented Mar 9, 2026

Summary

When modules are marked as side-effect-free, Rspack may inline re-export chains and skip intermediate modules. That can break rs.mock() / rs.doMock() when users mock the intermediate package name (the mocked module ID is no longer the one consumers execute).

This change marks mocked modules as non-side-effect-free during optimize_dependencies, so module boundaries are preserved and mocks reliably hit the intended module IDs.

  • Added an optimize_dependencies hook in RstestPlugin (stage = -1000) to run before side-effects optimization.
  • Collected all modules referenced by DependencyType::RstestMockModuleId (covers both rs.mock and rs.doMock).
  • For those modules, if factory_meta.side_effect_free == Some(true), override to Some(false).

Related links

fix web-infra-dev/rstest#972

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

Copilot AI review requested due to automatic review settings March 9, 2026 09:56
@9aoy 9aoy requested a review from LingyuCoder as a code owner March 9, 2026 09:56
@github-actions github-actions bot added release: bug fix release: bug related release(mr only) team The issue/pr is created by the member of Rspack. labels Mar 9, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

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 an rstest mocking edge case where side-effects optimization can inline re-export chains and skip intermediate modules, causing rs.mock() / rs.doMock() to miss the intended module IDs. It does so by ensuring modules referenced by rstest mock-module-id dependencies are treated as non-side-effect-free during optimize_dependencies, and adds regression tests covering re-export chains.

Changes:

  • Add an early CompilationOptimizeDependencies hook in RstestPlugin to mark mocked modules as not side-effect-free.
  • Add new rstest config-case entries and test specs for mocking through single and multi-hop re-export chains.
  • Add local node_modules/* fixtures (reexport-top, reexport-intermediate, reexport-source) to reproduce sideEffects-driven re-export inlining.

Reviewed changes

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

Show a summary per file
File Description
tests/rspack-test/configCases/rstest/mock/rspack.config.js Adds new rstest entries to run the added re-export mocking regression tests.
tests/rspack-test/configCases/rstest/mock/reExportMockedModule.js New regression test: rs.mock() against a side-effect-free re-exported package.
tests/rspack-test/configCases/rstest/mock/reExportDoMockedModule.js New regression test: rs.doMock() against a side-effect-free re-exported package.
tests/rspack-test/configCases/rstest/mock/reExportTripleMockedModule.js New regression test: rs.mock() across A→B→C re-export chain.
tests/rspack-test/configCases/rstest/mock/reExportTripleDoMockedModule.js New regression test: rs.doMock() across A→B→C re-export chain.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-top/package.json Fixture package marked sideEffects: false to trigger side-effects optimization behavior.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-top/index.js Fixture top package that re-exports from reexport-intermediate.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-intermediate/package.json Fixture package marked sideEffects: false to trigger re-export inlining.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-intermediate/index.js Fixture intermediate package that re-exports from reexport-source.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-source/package.json Fixture “real implementation” package metadata.
tests/rspack-test/configCases/rstest/mock/node_modules/reexport-source/index.js Fixture “real implementation” export.
crates/rspack_plugin_rstest/src/plugin.rs Adds optimize_dependencies hook to flip side-effect-free flags for mocked modules.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

You can also share your feedback on Copilot code review. Take the survey.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 9, 2026

📦 Binary Size-limit

Comparing 88223d5 to perf: remove ukey collections (#13309) by harpsealjs

❌ Size increased by 4.88KB from 48.99MB to 49.00MB (⬆️0.01%)

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 9, 2026

Rsdoctor Bundle Diff Analysis

⚠️ Note: The latest commit (3a5fc54ceb) does not have baseline artifacts. Using commit 9f580f5543 for baseline comparison instead. If this seems incorrect, please wait a few minutes and try rerunning the workflow.

Found 5 projects in monorepo, 0 projects with changes.

📊 Quick Summary
Project Total Size Change
react-10k 5.7 MB 0
react-1k 826.2 KB 0
react-5k 2.7 MB 0
rome 984.2 KB 0
ui-components 2.3 MB 0

Generated by Rsdoctor GitHub Action

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq bot commented Mar 9, 2026

Merging this PR will not alter performance

✅ 16 untouched benchmarks
⏩ 3 skipped benchmarks1


Comparing mock-with-re-export (88223d5) with main (9f580f5)

Open in CodSpeed

Footnotes

  1. 3 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@cloudflare-workers-and-pages
Copy link
Copy Markdown

cloudflare-workers-and-pages bot commented Mar 12, 2026

Deploying rspack with  Cloudflare Pages  Cloudflare Pages

Latest commit: 1c2bde9
Status: ✅  Deploy successful!
Preview URL: https://a5de4d39.rspack-v2.pages.dev
Branch Preview URL: https://mock-with-re-export.rspack-v2.pages.dev

View logs

@github-actions
Copy link
Copy Markdown
Contributor

github-actions bot commented Mar 12, 2026

📝 Ecosystem CI detail: Open

suite result
rstest ✅ success
plugin ✅ success
rslib ✅ success
rsdoctor ❌ failure
nuxt ❌ failure
devserver ❌ failure
rspress ❌ failure
rsbuild ✅ success
lynx-stack ❌ failure
examples ❌ failure
modernjs ❌ failure

@9aoy 9aoy enabled auto-merge (squash) March 12, 2026 08:13
@9aoy 9aoy merged commit 802fca1 into main Mar 12, 2026
74 of 76 checks passed
@9aoy 9aoy deleted the mock-with-re-export branch March 12, 2026 08:17
@web-infra-dev web-infra-dev deleted a comment from github-actions bot Mar 18, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

release: bug fix release: bug related release(mr only) team The issue/pr is created by the member of Rspack.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature]: rs.mock() should propagate through re-exported / inlined modules

3 participants