diff --git a/.changeset/red-shirts-take.md b/.changeset/red-shirts-take.md new file mode 100644 index 0000000000..160b694311 --- /dev/null +++ b/.changeset/red-shirts-take.md @@ -0,0 +1,5 @@ +--- +"@pnpm/worker": patch +--- + +Fix inconsistent store structure due to race condition. diff --git a/worker/src/start.ts b/worker/src/start.ts index 33e47cdbac..63011b2317 100644 --- a/worker/src/start.ts +++ b/worker/src/start.ts @@ -224,20 +224,25 @@ interface AddFilesFromDirResult { function initStore ({ storeDir }: InitStoreMessage): { status: string } { fs.mkdirSync(storeDir, { recursive: true }) - try { - const hexChars = '0123456789abcdef'.split('') - for (const subDir of ['files', 'index']) { - const subDirPath = path.join(storeDir, subDir) + const hexChars = '0123456789abcdef'.split('') + for (const subDir of ['files', 'index']) { + const subDirPath = path.join(storeDir, subDir) + try { fs.mkdirSync(subDirPath) - for (const hex1 of hexChars) { - for (const hex2 of hexChars) { + } catch { + // If a parallel process has already started creating the directories in the store, + // ignore if it already exists. + } + for (const hex1 of hexChars) { + for (const hex2 of hexChars) { + try { fs.mkdirSync(path.join(subDirPath, `${hex1}${hex2}`)) + } catch { + // If a parallel process has already started creating the directories in the store, + // ignore if it already exists. } } } - } catch { - // If a parallel process has already started creating the directories in the store, - // then we just stop. } return { status: 'success' } }