fix: harden core build system against silent failures
- Wrap rebuild() in try/finally to guarantee mutex release (prevents deadlock) - Surface rebuild errors via .catch() instead of discarding with void - Add null checks for vfile.data.relativePath (prevents crash on virtual files) - Add try-finally for worker pool cleanup in parse.ts (prevents thread leaks) - Remove process.exit(1) from parse error handler (let errors propagate) - Add per-emitter error handling in emit phase with degraded-build warning - Replace unsafe Map.get()! assertions with null-checked access in config-loader
This commit is contained in:
@@ -78,12 +78,23 @@ export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) {
|
||||
const otherEmitters = cfg.plugins.emitters.filter(
|
||||
(e) => e.name !== "PageTypeDispatcher" && e.name !== "ComponentResources",
|
||||
)
|
||||
let emitErrors = 0
|
||||
const counts = await Promise.all(
|
||||
otherEmitters.map((emitter) =>
|
||||
runEmitter(emitter, ctx, contentWithVirtual, staticResources, log),
|
||||
runEmitter(emitter, ctx, contentWithVirtual, staticResources, log).catch((err) => {
|
||||
emitErrors++
|
||||
console.error(`Emitter "${emitter.name}" failed:`, err.message ?? err)
|
||||
return 0
|
||||
}),
|
||||
),
|
||||
)
|
||||
emittedFiles += counts.reduce((sum, c) => sum + c, 0)
|
||||
|
||||
if (emitErrors > 0) {
|
||||
console.warn(
|
||||
`\nBuild completed with ${emitErrors} emitter failure(s). Output may be incomplete.`,
|
||||
)
|
||||
}
|
||||
|
||||
log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
|
||||
}
|
||||
|
||||
+33
-35
@@ -171,11 +171,6 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
|
||||
maxWorkers: concurrency,
|
||||
workerType: "thread",
|
||||
})
|
||||
const errorHandler = (err: any) => {
|
||||
console.error(err)
|
||||
process.exit(1)
|
||||
}
|
||||
|
||||
const serializableCtx: WorkerSerializableBuildCtx = {
|
||||
buildId: ctx.buildId,
|
||||
argv: ctx.argv,
|
||||
@@ -185,37 +180,40 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
|
||||
virtualPages: [],
|
||||
}
|
||||
|
||||
const textToMarkdownPromises: WorkerPromise<MarkdownContent[]>[] = []
|
||||
let processedFiles = 0
|
||||
for (const chunk of chunks(fps, CHUNK_SIZE)) {
|
||||
textToMarkdownPromises.push(pool.exec("parseMarkdown", [serializableCtx, chunk]))
|
||||
try {
|
||||
const textToMarkdownPromises: WorkerPromise<MarkdownContent[]>[] = []
|
||||
let processedFiles = 0
|
||||
for (const chunk of chunks(fps, CHUNK_SIZE)) {
|
||||
textToMarkdownPromises.push(pool.exec("parseMarkdown", [serializableCtx, chunk]))
|
||||
}
|
||||
|
||||
const mdResults: Array<MarkdownContent[]> = await Promise.all(
|
||||
textToMarkdownPromises.map(async (promise) => {
|
||||
const result = await promise
|
||||
processedFiles += result.length
|
||||
log.updateText(`text->markdown ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
|
||||
return result
|
||||
}),
|
||||
)
|
||||
|
||||
const markdownToHtmlPromises: WorkerPromise<ProcessedContent[]>[] = []
|
||||
processedFiles = 0
|
||||
for (const mdChunk of mdResults) {
|
||||
markdownToHtmlPromises.push(pool.exec("processHtml", [serializableCtx, mdChunk]))
|
||||
}
|
||||
const results: ProcessedContent[][] = await Promise.all(
|
||||
markdownToHtmlPromises.map(async (promise) => {
|
||||
const result = await promise
|
||||
processedFiles += result.length
|
||||
log.updateText(`markdown->html ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
|
||||
return result
|
||||
}),
|
||||
)
|
||||
|
||||
res = results.flat()
|
||||
} finally {
|
||||
await pool.terminate()
|
||||
}
|
||||
|
||||
const mdResults: Array<MarkdownContent[]> = await Promise.all(
|
||||
textToMarkdownPromises.map(async (promise) => {
|
||||
const result = await promise
|
||||
processedFiles += result.length
|
||||
log.updateText(`text->markdown ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
|
||||
return result
|
||||
}),
|
||||
).catch(errorHandler)
|
||||
|
||||
const markdownToHtmlPromises: WorkerPromise<ProcessedContent[]>[] = []
|
||||
processedFiles = 0
|
||||
for (const mdChunk of mdResults) {
|
||||
markdownToHtmlPromises.push(pool.exec("processHtml", [serializableCtx, mdChunk]))
|
||||
}
|
||||
const results: ProcessedContent[][] = await Promise.all(
|
||||
markdownToHtmlPromises.map(async (promise) => {
|
||||
const result = await promise
|
||||
processedFiles += result.length
|
||||
log.updateText(`markdown->html ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
|
||||
return result
|
||||
}),
|
||||
).catch(errorHandler)
|
||||
|
||||
res = results.flat()
|
||||
await pool.terminate()
|
||||
}
|
||||
|
||||
log.end(`Parsed ${res.length} Markdown files in ${perf.timeSince()}`)
|
||||
|
||||
Reference in New Issue
Block a user