storybookjs / storybook

Storybook is the industry standard workshop for building, documenting, and testing UI components in isolation
https://storybook.js.org
MIT License
84.68k stars 9.32k forks source link

Core: Fix race condition during empty folder init #29490

Closed valentinpalkovic closed 2 weeks ago

valentinpalkovic commented 2 weeks ago

Closes #

What I did

Initializing Storybook in an empty directory currently fails due to a race condition with the following error:

/.npm/_npx/aa1ee7aee97d6cac/node_modules/create-storybook/bin/index.cjs:23
  throw error;
  ^

[Error: ENOENT: no such file or directory, open '/projects/testing/react-vite-84/.cache/storybook/default/dev-server/storybook-e35710dcf9c6d4f837d5dae15e56122e'] {
  errno: -2,
  code: 'ENOENT',
  syscall: 'open',
  path: '/projects/testing/react-vite-84/.cache/storybook/default/dev-server/storybook-e35710dcf9c6d4f837d5dae15e56122e'
}

Node.js v18.20.3

The issue was, that we introduced an alternative implementation for our internal caching, where we relied on file-system-cache, but we removed the package to use a custom caching solution.

The problem with the new solution is that every time we want to write into the cache, we don’t check, whether the cache folder actually exists. The cache folder itself gets initialized during module loading, so it should be, in theory, in place. Now, the crux is that when we initialize into an empty directory, we remove especially a .cache folder before continuing because in the past, we had issues initializing into an empty directory if it wasn’t empty.

Checklist for Contributors

Testing

The changes in this PR are covered in the following automated tests:

Manual testing

This section is mandatory for all contributions. If you believe no manual test is necessary, please state so explicitly. Thanks!

Documentation

Checklist for Maintainers

🦋 Canary release

This PR does not have a canary release associated. You can request a canary release of this pull request by mentioning the @storybookjs/core team here.

_core team members can create a canary release here or locally with gh workflow run --repo storybookjs/storybook canary-release-pr.yml --field pr=<PR_NUMBER>_

name before after diff z %
createSize 0 B 0 B 0 B - -
generateSize 77.7 MB 77.7 MB 0 B 0.92 0%
initSize 143 MB 143 MB 370 B -0.49 0%
diffSize 65.1 MB 65.1 MB 370 B -0.5 0%
buildSize 6.87 MB 6.87 MB 0 B 0.58 0%
buildSbAddonsSize 1.51 MB 1.51 MB 0 B 0.51 0%
buildSbCommonSize 195 kB 195 kB 0 B - 0%
buildSbManagerSize 1.9 MB 1.9 MB 0 B 0.58 0%
buildSbPreviewSize 271 kB 271 kB 0 B - 0%
buildStaticSize 0 B 0 B 0 B - -
buildPrebuildSize 3.88 MB 3.88 MB 0 B 0.58 0%
buildPreviewSize 3 MB 3 MB 0 B - 0%
testBuildSize 0 B 0 B 0 B - -
testBuildSbAddonsSize 0 B 0 B 0 B - -
testBuildSbCommonSize 0 B 0 B 0 B - -
testBuildSbManagerSize 0 B 0 B 0 B - -
testBuildSbPreviewSize 0 B 0 B 0 B - -
testBuildStaticSize 0 B 0 B 0 B - -
testBuildPrebuildSize 0 B 0 B 0 B - -
testBuildPreviewSize 0 B 0 B 0 B - -
name before after diff z %
createTime 17.4s 8.6s -8s -808ms -0.82 -102%
generateTime 27.1s 24.8s -2s -374ms 0.78 -9.6%
initTime 20.5s 17.6s -2s -895ms 0.58 -16.4%
buildTime 10.1s 9s -1s -96ms -0.07 -12.1%
testBuildTime 0ms 0ms 0ms - -
devPreviewResponsive 5.9s 6s 162ms -0.46 2.7%
devManagerResponsive 4.2s 3.9s -311ms -0.34 -7.9%
devManagerHeaderVisible 648ms 599ms -49ms -0.55 -8.2%
devManagerIndexVisible 722ms 685ms -37ms -0.37 -5.4%
devStoryVisibleUncached 1.9s 1.1s -831ms -0.22 -74.6%
devStoryVisible 720ms 684ms -36ms -0.35 -5.3%
devAutodocsVisible 565ms 536ms -29ms -0.6 -5.4%
devMDXVisible 556ms 519ms -37ms -0.56 -7.1%
buildManagerHeaderVisible 742ms 661ms -81ms 0.24 -12.3%
buildManagerIndexVisible 752ms 718ms -34ms 0.62 -4.7%
buildStoryVisible 744ms 652ms -92ms 0.18 -14.1%
buildAutodocsVisible 553ms 600ms 47ms 0.74 7.8%
buildMDXVisible 517ms 524ms 7ms 0.01 1.3%

Greptile Summary

This PR fixes a race condition in FileSystemCache by ensuring the cache directory exists before writing files during initialization of empty folders.

nx-cloud[bot] commented 2 weeks ago

☁️ Nx Cloud Report

CI is running/has finished running commands for commit 0d576eb07b753ac2d550db047311e369e1664fcd. As they complete they will appear below. Click to see the status, the terminal output, and the build insights.

📂 See all runs for this CI Pipeline Execution


✅ Successfully ran 2 targets - [`nx affected -t check -c production --parallel=7`](https://cloud.nx.app/runs/R9vbeH9VCj?utm_source=pull-request&utm_medium=comment) - [`nx run-many -t build -c production --parallel=3`](https://cloud.nx.app/runs/aL0xDLeTKj?utm_source=pull-request&utm_medium=comment)

Sent with 💌 from NxCloud.