Skip to content

Turbopack: shutdown turbo-tasks even when build crashes #77937

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 9, 2025
Merged
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
193 changes: 100 additions & 93 deletions packages/next/src/build/turbopack-build/impl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -87,66 +87,77 @@ export async function turbopackBuild(): Promise<{
dependencyTracking: persistentCaching,
}
)

// Write an empty file in a known location to signal this was built with Turbopack
await fs.writeFile(path.join(distDir, 'turbopack'), '')

await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true,
})
await fs.writeFile(
path.join(distDir, 'package.json'),
JSON.stringify(
{
type: 'commonjs',
},
null,
2
try {
// Write an empty file in a known location to signal this was built with Turbopack
await fs.writeFile(path.join(distDir, 'turbopack'), '')

await fs.mkdir(path.join(distDir, 'server'), { recursive: true })
await fs.mkdir(path.join(distDir, 'static', buildId), {
recursive: true,
})
await fs.writeFile(
path.join(distDir, 'package.json'),
JSON.stringify(
{
type: 'commonjs',
},
null,
2
)
)
)

// eslint-disable-next-line @typescript-eslint/no-unused-vars
const entrypoints = await project.writeAllEntrypointsToDisk(appDirOnly)
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const entrypoints = await project.writeAllEntrypointsToDisk(appDirOnly)

const manifestLoader = new TurbopackManifestLoader({
buildId,
distDir,
encryptionKey,
})
const manifestLoader = new TurbopackManifestLoader({
buildId,
distDir,
encryptionKey,
})

const topLevelErrors = []
const topLevelWarnings = []
for (const issue of entrypoints.issues) {
if (issue.severity === 'error' || issue.severity === 'fatal') {
topLevelErrors.push(formatIssue(issue))
} else if (isRelevantWarning(issue)) {
topLevelWarnings.push(formatIssue(issue))
}
}

const topLevelErrors = []
const topLevelWarnings = []
for (const issue of entrypoints.issues) {
if (issue.severity === 'error' || issue.severity === 'fatal') {
topLevelErrors.push(formatIssue(issue))
} else if (isRelevantWarning(issue)) {
topLevelWarnings.push(formatIssue(issue))
if (topLevelWarnings.length > 0) {
console.warn(
`Turbopack build encountered ${
topLevelWarnings.length
} warnings:\n${topLevelWarnings.join('\n')}`
)
}
}

if (topLevelWarnings.length > 0) {
console.warn(
`Turbopack build encountered ${
topLevelWarnings.length
} warnings:\n${topLevelWarnings.join('\n')}`
)
}
if (topLevelErrors.length > 0) {
throw new Error(
`Turbopack build failed with ${
topLevelErrors.length
} errors:\n${topLevelErrors.join('\n')}`
)
}

if (topLevelErrors.length > 0) {
throw new Error(
`Turbopack build failed with ${
topLevelErrors.length
} errors:\n${topLevelErrors.join('\n')}`
)
}
const currentEntrypoints = await rawEntrypointsToEntrypoints(entrypoints)

const currentEntrypoints = await rawEntrypointsToEntrypoints(entrypoints)
const promises: Promise<any>[] = []

const promises: Promise<any>[] = []
if (!appDirOnly) {
for (const [page, route] of currentEntrypoints.page) {
promises.push(
handleRouteType({
page,
route,
manifestLoader,
})
)
}
}

if (!appDirOnly) {
for (const [page, route] of currentEntrypoints.page) {
for (const [page, route] of currentEntrypoints.app) {
promises.push(
handleRouteType({
page,
Expand All @@ -155,50 +166,46 @@ export async function turbopackBuild(): Promise<{
})
)
}
}

for (const [page, route] of currentEntrypoints.app) {
promises.push(
handleRouteType({
page,
route,
manifestLoader,
})
)
}

await Promise.all(promises)

await Promise.all([
manifestLoader.loadBuildManifest('_app'),
manifestLoader.loadPagesManifest('_app'),
manifestLoader.loadFontManifest('_app'),
manifestLoader.loadPagesManifest('_document'),
manifestLoader.loadBuildManifest('_error'),
manifestLoader.loadPagesManifest('_error'),
manifestLoader.loadFontManifest('_error'),
entrypoints.instrumentation &&
manifestLoader.loadMiddlewareManifest(
'instrumentation',
'instrumentation'
),
entrypoints.middleware &&
(await manifestLoader.loadMiddlewareManifest('middleware', 'middleware')),
])

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites: rewrites,
entrypoints: currentEntrypoints,
})

const shutdownPromise = project.shutdown()

const time = process.hrtime(startTime)
return {
duration: time[0] + time[1] / 1e9,
buildTraceContext: undefined,
shutdownPromise,
await Promise.all(promises)

await Promise.all([
manifestLoader.loadBuildManifest('_app'),
manifestLoader.loadPagesManifest('_app'),
manifestLoader.loadFontManifest('_app'),
manifestLoader.loadPagesManifest('_document'),
manifestLoader.loadBuildManifest('_error'),
manifestLoader.loadPagesManifest('_error'),
manifestLoader.loadFontManifest('_error'),
entrypoints.instrumentation &&
manifestLoader.loadMiddlewareManifest(
'instrumentation',
'instrumentation'
),
entrypoints.middleware &&
(await manifestLoader.loadMiddlewareManifest(
'middleware',
'middleware'
)),
])

await manifestLoader.writeManifests({
devRewrites: undefined,
productionRewrites: rewrites,
entrypoints: currentEntrypoints,
})

const shutdownPromise = project.shutdown()

const time = process.hrtime(startTime)
return {
duration: time[0] + time[1] / 1e9,
buildTraceContext: undefined,
shutdownPromise,
}
} catch (err) {
await project.shutdown()
throw err
}
}

Expand Down
Loading