Do not write verbose logs synchronously #1167
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Background in #1165 and #600.
When the
verbose
option is enabled, logs are printed onstderr
. At the same time, the subprocess might print its output onstdout
orstderr
. To ensure that verbose logs are always printed in the same order when they interleave with the subprocess output, we are using synchronous I/O (fs.writeFileSync(2, ...)
).Unfortunately, this does not work. That's because, even when
stderr
is written synchronously, it seems to be buffered by the OS. Sincestdout
andstderr
are using different file descriptors, verbose logs might still be written out-of-order.At least, that's how I explain it, I could be wrong. The thing that's sure is that, when writing automated tests for this, the out-of-order problem was still clearly present.
The current solution using
fs.writeFileSync()
has several caveats:fs.writeFileSync()
than withprocess.stderr.write()
. In particular, it does not use some TTY-specific logic, which includes some UTF-8 to UTF-16 conversion on Windows. This leads to weird characters being printed on Windows. In general, I am not surprisedfs.writeFileSync(2, ...)
leads to weird behavior since this is quite an unusual way to write to a terminal'sstderr
which Node.js probably hasn't optimized for.console
which has some niceties when printing to a TTY (which verbose logs are likely to) such as more graceful error handling.With all of this taken into account, this PR is reverting to using
console.warn()
(as opposed toconsole.log()
, so we still print tostderr
) instead offs.writeFileSync()
.