1- import { writeFileSync } from 'node:fs' ;
21import { inspect } from 'node:util' ;
32import { escapeLines } from '../arguments/escape.js' ;
43import { defaultVerboseFunction } from './default.js' ;
54import { applyVerboseOnLines } from './custom.js' ;
65
7- // Write synchronously to ensure lines are properly ordered and not interleaved with `stdout`
6+ // This prints on stderr.
7+ // If the subprocess prints on stdout and is using `stdout: 'inherit'`,
8+ // there is a chance both writes will compete (introducing a race condition).
9+ // This means their respective order is not deterministic.
10+ // In particular, this means the verbose command lines might be after the start of the subprocess output.
11+ // Using synchronous I/O does not solve this problem.
12+ // However, this only seems to happen when the `stdout`/`stderr` target
13+ // (e.g. a terminal) is being written to by many subprocesses at once, which is unlikely in real scenarios.
814export const verboseLog = ( { type, verboseMessage, fdNumber, verboseInfo, result} ) => {
915 const verboseObject = getVerboseObject ( { type, result, verboseInfo} ) ;
1016 const printedLines = getPrintedLines ( verboseMessage , verboseObject ) ;
1117 const finalLines = applyVerboseOnLines ( printedLines , verboseInfo , fdNumber ) ;
12- writeFileSync ( STDERR_FD , finalLines ) ;
18+ if ( finalLines !== '' ) {
19+ console . warn ( finalLines . slice ( 0 , - 1 ) ) ;
20+ }
1321} ;
1422
1523const getVerboseObject = ( {
@@ -35,9 +43,6 @@ const getPrintedLine = verboseObject => {
3543 return { verboseLine, verboseObject} ;
3644} ;
3745
38- // Unless a `verbose` function is used, print all logs on `stderr`
39- const STDERR_FD = 2 ;
40-
4146// Serialize any type to a line string, for logging
4247export const serializeVerboseMessage = message => {
4348 const messageString = typeof message === 'string' ? message : inspect ( message ) ;
0 commit comments