Fix: Deduplicate variable names in builder-vite codegen to prevent hash collisions#34424
Conversation
When two different addon preview paths produce the same djb2 hash (e.g., addon-links and addon-a11y on certain systems), the generated virtual module contains duplicate variable names, causing: Uncaught SyntaxError: Identifier 'preview_22070' has already been declared Track seen variable names with a Set and append a numeric suffix (_2, _3, ...) when a collision is detected. This guarantees unique identifiers regardless of the hash function used. Fixes storybookjs#34372
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthroughAdded collision detection and resolution logic to the project annotations codegen to ensure unique variable names during import statement generation. When variable name collisions occur, numeric suffixes are appended incrementally. Comprehensive test coverage verifies uniqueness across single collision, no-collision, and multi-collision scenarios. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Possibly related PRs
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. Comment |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Inline comments:
In `@code/builders/builder-vite/src/codegen-project-annotations.test.ts`:
- Around line 19-21: The test currently only asserts uniqueness of the imported
variable names extracted via `importedVars = [...result.matchAll(/import \* as
(\w+) from/g)].map((m) => m[1])` and `expect(new
Set(importedVars).size).toBe(importedVars.length)`, but doesn’t assert the
expected number of imports; add explicit length assertions for each case
(expect(importedVars.length).toBe(2), expect(importedVars.length).toBe(2), and
expect(importedVars.length).toBe(3) respectively) immediately after extracting
`importedVars`, and make the same addition for the other occurrences around the
`result.matchAll` usages at the locations corresponding to lines 35-37 and 55-56
so the tests check both uniqueness and completeness.
- Line 38: The current assertion using importedVars.every((v) =>
!/_\d+$/.test(v) || /^\w+_\d+$/.test(v)) is too permissive; replace it with a
stricter check that each identifier is alphanumeric and may have an optional
numeric dedupe suffix that is a positive integer without leading zeros (for
example: /^\w+(?:_[1-9]\d*)?$/.test(v)). Update the assertion on importedVars to
use that regex so the test fails for malformed or unintended dedupe suffixes;
locate the check using the importedVars symbol in
codegen-project-annotations.test.ts and swap the predicate accordingly.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: c38c9060-d8a1-48ab-a15b-b63988245e8a
📒 Files selected for processing (2)
code/builders/builder-vite/src/codegen-project-annotations.test.tscode/builders/builder-vite/src/codegen-project-annotations.ts
|
Should already be resolved by #34274 |
Description
Adds a collision-proof deduplication mechanism to the project annotations codegen in
@storybook/builder-vite.When two different addon preview paths produce the same hash value (e.g.,
addon-linksandaddon-a11yboth generating hash22070), the generated virtual module contains duplicate variable names:This causes
Uncaught SyntaxError: Identifier 'preview_22070' has already been declaredand prevents Storybook from loading.Fixes #34372
Changes
codegen-project-annotations.ts:Set_2,_3, etc. to make the name uniquecodegen-project-annotations.test.ts(new):Context
PR #34274 improved the hash function from a simple character-code sum to djb2, which greatly reduces collision probability. However, djb2 can still theoretically collide, and the original simple hash is what's deployed in Storybook 10.3.x. This PR adds a defensive dedup layer that guarantees correctness regardless of the hash function used.
Summary by CodeRabbit
Bug Fixes
Tests