Skip to content

fix(cjs): recompute a re-export whose required module was missing - #22631

Merged
alexander-akait merged 1 commit into
webpack:mainfrom
aryanraj45:fix/cjs-reexport-restored-target
Oct 12, 2026
Merged

alexander-akait merged 1 commit into
webpack:mainfrom
aryanraj45:fix/cjs-reexport-restored-target

Conversation

@aryanraj45

@aryanraj45 aryanraj45 commented Oct 12, 2026 •

Copy link
Copy Markdown
Member

Summary

Found by the rebuild fuzzer. While ./dep was missing, exports.r = require("./dep").a and module.exports = require("./dep") reported no exports, which left the module's provided exports cacheable. When ./dep came back the module itself had not changed, so watch restored its exports without the re-export:

// barrel.js: export * from "./cjs"; cjs.js: exports.r = require("./dep").a; exports.b = 1
// delete dep.js, then restore it
before: Object.keys(barrel) → ["b"]        after: ["b", "r"]

In production module.exports = require("./dep") lost every export (/* unused reexport */). A missing target now reports an uncacheable empty export set, as #22432 does for ESM. A sweep of 5 CommonJS shapes × ESM/CommonJS targets × 3 importers × 3 delete/restore orders × 2 modes fails 153 of 180 on main and 0 with this change.

What kind of change does this PR introduce?

fix

Did you add tests for your changes?

Yes: watchCases/recover-from-error/restored-commonjs-reexport, memory and filesystem cache; reverting either changed line fails it.

Does this PR introduce a breaking change?

No

If relevant, what needs to be documented once your changes are merged or what have you already documented?

n/a

Use of AI

I used Claude Code to find the bug with a rebuild fuzzer, write the fix and the tests. I reviewed every change myself.

Summary by CodeRabbit

  • Bug Fixes
    • Improved recovery of CommonJS re-exports after missing dependencies are restored, including named and whole-module re-exports.
  • Tests
    • Added coverage for recovery across watch updates with both memory and filesystem caches.

`exports.x = require("./m").y` and `module.exports = require("./m")` gave no exports while `./m` was missing, which flagged the module cacheable. Once `./m` came back the module had not changed, so its exports were restored without the re-export and an `export *` of it lost the name. Report them uncacheable, as webpack#22432 did for ESM.
@changeset-bot

changeset-bot Bot commented Oct 12, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 29429c1

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions github-actions Bot added the area: parser JavaScript parsing and dependency analysis (lib/javascript, lib/dependencies) label Oct 12, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Reported by Types Coverage #1265 for 29429c1, computed by this pull request's own code.

Types Coverage

Coverage after merging fix/cjs-reexport-restored-target into main will be
97.29%
Coverage Report
FileStmtsBranchesFuncsLinesUncovered Lines
bin
   webpack.js98.82%100%100%98.82%103
declarations
   WebpackOptions.ts99.94%100%100%99.94%4188
examples
   build-common.js100%100%100%100%
   buildAll.js100%100%100%100%
   examples.js100%100%100%100%
   template-common.js98.21%100%100%98.21%72
examples/custom-javascript-parser
   test.filter.js100%100%100%100%
examples/custom-javascript-parser/internals
   acorn-parse.js100%100%100%100%
   meriyah-parse.js100%100%100%100%
   oxc-parse.js100%100%100%100%
examples/markdown
   webpack.config.mjs100%100%100%100%
examples/module-federation
   test.filter.js100%100%100%100%
examples/reexport-components
   test.filter.js100%100%100%100%
examples/ssr
   test.filter.js100%100%100%100%
examples/typescript
   test.filter.js100%100%100%100%
examples/typescript-non-erasable
   test.filter.js50%100%100%50%5
examples/virtual-modules
   test.filter.js100%100%100%100%
examples/wasm-bindgen-esm
   test.filter.js100%100%100%100%
examples/wasm-complex
   test.filter.js100%100%100%100%
examples/wasm-emscripten
   test.filter.js100%100%100%100%
examples/wasm-simple
   test.filter.js100%100%100%100%
examples/wasm-simple-source-phase
   test.filter.js100%100%100%100%
lib
   Chunk.js100%100%100%100%
   Compilation.js98.60%100%100%98.60%1757, 2078, 2085, 2093, 2115, 2118, 23, 3075, 3578–3579, 3611, 4200, 4204, 4331, 4352, 4477, 4514, 4521, 4574–4575, 4579, 4584, 4600–4601, 4615–4616, 4621–4622, 5145, 5171, 6001, 6018, 6036, 6052, 6067, 6092, 6095, 6463, 6466–6467, 6472, 6487, 6570, 6594, 6708, 837–838
   Compiler.js98.81%100%100%98.81%1116, 1153, 1156, 1336–1337, 1345, 848, 934, 975
   Dependency.js100%100%100%100%
   DependencyTemplate.js100%100%100%100%
   EntryPlugin.js100%100%100%100%
   Entrypoint.js100%100%100%100%
   ExternalsPlugin.js100%100%100%100%
   FileSystemInfo.js100%100%100%100%
   InitFragment.js100%100%100%100%
   LibraryTemplatePlugin.js100%100%100%100%
   ModuleFilenameHelpers.js100%100%100%100%
   ModuleNotFoundError.js100%100%100%100%
   MultiCompiler.js98.88%100%100%98.88%460, 532, 719, 784
   NormalModule.js100%100%100%100%
   NullFactory.js100%100%100%100%
   ProgressPlugin.js100%100%100%100%
   RawModule.js100%100%100%100%
   RequestShortener.js100%100%100%100%
   SingleEntryPlugin.js100%100%100%100%
   WebpackError.js100%100%100%100%
   index.js99.78%100%100%99.78%206
   webpack.js97.84%100%100%97.84%278, 300, 302
lib/asset
   AssetBytesGenerator.js100%100%100%100%
   AssetBytesParser.js100%100%100%100%
   AssetGenerator.js100%100%100%100%
   AssetModule.js100%100%100%100%
   AssetModulesPlugin.js98.14%100%100%98.14%337, 361, 364, 494, 56, 61
   AssetParser.js100%100%100%100%
   AssetSourceGenerator.js100%100%100%100%
   AssetSourceParser.js100%100%100%100%
   RawDataUrlModule.js100%100%100%100%
   WebManifestGenerator.js100%100%100%100%
   WebManifestParser.js100%100%100%100%
lib/async-modules
   AsyncModuleHelpers.js100%100%100%100%
   AwaitDependenciesInitFragment.js100%100%100%100%
   InferAsyncModulesPlugin.js100%100%100%100%
   isGeneratorLowered.js100%100%100%100%
lib/bun
   BunTargetPlugin.js100%100%100%100%
lib/cache
   AddBuildDependenciesPlugin.js100%100%100%100%
   AddManagedPathsPlugin.js100%100%100%100%
   Cache.js96.49%100%100%96.49%10, 104
   CacheFacade.js100%100%100%100%
   IdleFileCachePlugin.js97.92%100%100%97.92%75, 87, 95
   MemoryCachePlugin.js92%100%100%92%33, 42
   MemoryWithGcCachePlugin.js93.42%100%100%93.42%106, 120–121, 130, 88
   PackFileCacheStrategy.js96.54%100%100%96.54%1304, 1404, 1408, 1470, 1716, 1800, 1823, 1853, 682, 701, 711–713, 715, 731–732, 737, 740, 742, 747, 752, 777, 783, 817, 823, 829, 834, 845, 854, 859–860, 862, 879, 885–886, 888
   ResolverCachePlugin.js100%100%100%100%
   getLazyHashedEtag.js100%100%100%100%
   mergeEtags.js100%100%100%100%
lib/config
   OptionsApply.js100%100%100%100%
   PlatformPlugin.js100%100%100%100%
   WebpackOptionsApply.js100%100%100%100%
   WebpackOptionsDefaulter.js100%100%100%100%
   browserslistTargetHandler.js100%100%100%100%
   cli.js98.61%100%100%98.61%116, 546, 578, 628, 9
   defaults.js99.44%100%100%99.44%1624–1626, 1634, 283, 286, 291, 295
   defineConfig.js100%100%100%100%
   getClaimedAssetTypes.js100%100%100%100%
   normalization.js99.12%100%100%99.12%279–280, 346, 361
   target.js100%100%100%100%
   validateSchema.js94.67%100%100%94.67%100, 87, 89, 98
lib/container
   ContainerEntryDependency.js100%100%100%100%
   ContainerEntryModule.js100%100%100%100%
   ContainerEntryModuleFactory.js100%100%100%100%
   ContainerExposedDependency.js100%100%100%100%
   ContainerPlugin.js100%100%100%100%
   ContainerReferencePlugin.js100%100%100%100%
   FallbackDependency.js100%100%100%100%
   FallbackItemDependency.js100%100%100%100%
   FallbackModule.js100%100%100%100%
   FallbackModuleFactory.js100%100%100%100%
   HoistContainerReferencesPlugin.js100%100%100%100%
   ModuleFederationPlugin.js100%100%100%100%
   RemoteModule.js100%100%100%100%
   RemoteRuntimeModule.js100%100%100%100%
   RemoteToExternalDependency.js100%100%100%100%
   declaredRemotes.js100%100%100%100%
   moduleFederationHooks.js100%100%100%100%
   options.js100%100%100%100%
lib/context
   ContextExclusionPlugin.js100%100%100%100%
   ContextModule.js99.88%100%100%99.88%1495
   ContextModuleFactory.js95.70%100%100%95.70%230, 257, 276, 280–281, 289, 466, 487, 492, 534, 543, 545, 549, 552, 558–559
   ContextReplacementPlugin.js100%100%100%100%
lib/css
   CssGenerator.js99.29%100%100%99.29%1092, 134, 301, 849
   CssInjectStyleRuntimeModule.js100%100%100%100%
   CssLoadingRuntimeModule.js100%100%100%100%
   CssModule.js83.33%100%100%83.33%197, 202–210, 212–217
   CssModulesPlugin.js99.86%100%100%99.86%884
   CssParser.js99.48%100%100%99.48%1044, 1075, 24, 2406, 3053, 3056, 3062, 3103, 3106, 3145, 3148, 3485, 5039, 5042, 928
   CssServerStylesRuntimeModule.js100%100%100%100%
   conventions.js98.65%100%100%98.65%93
   cssMinify.js97.50%100%100%97.50%162, 171
   data.js99.84%100%100%99.84%229, 5601
   syntax-parser.js99.65%100%100%99.65%10258–10259, 10793–10794, 10831, 11026, 11072, 13388–13391, 13491–13492, 13589–13590, 14235, 14532, 14534, 15024, 15390, 17500, 2596, 3295, 3297, 3302, 3321, 4066, 4072, 7544, 7582, 7589
   syntax-printer.js99.31%100%100%99.31%2548–2549, 2551–2552, 2554–2555, 2560, 2827–2830, 2923, 3596, 370, 4016, 4125, 4813, 5114, 5900, 738
   syntax.js100%100%100%100%
   topologicalSort.js100%100%100%100%
lib/debug
   ProfilingPlugin.js96.22%100%100%96.22%169, 425, 554, 561, 570, 574, 583, 588, 594, 599, 92
lib/define
   DefinePlugin.js98.85%100%100%98.85%1495, 268–269, 285, 304, 378, 936, 938, 943, 967
   DotenvPlugin.js98.07%100%100%98.07%391, 439, 452–453
   EnvironmentPlugin.js97.14%100%100%97.14%49
lib/deno
   DenoTargetPlugin.js100%100%100%100%
lib/dependencies
   ConstDependency.js100%100%100%100%
   ContextElementDependency.js100%100%100%100%
   NullDependency.js100%100%100%100%
lib/dependencies/amd
   AMDDefineDependency.js100%100%100%100%
   AMDDefineDependencyParserPlugin.js100%100%100%100%
   AMDPlugin.js99.31%100%100%99.31%54
   AMDRequireArrayDependency.js100%100%100%100%
   AMDRequireContextDependency.js100%100%100%100%
   

@coderabbitai

coderabbitai Bot commented Oct 12, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Repository: webpack/webpack/.coderabbit.yaml
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 392817b3-c8da-42a0-811e-c4bcccc84592

📥 Commits

Reviewing files that changed from the base of the PR and between d5d84cd and 29429c1.


📒 Files selected for processing (21)
  • lib/dependencies/commonjs/CommonJsExportRequireDependency.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/errors.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/index.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/later-barrel.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/later.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/name-barrel.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/name-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/name.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/whole-barrel.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/whole-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/0/whole.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/1/errors.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/1/later-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/1/name-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/1/whole-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/2/errors.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/2/later-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/2/name-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/2/whole-dep.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/test.config.js
  • test/watchCases/recover-from-error/restored-commonjs-reexport/webpack.config.js

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.



📝 Walkthrough

Walkthrough

getExports now returns empty export and dependency lists when a named or whole-module CommonJS re-export has no module-graph connection. A watch case adds coverage for re-export behavior as dependencies are removed and restored.

Changes

CommonJS re-export recovery

Layer / File(s) Summary
Disconnected re-export results
lib/dependencies/commonjs/CommonJsExportRequireDependency.js
getExports returns empty exports and dependencies arrays when a named or whole-module re-export has no module-graph connection.
Watch-case recovery coverage
test/watchCases/recover-from-error/restored-commonjs-reexport/*
The watch case checks named and whole-module re-exports across WATCH_STEP states. Its fixtures cover missing and restored dependencies, expected errors, and memory and filesystem cache configurations.

Suggested labels: area: parser


Priority: ➖ Normal

Change: Bug fix

Merge Risk: ⚪ Minimal · up to 29429

The change allows missing CommonJS re-exports to recover when dependencies return, and the added watch test checks the corresponding fixture states. No actionable merge risk remains.

Pre-merge checks | Passed 4
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check Passed The title uses the required Conventional Commit form, fix(cjs): subject, and accurately describes the CommonJS re-export fix. The fix type is allowed; branch-prefix alignment cannot be independent…
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@codecov

codecov Bot commented Oct 12, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 96.26%. Comparing base (d5d84cd) to head (29429c1).
⚠️ Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main   #22631   +/-   ##
=======================================
  Coverage   96.26%   96.26%           
=======================================
  Files         774      774           
  Lines      129796   129796           
  Branches    43189    43189           
=======================================
+ Hits       124947   124948    +1     
+ Misses       4849     4848    -1     
Flag Coverage Δ
css-parsing 25.09% <0.00%> (ø)
html5lib 27.01% <0.00%> (ø)
integration 82.25% <100.00%> (+<0.01%) ⬆️
minify-corpora 79.68% <ø> (ø)
syntax-equivalence 79.13% <ø> (ø)
test262 57.11% <0.00%> (ø)
test262-parser 83.01% <ø> (ø)
unit 75.58% <50.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@github-actions

Copy link
Copy Markdown
Contributor

Reported by Code Size #4503 for 29429c1, computed by this pull request's own code.

Generated code size

Comparing base (d5d84cd) to head (29429c1). Merging this pull request will not change the code webpack generates.

What moved Tests Assets Gzip Raw Test edit
Changed, test untouched 0 0 — — —
Changed, test edited 0 0 — — —
New 0 0 — — —
Deleted 0 0 — — —
Unchanged — 8609 — — —

Read Changed, test untouched first: the test is byte-identical on both sides, so webpack generated the difference. Changed, test edited moved partly because the test did — Test edit is how many bytes the test gained, and a bundle that grew by less is not a regression. New and Deleted are whole files, not deltas — a new asset was created, not grown, so it reads gray (⚪ added) — and adding tests cannot bury a real change. Gzip decides, because that is what users download; raw is the tiebreak, and brotli and zstd are per asset below.

No asset changed size.

No runtime gained or lost a runtime module.

Built test/configCases with the defaults a user gets: 2218 test(s), 8609 asset(s), 99 emitted nothing.

@codspeed

codspeed Bot commented Oct 12, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 68.42%

⚠️ 108 benchmarks spent significant time in system calls

System calls cannot be consistently instrumented, so they are not included in the measure, which understates the real cost. Please switch to the Walltime instrument to accurately measure system calls.

Measurement and system calls

ℹ️ 29 benchmarks have no flame graph

The flamegraph could not be generated because too much time was spent in runtime internals, making the visualization not useful for performance analysis.

⚡ 1 improved benchmark
❌ 1 regressed benchmark
✅ 404 untouched benchmarks
⏩ 6 skipped benchmarks1

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Mode Benchmark BASE HEAD Efficiency
❌ Memory unit benchmark "html-parser-document-unit", parseHtml (skip text) 56.3 KB 681.1 KB -91.73%
⚡ Memory benchmark "concatenate-modules", scenario '{"name":"mode-development-rebuild","mode":"development","watch":true}' 565.4 KB 468.7 KB +20.63%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing aryanraj45:fix/cjs-reexport-restored-target (29429c1) with main (d5d84cd)

Open in CodSpeed

Footnotes

  1. 6 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. ↩

@alexander-akait
alexander-akait merged commit b19519a into webpack:main Oct 12, 2026
67 checks passed
@github-actions

Copy link
Copy Markdown
Contributor

This PR is packaged and the instant preview is available (b19519a).

Install it locally:

  • npm
npm i -D webpack@https://pkg.pr.new/webpack@b19519a
  • yarn
yarn add -D webpack@https://pkg.pr.new/webpack@b19519a
  • pnpm
pnpm add -D webpack@https://pkg.pr.new/webpack@b19519a

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

Labels

area: parser JavaScript parsing and dependency analysis (lib/javascript, lib/dependencies)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants