From aef74d6f21858c67fe3382d64e73425d93732701 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Thu, 16 Nov 2023 17:18:44 +0100 Subject: [PATCH 1/3] Detect no matching export error in storybook start and build --- .../core-events/src/errors/server-errors.ts | 25 +++++++++++++++++++ code/lib/core-server/src/build-dev.ts | 5 ++-- code/lib/core-server/src/build-static.ts | 5 +++- .../core-server/src/utils/build-or-throw.ts | 20 +++++++++++++++ 4 files changed, 52 insertions(+), 3 deletions(-) create mode 100644 code/lib/core-server/src/utils/build-or-throw.ts diff --git a/code/lib/core-events/src/errors/server-errors.ts b/code/lib/core-events/src/errors/server-errors.ts index 6ca430ae1966..188c299f4a66 100644 --- a/code/lib/core-events/src/errors/server-errors.ts +++ b/code/lib/core-events/src/errors/server-errors.ts @@ -385,3 +385,28 @@ export class NextjsSWCNotSupportedError extends StorybookError { `; } } + +export class NoMatchingExportError extends StorybookError { + readonly category = Category.CORE_SERVER; + + readonly code = 4; + + constructor(public data: { error: unknown | Error }) { + super(); + } + + template() { + return dedent` + There was an exports mismatch error when trying to build Storybook. + Please check whether the versions of your Storybook packages match whenever possible, as this might be the cause. + + Problematic example: + { "@storybook/react": "7.5.3", "@storybook/react-vite": "7.4.5", "storybook": "7.3.0" } + + Correct example: + { "@storybook/react": "7.5.3", "@storybook/react-vite": "7.5.3", "storybook": "7.5.3" } + + Clearing your lock file and reinstalling your dependencies might help as well. + `; + } +} diff --git a/code/lib/core-server/src/build-dev.ts b/code/lib/core-server/src/build-dev.ts index 1e321a7d2933..2811d93905f9 100644 --- a/code/lib/core-server/src/build-dev.ts +++ b/code/lib/core-server/src/build-dev.ts @@ -27,6 +27,7 @@ import { updateCheck } from './utils/update-check'; import { getServerPort, getServerChannelUrl } from './utils/server-address'; import { getManagerBuilder, getPreviewBuilder } from './utils/get-builders'; import { warnOnIncompatibleAddons } from './utils/warnOnIncompatibleAddons'; +import { buildOrThrow } from './utils/build-or-throw'; export async function buildDevStandalone( options: CLIOptions & LoadOptions & BuilderOptions @@ -134,8 +135,8 @@ export async function buildDevStandalone( features, }; - const { address, networkAddress, managerResult, previewResult } = await storybookDevServer( - fullOptions + const { address, networkAddress, managerResult, previewResult } = await buildOrThrow(async () => + storybookDevServer(fullOptions) ); const previewTotalTime = previewResult?.totalTime; diff --git a/code/lib/core-server/src/build-static.ts b/code/lib/core-server/src/build-static.ts index e6775f519829..cde136c47413 100644 --- a/code/lib/core-server/src/build-static.ts +++ b/code/lib/core-server/src/build-static.ts @@ -35,6 +35,7 @@ import { extractStorybookMetadata } from './utils/metadata'; import { StoryIndexGenerator } from './utils/StoryIndexGenerator'; import { summarizeIndex } from './utils/summarizeIndex'; import { defaultStaticDirs } from './utils/constants'; +import { buildOrThrow } from './utils/build-or-throw'; export type BuildStaticStandaloneOptions = CLIOptions & LoadOptions & @@ -146,7 +147,9 @@ export async function buildStaticStandalone(options: BuildStaticStandaloneOption global.FEATURES = features; - await managerBuilder.build({ startTime: process.hrtime(), options: fullOptions }); + await buildOrThrow(async () => + managerBuilder.build({ startTime: process.hrtime(), options: fullOptions }) + ); if (staticDirs) { effects.push( diff --git a/code/lib/core-server/src/utils/build-or-throw.ts b/code/lib/core-server/src/utils/build-or-throw.ts new file mode 100644 index 000000000000..9370446f1fa0 --- /dev/null +++ b/code/lib/core-server/src/utils/build-or-throw.ts @@ -0,0 +1,20 @@ +import { NoMatchingExportError } from '@storybook/core-events/server-errors'; + +export async function buildOrThrow(callback: () => Promise): Promise { + try { + return await callback(); + } catch (err: any) { + const builderErrors = err.errors as { text: string }[]; + if (builderErrors) { + const inconsistentVersionsError = builderErrors.find((er) => + er.text.includes('No matching export') + ); + + if (inconsistentVersionsError) { + throw new NoMatchingExportError(err); + } + } + + throw err; + } +} From 2c1dcf3a4d90b7e03fee080cd5155229e37ea5aa Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Fri, 17 Nov 2023 14:42:50 +0100 Subject: [PATCH 2/3] Update code/lib/core-server/src/utils/build-or-throw.ts --- code/lib/core-server/src/utils/build-or-throw.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/lib/core-server/src/utils/build-or-throw.ts b/code/lib/core-server/src/utils/build-or-throw.ts index 9370446f1fa0..5c1746a0be2a 100644 --- a/code/lib/core-server/src/utils/build-or-throw.ts +++ b/code/lib/core-server/src/utils/build-or-throw.ts @@ -7,7 +7,7 @@ export async function buildOrThrow(callback: () => Promise): Promise { const builderErrors = err.errors as { text: string }[]; if (builderErrors) { const inconsistentVersionsError = builderErrors.find((er) => - er.text.includes('No matching export') + er.text?.includes('No matching export') ); if (inconsistentVersionsError) { From f0fd10b1ab2945ce16e02346ed0c3c12af2848e4 Mon Sep 17 00:00:00 2001 From: Yann Braga Date: Mon, 20 Nov 2023 15:48:52 +0100 Subject: [PATCH 3/3] Update code/lib/core-events/src/errors/server-errors.ts --- code/lib/core-events/src/errors/server-errors.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/lib/core-events/src/errors/server-errors.ts b/code/lib/core-events/src/errors/server-errors.ts index 188c299f4a66..964bbbb64a92 100644 --- a/code/lib/core-events/src/errors/server-errors.ts +++ b/code/lib/core-events/src/errors/server-errors.ts @@ -406,7 +406,7 @@ export class NoMatchingExportError extends StorybookError { Correct example: { "@storybook/react": "7.5.3", "@storybook/react-vite": "7.5.3", "storybook": "7.5.3" } - Clearing your lock file and reinstalling your dependencies might help as well. + Clearing your lock file and reinstalling your dependencies might help as well, as sometimes the version you see in your package.json might not be the one defined in your lock file, leading to version inconsistency issues. `; } }