diff --git a/code/lib/core-server/src/utils/StoryIndexGenerator.ts b/code/lib/core-server/src/utils/StoryIndexGenerator.ts index 39e9817d77f6..6d8817147f0f 100644 --- a/code/lib/core-server/src/utils/StoryIndexGenerator.ts +++ b/code/lib/core-server/src/utils/StoryIndexGenerator.ts @@ -113,7 +113,7 @@ export class StoryIndexGenerator { async initialize() { // Find all matching paths for each specifier - await Promise.all( + const specifiersAndCaches = await Promise.all( this.specifiers.map(async (specifier) => { const pathToSubIndex = {} as SpecifierStoriesCache; @@ -132,10 +132,16 @@ export class StoryIndexGenerator { pathToSubIndex[absolutePath] = false; }); - this.specifierToCache.set(specifier, pathToSubIndex); + return [specifier, pathToSubIndex] as const; }) ); + // We do this in a second step to avoid timing issues with the Promise.all above -- to ensure + // the keys in the `specifierToCache` object are consistent with the order of specifiers. + specifiersAndCaches.forEach(([specifier, cache]) => + this.specifierToCache.set(specifier, cache) + ); + // Extract stories for each file await this.ensureExtracted(); } diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index 0f826a81343e..967b826518e9 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -432,9 +432,9 @@ export const addStories: Task['run'] = async ( if (isCoreRenderer) { const existingStories = await filterExistsInCodeDir(addonDirs, join('template', 'stories')); - await Promise.all( - existingStories.map(async (packageDir) => linkPackageStories(packageDir, { mainConfig, cwd })) - ); + for (const packageDir of existingStories) { + await linkPackageStories(packageDir, { mainConfig, cwd }); + } // Add some extra settings (see above for what these do) if (template.expected.builder === '@storybook/builder-webpack5') {