Skip to content

Commit 9b90385

Browse files
richardlautargos
authored andcommitted
Revert "lib: print to stdout/stderr directly instead of using console"
This reverts commit 2b24ffa. Fixes: #27819 PR-URL: #27823 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jeremiah Senkpiel <[email protected]> Reviewed-By: Сковорода Никита Андреевич <[email protected]> Reviewed-By: Joyee Cheung <[email protected]> Reviewed-By: Gireesh Punathil <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Reviewed-By: Rich Trott <[email protected]>
1 parent 34ef9e4 commit 9b90385

File tree

6 files changed

+35
-103
lines changed

6 files changed

+35
-103
lines changed

lib/fs.js

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ const {
7575
validateOffsetLengthRead,
7676
validateOffsetLengthWrite,
7777
validatePath,
78-
warnOnNonPortableTemplate,
79-
handleErrorFromBinding
78+
warnOnNonPortableTemplate
8079
} = require('internal/fs/utils');
8180
const {
8281
CHAR_FORWARD_SLASH,
@@ -119,6 +118,23 @@ function showTruncateDeprecation() {
119118
}
120119
}
121120

121+
function handleErrorFromBinding(ctx) {
122+
if (ctx.errno !== undefined) { // libuv error numbers
123+
const err = uvException(ctx);
124+
// eslint-disable-next-line no-restricted-syntax
125+
Error.captureStackTrace(err, handleErrorFromBinding);
126+
throw err;
127+
}
128+
if (ctx.error !== undefined) { // Errors created in C++ land.
129+
// TODO(joyeecheung): currently, ctx.error are encoding errors
130+
// usually caused by memory problems. We need to figure out proper error
131+
// code(s) for this.
132+
// eslint-disable-next-line no-restricted-syntax
133+
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
134+
throw ctx.error;
135+
}
136+
}
137+
122138
function maybeCallback(cb) {
123139
if (typeof cb === 'function')
124140
return cb;

lib/internal/fs/utils.js

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,7 @@ const {
1212
ERR_INVALID_OPT_VALUE_ENCODING,
1313
ERR_OUT_OF_RANGE
1414
},
15-
hideStackFrames,
16-
uvException
15+
hideStackFrames
1716
} = require('internal/errors');
1817
const {
1918
isUint8Array,
@@ -452,26 +451,7 @@ function warnOnNonPortableTemplate(template) {
452451
}
453452
}
454453

455-
// This handles errors following the convention of the fs binding.
456-
function handleErrorFromBinding(ctx) {
457-
if (ctx.errno !== undefined) { // libuv error numbers
458-
const err = uvException(ctx);
459-
// eslint-disable-next-line no-restricted-syntax
460-
Error.captureStackTrace(err, handleErrorFromBinding);
461-
throw err;
462-
}
463-
if (ctx.error !== undefined) { // Errors created in C++ land.
464-
// TODO(joyeecheung): currently, ctx.error are encoding errors
465-
// usually caused by memory problems. We need to figure out proper error
466-
// code(s) for this.
467-
// eslint-disable-next-line no-restricted-syntax
468-
Error.captureStackTrace(ctx.error, handleErrorFromBinding);
469-
throw ctx.error;
470-
}
471-
}
472-
473454
module.exports = {
474-
handleErrorFromBinding,
475455
assertEncoding,
476456
copyObject,
477457
Dirent,

lib/internal/main/repl.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const {
1111
evalScript
1212
} = require('internal/process/execution');
1313

14-
const { print, kStderr, kStdout } = require('internal/util/print');
14+
const console = require('internal/console/global');
1515

1616
const { getOptionValue } = require('internal/options');
1717

@@ -21,12 +21,14 @@ markBootstrapComplete();
2121

2222
// --input-type flag not supported in REPL
2323
if (getOptionValue('--input-type')) {
24-
print(kStderr, 'Cannot specify --input-type for REPL');
24+
// If we can't write to stderr, we'd like to make this a noop,
25+
// so use console.error.
26+
console.error('Cannot specify --input-type for REPL');
2527
process.exit(1);
2628
}
2729

28-
print(kStdout, `Welcome to Node.js ${process.version}.\n` +
29-
'Type ".help" for more information.');
30+
console.log(`Welcome to Node.js ${process.version}.\n` +
31+
'Type ".help" for more information.');
3032

3133
const cliRepl = require('internal/repl');
3234
cliRepl.createInternalRepl(process.env, (err, repl) => {

lib/internal/process/execution.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,24 @@ function tryGetCwd() {
3535
}
3636
}
3737

38-
function evalModule(source, printResult) {
38+
function evalModule(source, print) {
39+
const { log, error } = require('internal/console/global');
3940
const { decorateErrorStack } = require('internal/util');
4041
const asyncESM = require('internal/process/esm_loader');
41-
const { kStdout, kStderr, print } = require('internal/util/print');
4242
asyncESM.loaderPromise.then(async (loader) => {
4343
const { result } = await loader.eval(source);
44-
if (printResult) { print(kStdout, result); }
44+
if (print) {
45+
log(result);
46+
}
4547
})
4648
.catch((e) => {
4749
decorateErrorStack(e);
48-
print(kStderr, e);
50+
error(e);
4951
process.exit(1);
5052
});
5153
}
5254

53-
function evalScript(name, body, breakFirstLine, printResult) {
55+
function evalScript(name, body, breakFirstLine, print) {
5456
const CJSModule = require('internal/modules/cjs/loader');
5557
const { kVmBreakFirstLineSymbol } = require('internal/util');
5658

@@ -76,9 +78,9 @@ function evalScript(name, body, breakFirstLine, printResult) {
7678
[kVmBreakFirstLineSymbol]: ${!!breakFirstLine}
7779
});\n`;
7880
const result = module._compile(script, `${name}-wrapper`);
79-
if (printResult) {
80-
const { kStdout, print } = require('internal/util/print');
81-
print(kStdout, result);
81+
if (print) {
82+
const { log } = require('internal/console/global');
83+
log(result);
8284
}
8385

8486
if (origModule !== undefined)

lib/internal/util/print.js

Lines changed: 0 additions & 67 deletions
This file was deleted.

node.gyp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@
184184
'lib/internal/url.js',
185185
'lib/internal/util.js',
186186
'lib/internal/util/comparisons.js',
187-
'lib/internal/util/print.js',
188187
'lib/internal/util/debuglog.js',
189188
'lib/internal/util/inspect.js',
190189
'lib/internal/util/inspector.js',

0 commit comments

Comments
 (0)