Skip to content

Conversation

@valentinpalkovic
Copy link
Contributor

@valentinpalkovic valentinpalkovic commented Aug 7, 2025

Closes #32192

What I did

I have added error handling when parsing and transforming preview files in the context of module mocking. When a preview file cannot be parsed, Storybook shouldn't crash, but rather log the error (debug loglevel)

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

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

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 canary-release-pr.yml --field pr=<PR_NUMBER>

Greptile Summary

This PR adds defensive error handling to Storybook's module mocking system when transforming preview files containing sb.mock() calls. The changes affect two key components: the Vite plugin and Webpack loader responsible for parsing and transforming these files.

The implementation wraps the rewriteSbMockImportCalls function in try-catch blocks in both transformation pipelines. When parsing or transformation fails, instead of crashing the build process, the system now logs a debug message and gracefully degrades - the Vite plugin returns null while the Webpack loader returns the original untransformed source code.

This change addresses issue #32192 where overly permissive regex patterns were causing build failures for files with 'preview' in their names (like 'my-orders-preview.ts') that aren't actually Storybook preview configuration files. The error handling follows an existing pattern in the codebase where extractMockCalls already has similar defensive error handling, making the mocking system more resilient to edge cases.

The changes integrate well with Storybook's existing error handling philosophy of graceful degradation rather than hard failures, ensuring that build processes continue even when individual files cannot be processed by the mocking transformation pipeline.

Confidence score: 4/5

  • This PR is safe to merge with minimal risk as it adds defensive error handling without changing core functionality
  • Score reflects well-implemented error handling that follows existing patterns, but lacks automated test coverage
  • Pay close attention to the transformation logic to ensure the fallback behavior works correctly in production scenarios

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

2 files reviewed, 2 comments

Edit Code Review Bot Settings | Greptile

Co-authored-by: greptile-apps[bot] <165735046+greptile-apps[bot]@users.noreply.github.com>
@nx-cloud
Copy link

nx-cloud bot commented Aug 7, 2025

View your CI Pipeline Execution ↗ for commit 1e2ff7b

Command Status Duration Result
nx run-many -t build --parallel=3 ✅ Succeeded 1m 9s View ↗

☁️ Nx Cloud last updated this comment at 2025-08-07 11:14:11 UTC

@storybook-app-bot
Copy link

Package Benchmarks

Commit: 1e2ff7b, ran on 7 August 2025 at 11:06:34 UTC

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

storybook

Before After Difference
Dependency count 51 51 0
Self size 41.75 MB 41.74 MB 🎉 -15 KB 🎉
Dependency size 18.13 MB 18.13 MB 0 B
Bundle Size Analyzer Link Link

sb

Before After Difference
Dependency count 52 52 0
Self size 1 KB 1 KB 0 B
Dependency size 59.89 MB 59.87 MB 🎉 -15 KB 🎉
Bundle Size Analyzer Link Link

@storybook/cli

Before After Difference
Dependency count 220 220 0
Self size 585 KB 585 KB 0 B
Dependency size 104.38 MB 104.36 MB 🎉 -15 KB 🎉
Bundle Size Analyzer Link Link

@storybook/codemod

Before After Difference
Dependency count 189 189 0
Self size 31 KB 31 KB 0 B
Dependency size 88.45 MB 88.44 MB 🎉 -15 KB 🎉
Bundle Size Analyzer Link Link

@valentinpalkovic valentinpalkovic merged commit 906ad72 into next Aug 7, 2025
54 checks passed
@valentinpalkovic valentinpalkovic deleted the valentin/add-error-handling-to-mock-preview-parsing branch August 7, 2025 11:19
@github-actions github-actions bot mentioned this pull request Aug 7, 2025
14 tasks
@valentinpalkovic valentinpalkovic added the patch:yes Bugfix & documentation PR that need to be picked to main branch label Aug 14, 2025
@github-actions github-actions bot mentioned this pull request Aug 18, 2025
17 tasks
ndelangen pushed a commit that referenced this pull request Aug 20, 2025
…g-to-mock-preview-parsing

Mock: Catch errors when transforming preview files
(cherry picked from commit 906ad72)
ndelangen pushed a commit that referenced this pull request Aug 21, 2025
…g-to-mock-preview-parsing

Mock: Catch errors when transforming preview files
(cherry picked from commit 906ad72)
@github-actions github-actions bot added the patch:done Patch/release PRs already cherry-picked to main/release branch label Aug 21, 2025
@ndelangen ndelangen removed the patch:yes Bugfix & documentation PR that need to be picked to main branch label Oct 28, 2025
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 test utilities

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug]: Make mock loader rule more restrictive

4 participants