Skip to content

Commit 0fdeae8

Browse files
Harden CPU count and cache key; set exit code on compiler close errors
Safely detect CPU count with try/catch fallback to avoid init crashes. Use a filesystem-safe cache key by joining parts with '__'. Mark process exit code when compiler.close reports an error in prod and ensure unhandled build failures exit non-zero.
1 parent 1b82b76 commit 0fdeae8

File tree

1 file changed

+19
-5
lines changed

1 file changed

+19
-5
lines changed

build.mjs

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,14 @@ if (cacheCompressionEnv != null) {
3232
cacheCompressionOption = false
3333
}
3434
}
35-
const cpuCount = os.cpus().length || 1
35+
let cpuCount = 1
36+
try {
37+
const cpuInfo = os.cpus && os.cpus()
38+
const len = Array.isArray(cpuInfo) ? cpuInfo.length : 0
39+
cpuCount = Number.isInteger(len) && len > 0 ? len : 1
40+
} catch {
41+
cpuCount = 1
42+
}
3643
const rawWorkers = process.env.BUILD_THREAD_WORKERS
3744
? parseInt(process.env.BUILD_THREAD_WORKERS, 10)
3845
: undefined
@@ -97,13 +104,14 @@ async function runWebpack(isWithoutKatex, isWithoutTiktoken, minimal, sourceBuil
97104
}
98105

99106
const dirKey = path.basename(sourceBuildDir || outdir)
100-
const variantId = [
107+
const variantParts = [
101108
isWithoutKatex ? 'no-katex' : 'with-katex',
102109
isWithoutTiktoken ? 'no-tiktoken' : 'with-tiktoken',
103110
minimal ? 'minimal' : 'full',
104111
dirKey,
105112
isProduction ? 'prod' : 'dev',
106-
].join('|')
113+
]
114+
const variantId = variantParts.join('__')
107115

108116
const compiler = webpack({
109117
entry: {
@@ -373,7 +381,10 @@ async function runWebpack(isWithoutKatex, isWithoutTiktoken, minimal, sourceBuil
373381
compiler.run((err, stats) => {
374382
const finishClose = () =>
375383
compiler.close((closeErr) => {
376-
if (closeErr) console.error('Error closing compiler:', closeErr)
384+
if (closeErr) {
385+
console.error('Error closing compiler:', closeErr)
386+
process.exitCode = 1
387+
}
377388
})
378389
try {
379390
const ret = callback(err, stats)
@@ -581,4 +592,7 @@ async function build() {
581592
)
582593
}
583594

584-
build()
595+
build().catch((e) => {
596+
console.error(e)
597+
process.exit(1)
598+
})

0 commit comments

Comments
 (0)