parallelize

This commit is contained in:
Oleksii Bulenok
2026-09-01 18:54:03 +02:00
parent ab9f8df1b7
commit 62d8953841
2 changed files with 85 additions and 15 deletions
@@ -27,6 +27,7 @@ const {parse} = require('@babel/parser')
const fs = require('node:fs') const fs = require('node:fs')
const os = require('node:os') const os = require('node:os')
const path = require('node:path') const path = require('node:path')
const {Worker} = require('node:worker_threads')
const ts = require('typescript') const ts = require('typescript')
const plugin = require('../babel-plugin-lexicon-leaf-imports') const plugin = require('../babel-plugin-lexicon-leaf-imports')
@@ -526,7 +527,7 @@ describe('app callsites: transformed sources typecheck', () => {
*/ */
test( test(
'every file importing the barrel', 'every file importing the barrel',
() => { async () => {
const consumers = [] const consumers = []
;(function walk(dir) { ;(function walk(dir) {
for (const entry of fs.readdirSync(dir, {withFileTypes: true})) { for (const entry of fs.readdirSync(dir, {withFileTypes: true})) {
@@ -605,23 +606,36 @@ describe('app callsites: transformed sources typecheck', () => {
const options = loadAppCompilerOptions() const options = loadAppCompilerOptions()
/*
* The baseline and shadow typechecks are independent CPU-bound
* programs, so each runs in its own worker thread and the two proceed
* in parallel. The worker returns diagnostics as plain records (see
* lexiconTypecheckWorker.js).
*/
function diagnose(overlays) { function diagnose(overlays) {
const host = createOverlayHost(options, overlays) return new Promise((resolve, reject) => {
const program = ts.createProgram(consumers, options, host) const worker = new Worker(
const byFile = new Map() path.join(__dirname, '..', 'lexiconTypecheckWorker.js'),
for (const file of consumers) { {workerData: {consumers, overlays, options}},
const sf = program.getSourceFile(file) )
if (!sf) throw new Error(`${file} missing from program`) worker.once('message', byFile =>
byFile.set(file, fileDiagnostics(program, sf)) resolve(new Map(Object.entries(byFile))),
} )
return byFile worker.once('error', reject)
worker.once('exit', code => {
if (code !== 0) {
reject(new Error(`typecheck worker exited with code ${code}`))
}
})
})
} }
const baseline = diagnose(baselineOverlays) const [baseline, shadow] = await Promise.all([
const shadow = diagnose(shadowOverlays) diagnose(baselineOverlays),
diagnose(shadowOverlays),
])
const diagKey = d => const diagKey = d => `TS${d.code}: ${d.message}`
`TS${d.code}: ${ts.flattenDiagnosticMessageText(d.messageText, ' ')}`
const regressions = [] const regressions = []
for (const file of consumers) { for (const file of consumers) {
const known = new Set(baseline.get(file).map(diagKey)) const known = new Set(baseline.get(file).map(diagKey))
@@ -630,8 +644,16 @@ describe('app callsites: transformed sources typecheck', () => {
} }
} }
if (regressions.length > 0) { if (regressions.length > 0) {
const details = regressions
.slice(0, 20)
.map(d =>
d.fileName
? `${d.fileName}:${d.line} TS${d.code}: ${d.message}`
: `TS${d.code}: ${d.message}`,
)
.join('\n')
throw new Error( throw new Error(
`plugin introduced diagnostics in app sources:\n${formatDiagnostics(regressions)}`, `plugin introduced diagnostics in app sources:\n${details}`,
) )
} }
}, },
+48
View File
@@ -0,0 +1,48 @@
/*
* Worker thread for the "app callsites" test in
* __tests__/babel-plugin-lexicon-leaf-imports.test.js: typechecks the
* consumer files with the given overlay contents and reports error
* diagnostics. The baseline and shadow typechecks are independent CPU-bound
* programs, so the test runs one worker for each in parallel.
*
* Lives outside __tests__/ so Jest does not collect it as a test suite.
* ts.Diagnostic objects do not survive structured clone, so diagnostics are
* flattened to plain {code, message, fileName, line} records here.
*/
const {parentPort, workerData} = require('node:worker_threads')
const ts = require('typescript')
const {consumers, overlays, options} = workerData
const host = ts.createCompilerHost(options)
const origGetSourceFile = host.getSourceFile.bind(host)
const origFileExists = host.fileExists.bind(host)
const origReadFile = host.readFile.bind(host)
host.fileExists = f => overlays.has(f) || origFileExists(f)
host.readFile = f => overlays.get(f) ?? origReadFile(f)
host.getSourceFile = (f, lang, ...rest) =>
overlays.has(f)
? ts.createSourceFile(f, overlays.get(f), lang)
: origGetSourceFile(f, lang, ...rest)
const program = ts.createProgram(consumers, options, host)
const byFile = {}
for (const file of consumers) {
const sf = program.getSourceFile(file)
if (!sf) throw new Error(`${file} missing from program`)
byFile[file] = [
...program.getSyntacticDiagnostics(sf),
...program.getSemanticDiagnostics(sf),
]
.filter(d => d.category === ts.DiagnosticCategory.Error)
.map(d => ({
code: d.code,
message: ts.flattenDiagnosticMessageText(d.messageText, ' '),
fileName: d.file?.fileName,
line:
d.file && d.start !== undefined
? d.file.getLineAndCharacterOfPosition(d.start).line + 1
: undefined,
}))
}
parentPort.postMessage(byFile)