Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Core: Detect no matching export error in storybook start and build #24877

Merged
merged 3 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions code/lib/core-events/src/errors/server-errors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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, 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.
`;
}
}
5 changes: 3 additions & 2 deletions code/lib/core-server/src/build-dev.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
5 changes: 4 additions & 1 deletion code/lib/core-server/src/build-static.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 &
Expand Down Expand Up @@ -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(
Expand Down
20 changes: 20 additions & 0 deletions code/lib/core-server/src/utils/build-or-throw.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { NoMatchingExportError } from '@storybook/core-events/server-errors';

export async function buildOrThrow<T>(callback: () => Promise<T>): Promise<T> {
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;
}
}
Loading