Skip to content

Commit

Permalink
Backport v14: Static worker fixes (#71564 & #74154) (#74284)
Browse files Browse the repository at this point in the history
Backports:
- #71564
- #74154

---------

Co-authored-by: JJ Kasper <[email protected]>
  • Loading branch information
ztanner and ijjk authored Dec 24, 2024
1 parent a7f2879 commit 6c06474
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
10 changes: 4 additions & 6 deletions packages/next/src/build/type-check.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import type { Span } from '../trace'

import path from 'path'
import * as Log from './output/log'
import { Worker as JestWorker } from 'next/dist/compiled/jest-worker'
import { Worker } from '../lib/worker'
import { verifyAndLint } from '../lib/verifyAndLint'
import createSpinner from './spinner'
import { eventTypeCheckCompleted } from '../telemetry/events'
Expand All @@ -30,20 +30,18 @@ function verifyTypeScriptSetup(
hasAppDir: boolean,
hasPagesDir: boolean
) {
const typeCheckWorker = new JestWorker(
const typeCheckWorker = new Worker(
require.resolve('../lib/verify-typescript-setup'),
{
exposedMethods: ['verifyTypeScriptSetup'],
numWorkers: 1,
enableWorkerThreads,
maxRetries: 0,
}
) as JestWorker & {
) as Worker & {
verifyTypeScriptSetup: typeof import('../lib/verify-typescript-setup').verifyTypeScriptSetup
}

typeCheckWorker.getStdout().pipe(process.stdout)
typeCheckWorker.getStderr().pipe(process.stderr)

return typeCheckWorker
.verifyTypeScriptSetup({
dir,
Expand Down
22 changes: 14 additions & 8 deletions packages/next/src/lib/verifyAndLint.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { red } from './picocolors'
import { Worker } from 'next/dist/compiled/jest-worker'
import { Worker } from './worker'
import { existsSync } from 'fs'
import { join } from 'path'
import { ESLINT_DEFAULT_DIRS } from './constants'
Expand All @@ -15,18 +15,22 @@ export async function verifyAndLint(
enableWorkerThreads: boolean | undefined,
telemetry: Telemetry
): Promise<void> {
let lintWorkers:
| (Worker & {
runLintCheck: typeof import('./eslint/runLintCheck').runLintCheck
})
| undefined

try {
const lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), {
lintWorkers = new Worker(require.resolve('./eslint/runLintCheck'), {
exposedMethods: ['runLintCheck'],
numWorkers: 1,
enableWorkerThreads,
maxRetries: 0,
}) as Worker & {
runLintCheck: typeof import('./eslint/runLintCheck').runLintCheck
}

lintWorkers.getStdout().pipe(process.stdout)
lintWorkers.getStderr().pipe(process.stderr)

const lintDirs = (configLintDirs ?? ESLINT_DEFAULT_DIRS).reduce(
(res: string[], d: string) => {
const currDir = join(dir, d)
Expand All @@ -37,7 +41,7 @@ export async function verifyAndLint(
[]
)

const lintResults = await lintWorkers.runLintCheck(dir, lintDirs, {
const lintResults = await lintWorkers?.runLintCheck(dir, lintDirs, {
lintDuringBuild: true,
eslintOptions: {
cacheLocation,
Expand All @@ -63,8 +67,6 @@ export async function verifyAndLint(
if (lintOutput) {
console.log(lintOutput)
}

lintWorkers.end()
} catch (err) {
if (isError(err)) {
if (err.type === 'CompileError' || err instanceof CompileError) {
Expand All @@ -77,5 +79,9 @@ export async function verifyAndLint(
}
}
throw err
} finally {
try {
lintWorkers?.end()
} catch {}
}
}
5 changes: 5 additions & 0 deletions packages/next/src/lib/worker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ export class Worker {

this._worker = undefined

// ensure we end workers if they weren't before exit
process.on('exit', () => {
this.close()
})

const createWorker = () => {
this._worker = new JestWorker(workerPath, {
...farmOptions,
Expand Down

0 comments on commit 6c06474

Please sign in to comment.