From 887ecceb038bbecafbd7699e526ac4a222f7d774 Mon Sep 17 00:00:00 2001 From: ehmicky Date: Sun, 10 Dec 2023 15:54:30 +0000 Subject: [PATCH] Fix verbose option --- lib/verbose.js | 5 ++++- test/fixtures/verbose-script.js | 4 ++-- test/verbose.js | 12 +++++++----- 3 files changed, 13 insertions(+), 8 deletions(-) diff --git a/lib/verbose.js b/lib/verbose.js index 5f5490ed02..03682f42f8 100644 --- a/lib/verbose.js +++ b/lib/verbose.js @@ -1,3 +1,4 @@ +import {writeFileSync} from 'node:fs'; import {debuglog} from 'node:util'; import process from 'node:process'; @@ -15,5 +16,7 @@ export const logCommand = (escapedCommand, {verbose}) => { return; } - process.stderr.write(`[${getTimestamp()}] ${escapedCommand}\n`); + // Write synchronously to ensure it is written before spawning the child process. + // This guarantees this line is written to `stderr` before the child process prints anything. + writeFileSync(process.stderr.fd, `[${getTimestamp()}] ${escapedCommand}\n`); }; diff --git a/test/fixtures/verbose-script.js b/test/fixtures/verbose-script.js index b6c35ff6ef..c242074b77 100755 --- a/test/fixtures/verbose-script.js +++ b/test/fixtures/verbose-script.js @@ -2,5 +2,5 @@ import {$} from '../../index.js'; const $$ = $({stdio: 'inherit'}); -await $$`node -e console.error("one")`; -await $$`node -e console.error("two")`; +await $$`node -p "one"`; +await $$`node -p "two"`; diff --git a/test/verbose.js b/test/verbose.js index f6303de1d5..34c3b849a9 100644 --- a/test/verbose.js +++ b/test/verbose.js @@ -8,15 +8,17 @@ const normalizeTimestamp = output => output.replaceAll(/\d/g, '0'); const testTimestamp = '[00:00:00.000]'; test('Prints command when "verbose" is true', async t => { - const {stdout, stderr} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'test'], {all: true}); + const {stdout, stderr, all} = await execa('nested.js', [JSON.stringify({verbose: true}), 'noop.js', 'test'], {all: true}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`); + t.is(normalizeTimestamp(all), `${testTimestamp} noop.js test\ntest`); }); test('Prints command with NODE_DEBUG=execa', async t => { - const {stdout, stderr} = await execa('nested.js', [JSON.stringify({}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}}); + const {stdout, stderr, all} = await execa('nested.js', [JSON.stringify({}), 'noop.js', 'test'], {all: true, env: {NODE_DEBUG: 'execa'}}); t.is(stdout, 'test'); t.is(normalizeTimestamp(stderr), `${testTimestamp} noop.js test`); + t.is(normalizeTimestamp(all), `${testTimestamp} noop.js test\ntest`); }); test('Escape verbose command', async t => { @@ -25,9 +27,9 @@ test('Escape verbose command', async t => { }); test('Verbose option works with inherit', async t => { - const {stderr} = await execa('verbose-script.js', {all: true, env: {NODE_DEBUG: 'execa'}}); - t.is(normalizeTimestamp(stderr), `${testTimestamp} node -e "console.error(\\"one\\")" + const {all} = await execa('verbose-script.js', {all: true, env: {NODE_DEBUG: 'execa'}}); + t.is(normalizeTimestamp(all), `${testTimestamp} node -p "\\"one\\"" one -${testTimestamp} node -e "console.error(\\"two\\")" +${testTimestamp} node -p "\\"two\\"" two`); });