From d8ba291e3fa0137e5a1e658b08897f19302f0188 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Fri, 20 Jan 2023 18:32:14 +1100 Subject: [PATCH 1/2] Split up detecting files from writing cache to avoid concurrency issues --- code/lib/core-server/src/utils/StoryIndexGenerator.ts | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) 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(); } From 839cc79da4a8470aba82c7281e564bc0ad044809 Mon Sep 17 00:00:00 2001 From: Tom Coleman Date: Tue, 24 Jan 2023 13:24:21 +1100 Subject: [PATCH 2/2] Use a for loop rather than promise.all to link --- scripts/tasks/sandbox-parts.ts | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scripts/tasks/sandbox-parts.ts b/scripts/tasks/sandbox-parts.ts index e3bb76527df5..84b72cd0ed0e 100644 --- a/scripts/tasks/sandbox-parts.ts +++ b/scripts/tasks/sandbox-parts.ts @@ -420,9 +420,9 @@ export const addStories: Task['run'] = async ( ); 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')