Skip to content

Commit fbb2dd4

Browse files
committed
fix(build): drain compilation events after shutdown, set TURBO_ENGINE_IGNORE_DIRTY in test
- After project.shutdown() resolves, yield to the event loop via setTimeout so the JS async iterator drains all buffered persistence/compaction TraceEvents before getTraceEvents() is called in waitForShutdown. - Set TURBO_ENGINE_IGNORE_DIRTY=1 in the trace-build test so persistent caching works in dirty git repos (test directories are always dirty). - Update the turbopack snapshot to expect turbopack-compaction and turbopack-persistence spans.
1 parent 53adb6c commit fbb2dd4

File tree

2 files changed

+17
-1
lines changed

2 files changed

+17
-1
lines changed

packages/next/src/build/turbopack-build/impl.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,15 @@ export async function turbopackBuild(): Promise<{
264264
await project.writeAnalyzeData(appDirOnly)
265265
}
266266

267-
const shutdownPromise = project.shutdown()
267+
// Shutdown triggers persistence/compaction which emit TraceEvent
268+
// compilation events via the compilationEventsSubscribe iterator.
269+
// After shutdown resolves, all events have been emitted into the
270+
// iterator buffer but the JS for-await loop may not have drained
271+
// them yet. We yield to the microtask queue so the buffered events
272+
// are processed before getTraceEvents() is called by waitForShutdown.
273+
const shutdownPromise = project
274+
.shutdown()
275+
.then(() => new Promise<void>((resolve) => setTimeout(resolve, 0)))
268276

269277
const time = process.hrtime(startTime)
270278
return {

test/e2e/app-dir/trace-build-file/trace-build-file.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,12 @@ describe('trace-build-file', () => {
6262
files: __dirname,
6363
skipStart: !isNextDev,
6464
skipDeployment: true,
65+
env: {
66+
// Enable persistent caching even when the git repo is dirty (test dirs
67+
// are always dirty). Without this, the cache falls back to a temp
68+
// directory and persistence/compaction spans are not emitted.
69+
TURBO_ENGINE_IGNORE_DIRTY: '1',
70+
},
6571
})
6672

6773
if (isNextStart) {
@@ -134,6 +140,8 @@ describe('trace-build-file', () => {
134140
"static-generation",
135141
"telemetry-flush",
136142
"turbopack-build-events",
143+
"turbopack-compaction",
144+
"turbopack-persistence",
137145
]
138146
`)
139147
} else {

0 commit comments

Comments
 (0)