From 2481126013fad85be8e7f213f5113ae061648bc4 Mon Sep 17 00:00:00 2001 From: Patrik Oldsberg Date: Fri, 27 Feb 2026 08:48:19 +0100 Subject: [PATCH] Remove setBlocking calls and simplify file-redirect approach Remove fragile process.stdout._handle.setBlocking calls from CLI test commands. Revert the preload workaround in repo-tools since the root cause is removed. Signed-off-by: Patrik Oldsberg Made-with: Cursor --- .../src/modules/test/commands/package/test.ts | 5 -- .../src/modules/test/commands/repo/test.ts | 2 - packages/repo-tools/src/commands/util.ts | 48 +++---------------- 3 files changed, 6 insertions(+), 49 deletions(-) diff --git a/packages/cli/src/modules/test/commands/package/test.ts b/packages/cli/src/modules/test/commands/package/test.ts index c4acfb287b..9c13019bb5 100644 --- a/packages/cli/src/modules/test/commands/package/test.ts +++ b/packages/cli/src/modules/test/commands/package/test.ts @@ -87,11 +87,6 @@ export default async (_opts: OptionValues, cmd: Command) => { }--no-node-snapshot`; } - // This ensures that the process doesn't exit too early before stdout is flushed - if (args.includes('--help')) { - (process.stdout as any)._handle.setBlocking(true); - } - // Because of the ongoing migration to v30 of jest, jest is no longer hard-depended to allow // opt-in migration. Users instead need to add jest as a devDependency themselves and specify // the version they want. This prints a helpful error message if jest is not found, i.e. they diff --git a/packages/cli/src/modules/test/commands/repo/test.ts b/packages/cli/src/modules/test/commands/repo/test.ts index 53f86fd635..bb614798b4 100644 --- a/packages/cli/src/modules/test/commands/repo/test.ts +++ b/packages/cli/src/modules/test/commands/repo/test.ts @@ -304,11 +304,9 @@ export async function command(opts: OptionValues, cmd: Command): Promise { }--no-node-snapshot`; } - // This ensures that the process doesn't exit too early before stdout is flushed if (args.includes('--jest-help')) { removeOptionArg(args, '--jest-help'); args.push('--help'); - (process.stdout as any)._handle.setBlocking(true); } // This code path is enabled by the --successCache flag, which is specific to diff --git a/packages/repo-tools/src/commands/util.ts b/packages/repo-tools/src/commands/util.ts index c56a98fc04..bce2359589 100644 --- a/packages/repo-tools/src/commands/util.ts +++ b/packages/repo-tools/src/commands/util.ts @@ -15,42 +15,10 @@ */ import { spawnSync } from 'node:child_process'; -import { - openSync, - closeSync, - readFileSync, - unlinkSync, - writeFileSync, -} from 'node:fs'; +import { openSync, closeSync, readFileSync, unlinkSync } from 'node:fs'; import { tmpdir } from 'node:os'; import { join } from 'node:path'; -// Preload script that stubs process.stdout._handle when stdout is backed by a -// file (SyncWriteStream). Some CLI code accesses _handle.setBlocking directly -// and would crash without this. -const preloadContent = ` -if (process.stdout && !process.stdout._handle) { - process.stdout._handle = { setBlocking() {} }; -} -`; - -let preloadPath: string | undefined; - -function getPreloadPath() { - if (!preloadPath) { - preloadPath = join(tmpdir(), `backstage-stdout-preload-${process.pid}.cjs`); - writeFileSync(preloadPath, preloadContent); - process.on('exit', () => { - try { - unlinkSync(preloadPath!); - } catch { - /* ignore */ - } - }); - } - return preloadPath; -} - /** * Redirect stdout to a temp file so that Node.js creates a SyncWriteStream * (synchronous writes) in the child instead of an async pipe stream. This @@ -67,15 +35,11 @@ export function createBinRunner(cwd: string, path: string) { const outFd = openSync(outPath, 'w'); try { - const result = spawnSync( - 'node', - ['--require', getPreloadPath(), ...args], - { - cwd, - stdio: ['ignore', outFd, 'pipe'], - maxBuffer: 10 * 1024 * 1024, - }, - ); + const result = spawnSync('node', args, { + cwd, + stdio: ['ignore', outFd, 'pipe'], + maxBuffer: 10 * 1024 * 1024, + }); closeSync(outFd); const stdout = readFileSync(outPath, 'utf8');