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:
saberzero1
2026-05-24 17:09:46 +02:00
parent 17aa8ab233
commit caa55037f7
4 changed files with 192 additions and 160 deletions

View File

@@ -124,7 +124,12 @@ async function startWatching(
for (const content of initialContent) { for (const content of initialContent) {
const [_tree, vfile] = content const [_tree, vfile] = content
contentMap.set(vfile.data.relativePath!, { const relPath = vfile.data.relativePath
if (!relPath) {
console.warn(`Skipping file with no relativePath: ${vfile.path}`)
continue
}
contentMap.set(relPath, {
type: "markdown", type: "markdown",
content, content,
}) })
@@ -165,19 +170,25 @@ async function startWatching(
fp = toPosixPath(fp) fp = toPosixPath(fp)
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "add" }) changes.push({ path: fp as FilePath, type: "add" })
void rebuild(changes, clientRefresh, buildData) rebuild(changes, clientRefresh, buildData).catch((err) => {
console.error(styleText("red", "Rebuild failed:"), err.message ?? err)
})
}) })
.on("change", (fp) => { .on("change", (fp) => {
fp = toPosixPath(fp) fp = toPosixPath(fp)
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "change" }) changes.push({ path: fp as FilePath, type: "change" })
void rebuild(changes, clientRefresh, buildData) rebuild(changes, clientRefresh, buildData).catch((err) => {
console.error(styleText("red", "Rebuild failed:"), err.message ?? err)
})
}) })
.on("unlink", (fp) => { .on("unlink", (fp) => {
fp = toPosixPath(fp) fp = toPosixPath(fp)
if (buildData.ignored(fp)) return if (buildData.ignored(fp)) return
changes.push({ path: fp as FilePath, type: "delete" }) changes.push({ path: fp as FilePath, type: "delete" })
void rebuild(changes, clientRefresh, buildData) rebuild(changes, clientRefresh, buildData).catch((err) => {
console.error(styleText("red", "Rebuild failed:"), err.message ?? err)
})
}) })
return async () => { return async () => {
@@ -194,10 +205,9 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
buildData.lastBuildMs = new Date().getTime() buildData.lastBuildMs = new Date().getTime()
const numChangesInBuild = changes.length const numChangesInBuild = changes.length
const release = await mut.acquire() const release = await mut.acquire()
try {
// if there's another build after us, release and let them do it // if there's another build after us, release and let them do it
if (ctx.buildId !== buildId) { if (ctx.buildId !== buildId) {
release()
return return
} }
@@ -220,7 +230,12 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
const parsed = await parseMarkdown(ctx, pathsToParse) const parsed = await parseMarkdown(ctx, pathsToParse)
for (const content of parsed) { for (const content of parsed) {
contentMap.set(content[1].data.relativePath!, { const relPath = content[1].data.relativePath
if (!relPath) {
console.warn(`Skipping file with no relativePath: ${content[1].path}`)
continue
}
contentMap.set(relPath, {
type: "markdown", type: "markdown",
content, content,
}) })
@@ -330,11 +345,15 @@ async function rebuild(changes: ChangeEvent[], clientRefresh: () => void, buildD
} }
} }
console.log(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince("rebuild")}`) console.log(
`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince("rebuild")}`,
)
console.log(styleText("green", `Done rebuilding in ${perf.timeSince()}`)) console.log(styleText("green", `Done rebuilding in ${perf.timeSince()}`))
changes.splice(0, numChangesInBuild) changes.splice(0, numChangesInBuild)
clientRefresh() clientRefresh()
} finally {
release() release()
}
} }
export default async (argv: Argv, mut: Mutex, clientRefresh: () => void) => { export default async (argv: Argv, mut: Mutex, clientRefresh: () => void) => {

View File

@@ -854,12 +854,15 @@ function resolveGroups(
const groupConfig = groups[item.group] const groupConfig = groups[item.group]
groupPriority.set(item.group, groupConfig?.priority ?? item.priority) groupPriority.set(item.group, groupConfig?.priority ?? item.priority)
} }
groupedComponents.get(item.group)!.push({ const groupMembers = groupedComponents.get(item.group)
if (groupMembers) {
groupMembers.push({
component: item.component, component: item.component,
groupOptions: item.groupOptions, groupOptions: item.groupOptions,
}) })
} }
} }
}
// Build a unified list of renderable entries (ungrouped components + flex groups), // Build a unified list of renderable entries (ungrouped components + flex groups),
// each with a priority, so we can sort them together. // each with a priority, so we can sort them together.
@@ -873,7 +876,8 @@ function resolveGroups(
if (processedGroups.has(item.group)) continue if (processedGroups.has(item.group)) continue
processedGroups.add(item.group) processedGroups.add(item.group)
const members = groupedComponents.get(item.group)! const members = groupedComponents.get(item.group)
if (!members) continue
const groupConfig = groups[item.group] ?? {} const groupConfig = groups[item.group] ?? {}
const flexComponents = members.map((m) => ({ const flexComponents = members.map((m) => ({
@@ -896,7 +900,7 @@ function resolveGroups(
gap: groupConfig.gap ?? "1rem", gap: groupConfig.gap ?? "1rem",
}) as QuartzComponent }) as QuartzComponent
entries.push({ priority: groupPriority.get(item.group)!, component: flexComponent }) entries.push({ priority: groupPriority.get(item.group) ?? 50, component: flexComponent })
} else { } else {
entries.push({ priority: item.priority, component: item.component }) entries.push({ priority: item.priority, component: item.component })
} }

View File

@@ -78,12 +78,23 @@ export async function emitContent(ctx: BuildCtx, content: ProcessedContent[]) {
const otherEmitters = cfg.plugins.emitters.filter( const otherEmitters = cfg.plugins.emitters.filter(
(e) => e.name !== "PageTypeDispatcher" && e.name !== "ComponentResources", (e) => e.name !== "PageTypeDispatcher" && e.name !== "ComponentResources",
) )
let emitErrors = 0
const counts = await Promise.all( const counts = await Promise.all(
otherEmitters.map((emitter) => 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) 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()}`) log.end(`Emitted ${emittedFiles} files to \`${argv.output}\` in ${perf.timeSince()}`)
} }

View File

@@ -171,11 +171,6 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
maxWorkers: concurrency, maxWorkers: concurrency,
workerType: "thread", workerType: "thread",
}) })
const errorHandler = (err: any) => {
console.error(err)
process.exit(1)
}
const serializableCtx: WorkerSerializableBuildCtx = { const serializableCtx: WorkerSerializableBuildCtx = {
buildId: ctx.buildId, buildId: ctx.buildId,
argv: ctx.argv, argv: ctx.argv,
@@ -185,6 +180,7 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
virtualPages: [], virtualPages: [],
} }
try {
const textToMarkdownPromises: WorkerPromise<MarkdownContent[]>[] = [] const textToMarkdownPromises: WorkerPromise<MarkdownContent[]>[] = []
let processedFiles = 0 let processedFiles = 0
for (const chunk of chunks(fps, CHUNK_SIZE)) { for (const chunk of chunks(fps, CHUNK_SIZE)) {
@@ -198,7 +194,7 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
log.updateText(`text->markdown ${styleText("gray", `${processedFiles}/${fps.length}`)}`) log.updateText(`text->markdown ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
return result return result
}), }),
).catch(errorHandler) )
const markdownToHtmlPromises: WorkerPromise<ProcessedContent[]>[] = [] const markdownToHtmlPromises: WorkerPromise<ProcessedContent[]>[] = []
processedFiles = 0 processedFiles = 0
@@ -212,11 +208,13 @@ export async function parseMarkdown(ctx: BuildCtx, fps: FilePath[]): Promise<Pro
log.updateText(`markdown->html ${styleText("gray", `${processedFiles}/${fps.length}`)}`) log.updateText(`markdown->html ${styleText("gray", `${processedFiles}/${fps.length}`)}`)
return result return result
}), }),
).catch(errorHandler) )
res = results.flat() res = results.flat()
} finally {
await pool.terminate() await pool.terminate()
} }
}
log.end(`Parsed ${res.length} Markdown files in ${perf.timeSince()}`) log.end(`Parsed ${res.length} Markdown files in ${perf.timeSince()}`)
return res return res