Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .changepacks/changepack_log_next_state_per_process.json
Original file line number Diff line number Diff line change
@@ -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"}
58 changes: 56 additions & 2 deletions packages/next-plugin/src/__tests__/coordinator-port.test.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,25 @@
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,
isConnectionError,
parsePortFile,
removeOwnPortFile,
removeStalePortFile,
removeStaleStateDirs,
resolveCoordinatorPortFile,
resolveStateDir,
unreachableCoordinatorError,
} from '../coordinator-port'

Expand Down Expand Up @@ -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)
Expand Down
61 changes: 57 additions & 4 deletions packages/next-plugin/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,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,
Expand Down Expand Up @@ -1270,10 +1271,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<string, string> = {
[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({})

Expand All @@ -1286,6 +1289,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
Expand Down
43 changes: 42 additions & 1 deletion packages/next-plugin/src/coordinator-port.ts
Original file line number Diff line number Diff line change
@@ -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'

/**
Expand Down Expand Up @@ -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.<pid>` 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.<pid>` 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
Expand Down
28 changes: 20 additions & 8 deletions packages/next-plugin/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -216,12 +218,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
Expand Down Expand Up @@ -281,6 +287,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 {
Expand Down Expand Up @@ -309,7 +317,13 @@ 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.
restoreSessionState(wasm, [sheetFile, classMapFile, fileMapFile])
// Always restore from the shared `distDir` state: a process writing its
// own state dir still starts from what the cache was built against.
restoreSessionState(wasm, [
join(distDir, 'sheet.json'),
join(distDir, 'classMap.json'),
join(distDir, 'fileMap.json'),
])
// After the restore: dropping a state that does not import resets these.
setPrefix(prefix ?? null)
setDebug(debug)
Expand Down Expand Up @@ -338,8 +352,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
Expand Down
Loading