From 45740e6997057ea3effc7a701b2b43ef26357737 Mon Sep 17 00:00:00 2001 From: owjs3901 Date: Sun, 15 Nov 2026 17:32:46 +0900 Subject: [PATCH] fix(next-plugin): keep coordinator state per process on a shared distDir A process that finds another live coordinator on its distDir already takes its own coordinator..port, but it still wrote the same sheet.json, classMap.json, fileMap.json, canonicalMap.json and setup.bin. A next dev and a next build in one directory therefore overwrote each other's state, and the next restore imported a mix of both. Such a process now writes its state to distDir/state.. It still restores from the shared distDir files, which are what Turbopack's persistent cache was built against. The process holding the shared port file keeps distDir, so the single-process case is unchanged. Startup removes the state. directories of processes that have exited. Refs #694 Co-Authored-By: Claude Opus 5.5 --- ...changepack_log_next_state_per_process.json | 1 + .../src/__tests__/coordinator-port.test.ts | 58 +++++++++++++++++- .../next-plugin/src/__tests__/plugin.test.ts | 61 +++++++++++++++++-- packages/next-plugin/src/coordinator-port.ts | 43 ++++++++++++- packages/next-plugin/src/plugin.ts | 34 ++++++++--- 5 files changed, 180 insertions(+), 17 deletions(-) create mode 100644 .changepacks/changepack_log_next_state_per_process.json diff --git a/.changepacks/changepack_log_next_state_per_process.json b/.changepacks/changepack_log_next_state_per_process.json new file mode 100644 index 000000000..1473e64de --- /dev/null +++ b/.changepacks/changepack_log_next_state_per_process.json @@ -0,0 +1 @@ +{"changes":{"packages/next-plugin/package.json":"Patch"},"note":"Write coordinator state to a per-process directory when another live next dev or next build already uses the same distDir, so concurrent processes no longer overwrite each other's sheet and class/file maps","date":"2026-11-15T00:00:00.000Z"} diff --git a/packages/next-plugin/src/__tests__/coordinator-port.test.ts b/packages/next-plugin/src/__tests__/coordinator-port.test.ts index 08f51c22a..43bf872ad 100644 --- a/packages/next-plugin/src/__tests__/coordinator-port.test.ts +++ b/packages/next-plugin/src/__tests__/coordinator-port.test.ts @@ -1,8 +1,15 @@ -import { mkdtempSync, rmSync, writeFileSync } from 'node:fs' +import * as fs from 'node:fs' +import { + mkdirSync, + mkdtempSync, + readdirSync, + rmSync, + writeFileSync, +} from 'node:fs' import { tmpdir } from 'node:os' import { join } from 'node:path' -import { afterEach, describe, expect, it } from 'bun:test' +import { afterEach, describe, expect, it, spyOn } from 'bun:test' import { formatPortFile, @@ -10,7 +17,9 @@ import { parsePortFile, removeOwnPortFile, removeStalePortFile, + removeStaleStateDirs, resolveCoordinatorPortFile, + resolveStateDir, unreachableCoordinatorError, } from '../coordinator-port' @@ -92,6 +101,51 @@ describe('coordinator port file', () => { expect(message).toContain('restart the dev server or build') }) + it('writes state beside distDir only for a process with its own port file', () => { + expect(resolveStateDir('df', join('df', 'coordinator.port'))).toBe('df') + expect( + resolveStateDir('df', join('df', `coordinator.${process.pid}.port`)), + ).toBe(join('df', `state.${process.pid}`)) + }) + + it('removes the state dirs of exited processes and keeps live ones', () => { + const dir = tmp() + for (const name of [ + `state.${DEAD_PID}`, + `state.${process.pid}`, + `state.${LIVE_FOREIGN_PID}`, + 'state.other', + ]) + mkdirSync(join(dir, name)) + writeFileSync(join(dir, `state.${DEAD_PID}`, 'sheet.json'), '{}') + + removeStaleStateDirs(dir) + + expect(readdirSync(dir).sort()).toEqual( + [ + `state.${process.pid}`, + `state.${LIVE_FOREIGN_PID}`, + 'state.other', + ].sort(), + ) + // A missing distDir (first run) is nothing to clean. + expect(() => removeStaleStateDirs(join(dir, 'missing'))).not.toThrow() + }) + + it('leaves a state dir it cannot remove for a later start', () => { + const dir = tmp() + mkdirSync(join(dir, `state.${DEAD_PID}`)) + const rmSpy = spyOn(fs, 'rmSync').mockImplementation(() => { + throw Object.assign(new Error('EBUSY'), { code: 'EBUSY' }) + }) + try { + expect(() => removeStaleStateDirs(dir)).not.toThrow() + } finally { + rmSpy.mockRestore() + } + expect(readdirSync(dir)).toEqual([`state.${DEAD_PID}`]) + }) + it('recognises connection failures, including aggregated ones', () => { expect(isConnectionError({ code: 'ECONNREFUSED' })).toBe(true) expect(isConnectionError({ errors: [{ code: 'ETIMEDOUT' }] })).toBe(true) diff --git a/packages/next-plugin/src/__tests__/plugin.test.ts b/packages/next-plugin/src/__tests__/plugin.test.ts index 639d7cfe3..79551f3a4 100644 --- a/packages/next-plugin/src/__tests__/plugin.test.ts +++ b/packages/next-plugin/src/__tests__/plugin.test.ts @@ -17,6 +17,7 @@ import { } from 'bun:test' import * as coordinatorModule from '../coordinator' +import * as coordinatorPortModule from '../coordinator-port' import cssLoader, { resetInit as resetCssInit } from '../css-loader' import { DevupUI, @@ -1202,10 +1203,12 @@ export const box = style({ color: 'red' })` const prevClassMap = { a: 0 } const prevFileMap = { 'src/App.tsx': 0 } - readFileSyncSpy - .mockReturnValueOnce(JSON.stringify(prevSheet)) // sheetFile - .mockReturnValueOnce(JSON.stringify(prevClassMap)) // classMapFile - .mockReturnValueOnce(JSON.stringify(prevFileMap)) // fileMapFile + const files: Record = { + [join('df', 'sheet.json')]: JSON.stringify(prevSheet), + [join('df', 'classMap.json')]: JSON.stringify(prevClassMap), + [join('df', 'fileMap.json')]: JSON.stringify(prevFileMap), + } + readFileSyncSpy.mockImplementation((path: string) => files[path] ?? '{}') DevupUI({}) @@ -1218,6 +1221,56 @@ export const box = style({ color: 'red' })` // Verify stale port file was deleted before starting coordinator expect(unlinkSyncSpy).toHaveBeenCalledWith(join('df', 'coordinator.port')) }) + it('writes its own state next to another live process on the same distDir', () => { + process.env.TURBOPACK = '1' + const stateDir = join('df', `state.${process.pid}`) + const portSpy = spyOn( + coordinatorPortModule, + 'resolveCoordinatorPortFile', + ).mockReturnValue(join('df', `coordinator.${process.pid}.port`)) + const staleSpy = spyOn( + coordinatorPortModule, + 'removeStaleStateDirs', + ).mockReturnValue(undefined) + const prevSheet = { css: { a: 'color:red' } } + readFileSyncSpy.mockImplementation((path: string) => + path === join('df', 'sheet.json') ? JSON.stringify(prevSheet) : '{}', + ) + try { + const config = DevupUI({}) + + expect(staleSpy).toHaveBeenCalledWith('df') + expect(mkdirSyncSpy).toHaveBeenCalledWith(stateDir, { recursive: true }) + // It still restores what the shared state holds... + expect(importSheetSpy).toHaveBeenCalledWith(prevSheet) + // ...but writes only its own files. + expect(startCoordinatorSpy).toHaveBeenCalledWith( + expect.objectContaining({ + coordinatorPortFile: join('df', `coordinator.${process.pid}.port`), + sheetFile: join(stateDir, 'sheet.json'), + classMapFile: join(stateDir, 'classMap.json'), + fileMapFile: join(stateDir, 'fileMap.json'), + }), + ) + expect(writeFileSyncSpy).toHaveBeenCalledWith( + join(stateDir, 'canonicalMap.json'), + expect.any(String), + ) + expect(writeFileSyncSpy).toHaveBeenCalledWith( + join(stateDir, 'setup.bin'), + expect.anything(), + ) + const rule = config.turbopack?.rules?.['*.{tsx,ts,jsx,js,mjs}'] as { + loaders: { options: { sheetFile: string } }[] + } + expect(rule.loaders[0].options.sheetFile).toBe( + join(stateDir, 'sheet.json'), + ) + } finally { + portSpy.mockRestore() + staleSpy.mockRestore() + } + }) it('should handle missing state files gracefully on first run', () => { process.env.TURBOPACK = '1' existsSyncSpy diff --git a/packages/next-plugin/src/coordinator-port.ts b/packages/next-plugin/src/coordinator-port.ts index 1335a4c36..b91bd1d97 100644 --- a/packages/next-plugin/src/coordinator-port.ts +++ b/packages/next-plugin/src/coordinator-port.ts @@ -1,4 +1,10 @@ -import { existsSync, readFileSync, unlinkSync } from 'node:fs' +import { + existsSync, + readdirSync, + readFileSync, + rmSync, + unlinkSync, +} from 'node:fs' import { join } from 'node:path' /** @@ -65,6 +71,41 @@ export function resolveCoordinatorPortFile(distDir: string): string { : join(distDir, `coordinator.${process.pid}.port`) } +/** + * The directory this process writes its coordinator state to (sheet, class + * and file maps, canonical map, setup handoff). + * + * The process holding the shared port file keeps `distDir`, so the next + * `next dev` restores from it. A process that had to take its own port file + * because another live one holds `distDir` writes to `state.` instead: + * two coordinators writing one set of state files would leave a mix of both + * builds for the next restore. + */ +export function resolveStateDir(distDir: string, portFile: string): string { + return portFile === join(distDir, 'coordinator.port') + ? distDir + : join(distDir, `state.${process.pid}`) +} + +/** Remove the `state.` directories of processes that have exited. */ +export function removeStaleStateDirs(distDir: string): void { + let entries: string[] + try { + entries = readdirSync(distDir) + } catch { + return + } + for (const entry of entries) { + const pid = /^state\.(\d+)$/.exec(entry)?.[1] + if (pid === undefined || isProcessAlive(Number(pid))) continue + try { + rmSync(join(distDir, entry), { recursive: true, force: true }) + } catch { + // Still locked (Windows); a later start retries. + } + } +} + /** Remove the file unless a different live process owns it. */ export function removeStalePortFile(portFile: string): void { if (foreignLiveOwner(portFile) !== undefined) return diff --git a/packages/next-plugin/src/plugin.ts b/packages/next-plugin/src/plugin.ts index aa92f663e..5010c2034 100644 --- a/packages/next-plugin/src/plugin.ts +++ b/packages/next-plugin/src/plugin.ts @@ -35,7 +35,9 @@ import { } from './coordinator' import { removeStalePortFile, + removeStaleStateDirs, resolveCoordinatorPortFile, + resolveStateDir, } from './coordinator-port' import { collectProductionPrewarmFiles } from './prewarm' import { elapsedMs, profileStart, reportProfile } from './profile' @@ -178,12 +180,16 @@ export function DevupUI( ) const watch = process.env.NODE_ENV === 'development' const sourceMap = watch || config.productionBrowserSourceMaps === true - const sheetFile = join(distDir, 'sheet.json') - const classMapFile = join(distDir, 'classMap.json') - const fileMapFile = join(distDir, 'fileMap.json') - const canonicalMapFile = join(distDir, 'canonicalMap.json') + const coordinatorPortFile = resolveCoordinatorPortFile(distDir) + // Another live `next dev`/`next build` on this distDir keeps its state; + // this process then writes its own (see resolveStateDir). + const stateDir = resolveStateDir(distDir, coordinatorPortFile) + const sheetFile = join(stateDir, 'sheet.json') + const classMapFile = join(stateDir, 'classMap.json') + const fileMapFile = join(stateDir, 'fileMap.json') + const canonicalMapFile = join(stateDir, 'canonicalMap.json') const gitignoreFile = join(distDir, '.gitignore') - const setupHandoffFile = join(distDir, 'setup.bin') + const setupHandoffFile = join(stateDir, 'setup.bin') // Next loads next.config twice for a production Turbopack build in isolated // module contexts, but both evaluations share the same process. Hand the // completed first setup to exactly one different module instance so the @@ -242,6 +248,8 @@ export function DevupUI( recursive: true, }) if (!existsSync(gitignoreFile)) writeFileSync(gitignoreFile, '*') + removeStaleStateDirs(distDir) + if (stateDir !== distDir) mkdirSync(stateDir, { recursive: true }) const wasm = loadWasm(engine) const { @@ -274,10 +282,18 @@ export function DevupUI( // When the dev server restarts, Turbopack may skip re-running loaders for // unchanged files. Without importing previous state, the coordinator's WASM // starts empty and CSS for cached files would be missing. + // Always restore from the shared `distDir` state: a process writing its + // own state dir still starts from what the cache was built against. try { - importSheet(JSON.parse(readFileSync(sheetFile, 'utf-8'))) - importClassMap(JSON.parse(readFileSync(classMapFile, 'utf-8'))) - importFileMap(JSON.parse(readFileSync(fileMapFile, 'utf-8'))) + importSheet( + JSON.parse(readFileSync(join(distDir, 'sheet.json'), 'utf-8')), + ) + importClassMap( + JSON.parse(readFileSync(join(distDir, 'classMap.json'), 'utf-8')), + ) + importFileMap( + JSON.parse(readFileSync(join(distDir, 'fileMap.json'), 'utf-8')), + ) } catch { // No previous session state (first run) or corrupt files, start fresh } @@ -297,8 +313,6 @@ export function DevupUI( // disable turbo parallel const excludeRegex = createNodeModulesExcludeRegex(include, '.mdx.[tj]sx?$') - const coordinatorPortFile = resolveCoordinatorPortFile(distDir) - // Collapse and legacy whole-file hoisting remain independent of the deferred // Turbopack atom reservation. Never install atom reach/threshold without it. const extract = sourceMap ? codeExtract : codeExtractWithoutSourceMap