Skip to content

fix: warn in development when two plugins share a name - #5844

Merged
quantizor merged 2 commits into
styled-components:mainfrom
itisrmk:fix/5828-same-name-plugins
Nov 3, 2026
Merged

quantizor merged 2 commits into
styled-components:mainfrom
itisrmk:fix/5828-same-name-plugins

Conversation

@itisrmk

@itisrmk itisrmk commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Fixes #5828

Why a warning and not a new hash input

The issue lists two ways out: hash something unique per plugin, or warn in development when two different plugins share a name. I went with the warning. The class name has to come out identical on the server and the client, and neither of the other inputs can promise that:

  • Object identity: the server and the client never hold the same plugin object.
  • Function source: the two builds often emit different source for the same plugin, and a closure's captured values never show up in the source anyway.

So the hash still uses only each plugin's name (plus namespace), and nothing changes for production output.

What changed

  • src/utils/compiler.ts: in __DEV__ only, createCompiler records the first plugin object seen for each name. When a later, different object arrives with a name already taken, it calls warnOnce (once per name) and says to give each plugin its own name. The map is created lazily inside the dev branch.
  • test-utils/setupTestFramework.ts: clears that registry in beforeEach, next to the existing resetWarnOnce().
  • .changeset/plugin-name-collision.md: patch changeset describing the warning.

One trade-off worth flagging: because the check is identity-based, a plugin that's re-created on every render with the same name also triggers the (single) warning. Hoisting the plugin out of render avoids it, and that's already the better pattern since a new plugins array means a new compiler.

Tests

Added plugins that share a name in src/models/test/StyleSheetManager.test.tsx:

  • warns when two managers get different plugins with the same name (the repro from the issue)
  • warns when one plugins array holds two plugins with the same name
  • warns only once for a repeated name
  • no warning when the same plugin object is reused
  • no warning when the names differ
  • no warning when __DEV__ is false

The warning tests failed before the change and pass after it. Full main jest suite passes (1509 tests), tsc --noEmit and pnpm format:check are clean, and pnpm build passes with bundle size unchanged (15.36kB standalone gzip, 14.23kB webpack gzip). The warning string doesn't appear in styled-components.min.js.

Class names hash only a plugin's name, which has to match on the server and the client, so development builds warn when two different plugins share one.
@changeset-bot

changeset-bot Bot commented Oct 20, 2026 •

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: fd82969

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
styled-components Patch

Not sure what this means? Click here to learn what changesets are.

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

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

ℹ️ One narrow message-accuracy edge case; the implementation is otherwise sound.

Reviewed changes

  • Dev-only collision warning in createCompiler (utils/compiler.ts) — a lazily allocated module-level name→plugin registry calls warnOnce('plugin-name-collision', ...) when a second, different plugin object arrives under a name already seen. The whole path sits behind __DEV__, so production DCEs both the call and the message string.
  • Test reset wiring (test-utils/setupTestFramework.ts) — resetSeenPlugins() joins resetWarnOnce() in the global beforeEach, so the process-wide registry does not leak between tests.
  • Tests + changeset — six cases in models/test/StyleSheetManager.test.tsx (two managers, one array, warn-once, same-object reuse, differing names, production) plus a patch changeset.

Verified locally: pnpm --filter styled-components test:web -- StyleSheetManager.test compiler.test prefix.test → 169 passed. The per-test reset keeps the repeatedly redefined dual name in compiler.test.ts and the duplicated project-prefixes in prefix.test.tsx from tripping the warning, and the first-party plugin names (rtl, prefix, rsc) are distinct.

The changed compiler.ts call site keeps the same createCompiler seam, so the DOM and RSC paths both get the warning without new imports on the native entry.

Pullfrog  | Fix all ➔ | Fix 👍s ➔ | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

  • Keyed the collision registry on name plus namespace (utils/compiler.ts) — notePluginName now builds name + '\0' + namespace when a namespace is present, so same-named plugins under different namespaces no longer warn. This matches the actual collision condition: createCompiler folds both the plugin names and options.namespace into compiler.hash (compiler.ts:180,182), and compiler.hash is the only plugin-derived input to class names (WebStyle.ts:210, Keyframes.ts:58).
  • JSDoc and changeset updated — the registry comment and .changeset/plugin-name-collision.md now describe the namespace-scoped condition and note that one name under different namespaces produces different classes.
  • Two new tests (models/test/StyleSheetManager.test.tsx) — no warning when same-named plugins sit under different namespaces (also asserting both namespaced rules emit), and a warning when they share one.

Verified locally: pnpm --filter styled-components test:web -- StyleSheetManager.test → 26 passed; compiler.test prefix.test → 145 passed. The prior review's single concern (spurious warning across namespaces) is addressed.

Pullfrog  | View workflow run | Using deepseek-v4.1-flash (free via Pullfrog for OSS) | 𝕏

@quantizor
quantizor merged commit d4bb01e into styled-components:main Nov 3, 2026
8 checks passed
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.

Two plugins with the same name produce the same class names, so one subtree gets the other's CSS

4 participants