Skip to content

CommonJS self-reference crashes usedExports code generation #21619

Closed

Description

@iivvaannxx

Have you used AI?

Yes

Bug Description

Webpack crashes during bundle generation when an unused CommonJS namespace contains a function that reads one of its own exports. The reduced reproduction uses three small source files and has no runtime dependencies.

The error occurs with production usedExports optimization and a side-effect-free package:

Self-reference dependency has unused export name: This should not happen

Link to Minimal Reproduction and step to reproduce

https://stackblitz.com/edit/webpack-self-reference-repro?file=README.md&view=editor

Steps:

  1. Run pnpm install.
  2. Run pnpm run build.
  3. See the error from src/utility.cjs.

Optionally, run pnpm run build:without-tree-shaking and see how it succeeds.

Expected Behavior

Webpack should tree-shake or retain the unused CommonJS namespace and complete the build.

Actual Behavior

Webpack aborts during bundle generation:

ERROR in ./src/utility.cjs
Self-reference dependency has unused export name: This should not happen

Running pnpm run build:without-tree-shaking succeeds.

Environment

System:
  OS: macOS 26.5.1
  CPU: (16) arm64 Apple M4 Max
  Memory: 586.81 MB / 48.00 GB
Binaries:
  Node: 24.16.0 - ~/.local/state/fnm_multishells/55743_1785937995479/bin/node
  Yarn: 1.22.22 - /opt/homebrew/bin/yarn
  npm: 11.16.0 - /opt/homebrew/bin/npm
  pnpm: 11.1.2 - ~/Library/pnpm/bin/pnpm
Browsers:
  Chrome: 151.0.7922.76
  Safari: 26.5
Packages:
  webpack: 5.109.2 => 5.109.2
  webpack-cli: 6.0.1 => 6.0.1

Is this a regression?

No

Last Working Version

N/A

Additional Context

The reproduction also fails with webpack 5.26.3, 5.40.0, 5.50.0, 5.60.0, 5.70.0, 5.75.0, and 5.107.2. Disabling optimization.usedExports makes it succeed.

This may be related to #13983 and #18315, but this case does not use workers or usedExports: "global".

The issue was reduced from an OpenTelemetry package graph, but the final reproduction contains no OpenTelemetry dependency.

AI assistance was used to organize this report. I reviewed the reproduction, commands, and technical details.

Activity

  1. hai-x commented on Aug 6, 2026

    @hai-x
    Member

    Hi, thanks for report, the fixing is WIP.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions