Description
Specification
Using process.stderr
and console.error
behaves differently when on Windows compared Linux and MacOS.
- Files: synchronous on Windows and POSIX
- TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
- Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
By applying this fix to our stream handlers:
// Default behaviour on Node.js:
// Files: synchronous on Windows and POSIX
// TTYs (Terminals): asynchronous on Windows, synchronous on POSIX
// Pipes (and sockets): synchronous on Windows, asynchronous on POSIX
// In order to align Windows with POSIX behaviour:
if (process.stdout.isTTY) {
process.stdout._handle.setBlocking(true);
} else if (os.platform() === 'win32' && !process.stdout.isTTY) {
process.stdout._handle.setBlocking(false);
}
It ends up making Windows behave the same as Linux and MacOS.
The same needs to be applied to stderr
as well.
Note that we leave ConsoleErrHandler
the same as usual because it's not necessarily going to nodejs.
This means we should probably create StandardOutHandler
and StandardErrHandler
, which is a breaking change for any usage of StreamHandler
. This requires a rename in downstream projects... primarily tests in DB, EFS and PK.
Additionally Powershell currently outputs in UTF-16LE. This is a bit weird when piping the output of the stderr/stdout to a file. We can write in code:
process.stdout.setDefaultEncoding('utf-8');
process.stderr.setDefaultEncoding('utf-8');
Just to be really sure (but it is a global change note). One idea is to only set it when we outputting a log, to avoid global changes, we can tack on a utf-8
setting. Or we can respect whatever has been set in the application.
That being said, it won't work for powershell, only CMD. So powershell requires an update to the latest powershell core for it work properly.
As for CRLF vs LF, always use LF in our logs. Both powershell and cmd understand and render this properly.
Additional context
- https://github.com/matrixai/polykey/issues/401#issuecomment-1186873742
- https://github.com/matrixai/polykey/issues/401#issuecomment-1186832315
Tasks
- ...
- ...
- ...