[Bug]: Story index caches "Could not parse import/exports with acorn" for a story file that changes during indexing
Describe the bug
When a story file changes while the dev server builds the story index, the build can analyze that file as an MDX docs file. A TypeScript story file fails this analysis, so the index caches an error for a valid file, and /index.json answers 500 until the file changes again.
Possible cause, in StoryIndexGenerator.ts:
ensureExtractedrunsupdateExtractedtwice. Pass 2 callsextractDocsfor every entry that is still empty, without theisDocsMdxcheck of pass 1 (L304-L306).updateExtractedskips the entries that are set when it starts (L260-L263). Ifinvalidate()resets an entry after pass 1 has skipped or finished it (L864), pass 2 gets the story file.
Reproduction link
https://github.com/nkrkv/storybook-index-race-repro
Reproduction steps
The repository is the react-vite/default-ts sandbox after storybook init --no-features. The commits after the stock one (git diff 12ce6c4) pin Storybook 10.6.0, and add repro.sh and this indexer to .storybook/main.ts:
"experimental_indexers": (indexers = []) => [
{
test: /Header\.stories\.ts$/,
createIndex: async (fileName, options) => {
await new Promise((resolve) => setTimeout(resolve, 3000));
return indexers.find((indexer) => indexer.test.test(fileName))!.createIndex(fileName, options);
},
},
...indexers,
],The indexer waits 3 s, then indexes Header.stories.ts as usual, so the index build that the first change starts is still in pass 1 when the second change arrives. With @storybook/addon-vitest, each change starts one more index build; that build extracts Button.stories.ts again before the first build reaches pass 2, and the steps pass.
yarn installyarn storybook, and wait until it is ready. Do not open it in the browser: an/index.jsonrequest right after the second change hides the bug the same way../repro.shin a second terminal. It runstouch src/stories/Header.stories.ts,sleep 0.5,touch src/stories/Button.stories.ts,sleep 4, and then requests/index.json.
Actual:
FAIL: /index.json answered 500:
Unable to index ./src/stories/Button.stories.ts:
Error: Could not parse import/exports with acorn
To fix the index, run 'touch src/stories/Button.stories.ts'.The full response of /index.json:
Unable to index ./src/stories/Button.stories.ts:
Error: Could not parse import/exports with acorn
at file://…/node_modules/storybook/dist/core-server/index.js:16573:24
at async Promise.all (index 0)
at async Promise.all (index 0)
at async _StoryIndexGenerator.updateExtracted (file://…/node_modules/storybook/dist/core-server/index.js:16556:5)
at async _StoryIndexGenerator.ensureExtracted (file://…/node_modules/storybook/dist/core-server/index.js:16593:8)
at async _StoryIndexGenerator.getIndexAndStats (file://…/node_modules/storybook/dist/core-server/index.js:16824:134)
at async _StoryIndexGenerator.getIndex (file://…/node_modules/storybook/dist/core-server/index.js:16817:13)
at async ModuleGraphEngine.refreshStoryFiles (file://…/node_modules/storybook/dist/_node-chunks/chunk-5VUZRITZ.js:2927:22)
at async ModuleGraphEngine.whenSettled (file://…/node_modules/storybook/dist/_node-chunks/chunk-5VUZRITZ.js:2853:30)
at async Object.handler (file://…/node_modules/storybook/dist/_node-chunks/chunk-5VUZRITZ.js:3026:72)The dev server logs the same error. Later requests return it too, until Button.stories.ts changes again.
Expected: PASS: /index.json answered 200 with the stories of Button.stories.ts. Without the indexer, repro.sh passes.
System
Storybook Environment Info:
System:
OS: Linux 7.2 Arch Linux
CPU: (8) x64 Intel(R) Core(TM) i7-6700K CPU @ 4.00GHz
Shell: 5.9.2 - /usr/bin/zsh
Binaries:
Node: 24.9.0 - /tmp/xfs-4dc3ae53/node
Yarn: 4.18.0 - /tmp/xfs-4dc3ae53/yarn <----- active
npm: 11.6.0 - ~/.nvm/versions/node/v24.9.0/bin/npm
Browsers:
Firefox: 155.0.1
npmPackages:
@storybook/react-vite: 10.6.0 => 10.6.0
storybook: 10.6.0 => 10.6.0Additional context
The steps give the same result with storybook and @storybook/react-vite 11.0.0-alpha.1.
The isDocsMdx check in pass 2 alone is not enough. With it, repro.sh reports FAIL: /index.json answered 200 without the stories of Button.stories.ts, and later requests return the same index until the next change. The build that was running when the file changed still saves its result in lastIndex (L794).
#24117 and #25195 report the same error for .stories.tsx and .stories.ts files; both were closed without a reproduction.
We hit this with Storybook 10.1.0, when a compiler rewrote our story files while storybook dev was running.
Source: storybookjs/storybook