Skip to content

Commit 727339e

Browse files
bnoordhuisBridgeAR
authored andcommitted
repl: fix util.inspect() coloring regression
The `Object.assign()` calls introduced in commit 90a4390 ("repl: show proxies as Proxy objects") mutated their first argument, causing the `{ colors: true }` option from the REPL to leak over into the global `util.inspect.defaultOptions` object. Refs: #16485 (comment) PR-URL: #17565 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de> Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Jeremiah Senkpiel <fishrock123@rocketmail.com> Reviewed-By: Luigi Pinca <luigipinca@gmail.com>
1 parent 88a9e2d commit 727339e

File tree

2 files changed

+33
-2
lines changed

2 files changed

+33
-2
lines changed

lib/repl.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ function hasOwnProperty(obj, prop) {
103103
// This is the default "writer" value if none is passed in the REPL options.
104104
const writer = exports.writer = (obj) => util.inspect(obj, writer.options);
105105
writer.options =
106-
Object.assign(util.inspect.defaultOptions, { showProxy: true });
106+
Object.assign({}, util.inspect.defaultOptions, { showProxy: true });
107107

108108
exports._builtinLibs = internalModule.builtinLibs;
109109

@@ -470,7 +470,7 @@ function REPLServer(prompt,
470470
if (self.useColors && self.writer === writer) {
471471
// Turn on ANSI coloring.
472472
self.writer = (obj) => util.inspect(obj, self.writer.options);
473-
self.writer.options = Object.assign(writer.options, { colors: true });
473+
self.writer.options = Object.assign({}, writer.options, { colors: true });
474474
}
475475

476476
function filterInternalStackFrames(error, structuredStack) {

test/parallel/test-repl-colors.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/* eslint-disable quotes */
2+
'use strict';
3+
require('../common');
4+
const { Duplex } = require('stream');
5+
const { inspect } = require('util');
6+
const { strictEqual } = require('assert');
7+
const { REPLServer } = require('repl');
8+
9+
let output = '';
10+
11+
const inout = new Duplex({ decodeStrings: false });
12+
inout._read = function() {
13+
this.push('util.inspect("string")\n');
14+
this.push(null);
15+
};
16+
inout._write = function(s, _, cb) {
17+
output += s;
18+
cb();
19+
};
20+
21+
const repl = new REPLServer({ input: inout, output: inout, useColors: true });
22+
23+
process.on('exit', function() {
24+
// https://github.com/nodejs/node/pull/16485#issuecomment-350428638
25+
// The color setting of the REPL should not have leaked over into
26+
// the color setting of `util.inspect.defaultOptions`.
27+
strictEqual(output.includes(`'\\'string\\''`), true);
28+
strictEqual(output.includes(`'\u001b[32m\\'string\\'\u001b[39m'`), false);
29+
strictEqual(inspect.defaultOptions.colors, false);
30+
strictEqual(repl.writer.options.colors, true);
31+
});

0 commit comments

Comments
 (0)