Skip to content

CSF: Fix false positive detection of Zod v4 .meta() as CSF Factory#33666

Merged
kasperpeulen merged 1 commit into
nextfrom
fix-zod-meta-false-positive
Jan 28, 2026
Merged

CSF: Fix false positive detection of Zod v4 .meta() as CSF Factory#33666
kasperpeulen merged 1 commit into
nextfrom
fix-zod-meta-false-positive

Conversation

@kasperpeulen
Copy link
Copy Markdown
Member

@kasperpeulen kasperpeulen commented Jan 27, 2026

Closes #33654

What I did

Fixed a bug where the CSF parser incorrectly identified Zod v4's .meta() method calls as Storybook's CSF Factories meta() function. This caused a BadMetaError when using Zod schemas with .meta() in story files:

BadMetaError: CSF: meta() factory must be imported from .storybook/preview configuration

Root cause: The parser was checking if any .meta() call on an imported variable came from a valid preview path, and throwing an error if not.

Fix: Changed the detection logic to only throw BadMetaError when the variable calling .meta() is specifically named preview. This is the canonical name used in CSF Factories (preview.meta()), so if someone has a variable with this name but imports it from the wrong path, they get a helpful error. For all other variable names (like mySchema from Zod), the .meta() call is silently ignored.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

  • stories
  • unit tests
  • integration tests
  • end-to-end tests

Manual testing

Caution

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

No manual testing required - the fix is fully covered by unit tests that simulate the exact Zod v4 usage patterns from the issue.

Documentation

  • Add or update documentation reflecting your changes
  • If you are deprecating/removing a feature, make sure to update
    MIGRATION.MD

Checklist for Maintainers

  • When this PR is ready for testing, make sure to add ci:normal, ci:merged or ci:daily GH label to it to run a specific set of sandboxes. The particular set of sandboxes can be found in code/lib/cli-storybook/src/sandbox-templates.ts

  • Make sure this PR contains one of the labels below:

    Available labels
    • bug: Internal changes that fixes incorrect behavior.
    • maintenance: User-facing maintenance tasks.
    • dependencies: Upgrading (sometimes downgrading) dependencies.
    • build: Internal-facing build tooling & test updates. Will not show up in release changelog.
    • cleanup: Minor cleanup style change. Will not show up in release changelog.
    • documentation: Documentation only changes. Will not show up in release changelog.
    • feature request: Introducing a new feature.
    • BREAKING CHANGE: Changes that break compatibility in some way with current major version.
    • other: Changes that don't fit in the above categories.

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook publish.yml --field pr=<PR_NUMBER>

Summary by CodeRabbit

  • Bug Fixes

    • Reduce false positives by only flagging incorrect preview-style metadata imports and ignoring unrelated metadata calls from external libraries.
  • Tests

    • Added tests ensuring external libraries' metadata chaining is ignored and that preview configuration path detection still validates correctly.

✏️ Tip: You can customize this high-level summary in your review settings.

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jan 27, 2026

View your CI Pipeline Execution ↗ for commit 27badf8

Command Status Duration Result
nx run-many -t compile,check,knip,test,pretty-d... ✅ Succeeded 8m 58s View ↗

☁️ Nx Cloud last updated this comment at 2026-01-27 08:18:51 UTC

@nx-cloud
Copy link
Copy Markdown

nx-cloud Bot commented Jan 27, 2026

View your CI Pipeline Execution ↗ for commit 1c98f99


☁️ Nx Cloud last updated this comment at 2026-01-27 07:56:59 UTC

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Jan 27, 2026

📝 Walkthrough

Walkthrough

meta() handling was narrowed: BadMetaError is now thrown only when the call’s root object is named preview (preview-like import). Tests updated to use a preview variable and add cases ensuring unrelated .meta() calls (e.g., from Zod) are ignored and do not trigger CSF parsing errors.

Changes

Cohort / File(s) Summary
CSF meta error gate
code/core/src/csf-tools/CsfFile.ts
Modified meta() handling to throw BadMetaError only when the call root is preview (preview-like import); other .meta() usages are ignored instead of causing an error.
Tests for preview/meta handling
code/core/src/csf-tools/CsfFile.test.ts
Updated tests to use a preview variable and preview.meta(...); added tests asserting unrelated .meta() calls (including chained calls from libraries like Zod) are ignored and do not break CSF parsing.

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

✨ Finishing touches
  • 📝 Generate docstrings

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

@storybook-app-bot
Copy link
Copy Markdown

storybook-app-bot Bot commented Jan 27, 2026

Package Benchmarks

Commit: 27badf8, ran on 30 January 2026 at 08:02:28 UTC

The following packages have significant changes to their size or dependencies:

storybook

Before After Difference
Dependency count 49 49 0
Self size 20.40 MB 20.39 MB 🎉 -10 KB 🎉
Dependency size 16.52 MB 16.52 MB 0 B
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 183 183 0
Self size 776 KB 775 KB 🎉 -830 B 🎉
Dependency size 67.57 MB 67.56 MB 🎉 -10 KB 🎉
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 176 176 0
Self size 30 KB 30 KB 🚨 +63 B 🚨
Dependency size 66.14 MB 66.13 MB 🎉 -10 KB 🎉
Bundle Size Analyzer Link Link

create-storybook

Before After Difference
Dependency count 50 50 0
Self size 1000 KB 1000 KB 🚨 +42 B 🚨
Dependency size 36.92 MB 36.91 MB 🎉 -10 KB 🎉
Bundle Size Analyzer node node

The CSF parser was incorrectly identifying Zod v4's `.meta()` method calls as Storybook's CSF Factories `meta()` function when the object calling `.meta()` was a directly imported variable.

This caused the error:
```
BadMetaError: CSF: meta() factory must be imported from .storybook/preview configuration
```

The fix changes the detection logic to only throw BadMetaError when the variable calling `.meta()` is specifically named "preview". This is the canonical name used in CSF Factories (`preview.meta()`), so if someone has a variable with this name but imports it from the wrong path, they get a helpful error.

For all other variable names (like `mySchema` from Zod), the `.meta()` call is silently ignored since it's clearly not attempting to use CSF Factories.

Closes #33654
@kasperpeulen kasperpeulen force-pushed the fix-zod-meta-false-positive branch from 1c98f99 to 27badf8 Compare January 27, 2026 08:08
@kasperpeulen kasperpeulen requested a review from shilman January 27, 2026 08:10
@kasperpeulen kasperpeulen merged commit 32ee24e into next Jan 28, 2026
125 checks passed
@kasperpeulen kasperpeulen deleted the fix-zod-meta-false-positive branch January 28, 2026 09:08
@github-actions github-actions Bot mentioned this pull request Jan 28, 2026
24 tasks
@valentinpalkovic valentinpalkovic restored the fix-zod-meta-false-positive branch January 30, 2026 07:51
@kasperpeulen kasperpeulen added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Jan 30, 2026
@github-actions github-actions Bot mentioned this pull request Feb 2, 2026
11 tasks
valentinpalkovic pushed a commit that referenced this pull request Feb 2, 2026
CSF: Fix false positive detection of Zod v4 .meta() as CSF Factory
(cherry picked from commit 32ee24e)
@github-actions github-actions Bot mentioned this pull request Feb 2, 2026
13 tasks
@github-actions github-actions Bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Feb 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug ci:normal patch:done Patch/release PRs already cherry-picked to main/release branch patch:yes Bugfix & documentation PR that need to be picked to main branch

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: CSF parser incorrectly detects Zod v4 .meta() method as Storybook's meta() factory function

1 participant