Do not write verbose logs synchronously#1167
Merged
sindresorhus merged 1 commit intomainfrom Oct 28, 2024
Merged
Conversation
ef6f580 to
8abf30c
Compare
ehmicky
commented
Oct 28, 2024
| const finalLines = applyVerboseOnLines(printedLines, verboseInfo, fdNumber); | ||
| writeFileSync(STDERR_FD, finalLines); | ||
| if (finalLines !== '') { | ||
| console.warn(finalLines.slice(0, -1)); |
Collaborator
Author
There was a problem hiding this comment.
finalLines might be an empty string, indicating nothing should be printed.
Else, it is guaranteed to end with a newline due to the logic in applyVerboseOnLines(). We strip that newline since console.warn() appends one.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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
verboseoption is enabled, logs are printed onstderr. At the same time, the subprocess might print its output onstdoutorstderr. 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
stderris written synchronously, it seems to be buffered by the OS. Sincestdoutandstderrare 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'sstderrwhich Node.js probably hasn't optimized for.consolewhich 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().