Skip to content

Commit

Permalink
util: mark cwd grey while inspecting errors
Browse files Browse the repository at this point in the history
This changes the util.inspect() output for errors in case stack
traces contain the current working directory in their trace.
If that's the case, the cwd part is marked grey to focus on the
rest of the path.

Signed-off-by: Ruben Bridgewater <[email protected]>
  • Loading branch information
BridgeAR committed Dec 4, 2021
1 parent ea65e1f commit 73bcb9f
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 75 deletions.
36 changes: 6 additions & 30 deletions lib/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
'use strict';

const {
ArrayPrototypeIndexOf,
ArrayPrototypeJoin,
ArrayPrototypeShift,
ArrayPrototypeSlice,
Expand All @@ -32,7 +31,6 @@ const {
ErrorCaptureStackTrace,
FunctionPrototypeBind,
FunctionPrototypeCall,
MathMin,
NumberIsNaN,
ObjectCreate,
ObjectDefineProperty,
Expand All @@ -50,7 +48,10 @@ const {
SymbolAsyncIterator,
} = primordials;
const kRejection = SymbolFor('nodejs.rejection');
const { inspect } = require('internal/util/inspect');
const {
inspect,
identicalSequenceRange
} = require('internal/util/inspect');

let spliceOne;

Expand Down Expand Up @@ -282,31 +283,6 @@ EventEmitter.prototype.getMaxListeners = function getMaxListeners() {
return _getMaxListeners(this);
};

// Returns the length and line number of the first sequence of `a` that fully
// appears in `b` with a length of at least 4.
function identicalSequenceRange(a, b) {
for (let i = 0; i < a.length - 3; i++) {
// Find the first entry of b that matches the current entry of a.
const pos = ArrayPrototypeIndexOf(b, a[i]);
if (pos !== -1) {
const rest = b.length - pos;
if (rest > 3) {
let len = 1;
const maxLen = MathMin(a.length - i, rest);
// Count the number of consecutive entries.
while (maxLen > len && a[i + len] === b[pos + len]) {
len++;
}
if (len > 3) {
return [len, i];
}
}
}
}

return [0, 0];
}

function enhanceStackTrace(err, own) {
let ctorInfo = '';
try {
Expand All @@ -321,9 +297,9 @@ function enhanceStackTrace(err, own) {
const ownStack = ArrayPrototypeSlice(
StringPrototypeSplit(own.stack, '\n'), 1);

const { 0: len, 1: off } = identicalSequenceRange(ownStack, errStack);
const { len, offset } = identicalSequenceRange(ownStack, errStack);
if (len > 0) {
ArrayPrototypeSplice(ownStack, off + 1, len - 2,
ArrayPrototypeSplice(ownStack, offset + 1, len - 2,
' [... lines matching original stack trace ...]');
}

Expand Down
66 changes: 54 additions & 12 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1259,6 +1259,49 @@ function removeDuplicateErrorKeys(ctx, keys, err, stack) {
}
}

function markNodeModules(ctx, line) {
let tempLine = '';
let nodeModule;
let pos = 0;
while (nodeModule = nodeModulesRegExp.exec(line)) {
// '/node_modules/'.length === 14
tempLine += line.slice(pos, nodeModule.index + 14);
tempLine += ctx.stylize(nodeModule[1], 'module');
pos = nodeModule.index + nodeModule[0].length;
}
if (pos !== 0) {
line = tempLine + line.slice(pos);
}
return line;
}

function markCwd(ctx, line, workingDirectory) {
const cwdStartPos = line.indexOf(workingDirectory);
let tempLine = '';
if (cwdStartPos !== -1) {
const start = line[cwdStartPos - 1] === '(' ? cwdStartPos - 1 : cwdStartPos;
const end = start !== cwdStartPos && line.endsWith(')') ? -1 : line.length;
const workingDirectoryEndPos = cwdStartPos + workingDirectory.length;
const cwdSlice = line.slice(start, workingDirectoryEndPos);

tempLine += line.slice(0, start);
tempLine += ctx.stylize(cwdSlice, 'undefined');
tempLine += line.slice(workingDirectoryEndPos, end);
if (end === -1) {
tempLine += ctx.stylize(')', 'undefined');
}
} else {
tempLine += line;
}
return tempLine;
}

function safeGetCWD() {
try {
return process.cwd();
} catch {}
}

function formatError(err, constructor, tag, ctx, keys) {
const name = err.name != null ? String(err.name) : 'Error';
let stack = getStackString(err);
Expand All @@ -1284,22 +1327,20 @@ function formatError(err, constructor, tag, ctx, keys) {
const lines = getStackFrames(ctx, err, stack.slice(stackStart + 1));
if (ctx.colors) {
// Highlight userland code and node modules.
for (const line of lines) {
const workingDirectory = safeGetCWD();
for (let line of lines) {
const core = line.match(coreModuleRegExp);
if (core !== null && NativeModule.exists(core[1])) {
newStack += `\n${ctx.stylize(line, 'undefined')}`;
} else {
// This adds underscores to all node_modules to quickly identify them.
let nodeModule;
newStack += '\n';
let pos = 0;
while (nodeModule = nodeModulesRegExp.exec(line)) {
// '/node_modules/'.length === 14
newStack += line.slice(pos, nodeModule.index + 14);
newStack += ctx.stylize(nodeModule[1], 'module');
pos = nodeModule.index + nodeModule[0].length;

line = markNodeModules(ctx, line);
if (workingDirectory !== undefined) {
line = markCwd(ctx, line, workingDirectory);
}
newStack += pos === 0 ? line : line.slice(pos);

newStack += line;
}
}
} else {
Expand Down Expand Up @@ -2255,10 +2296,11 @@ function stripVTControlCharacters(str) {
}

module.exports = {
identicalSequenceRange,
inspect,
inspectDefaultOptions,
format,
formatWithOptions,
getStringWidth,
inspectDefaultOptions,
stripVTControlCharacters
stripVTControlCharacters,
};
83 changes: 55 additions & 28 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2730,35 +2730,62 @@ assert.strictEqual(
}

{
// Use a fake stack to verify the expected colored outcome.
const stack = [
'TypedError: Wonderful message!',
' at A.<anonymous> (/test/node_modules/foo/node_modules/bar/baz.js:2:7)',
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
' at Fancy (node:vm:697:32)',
// This file is not an actual Node.js core file.
' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
// This file is not an actual Node.js core file.
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)',
' at require (node:internal/modules/cjs/helpers:14:16)',
' at /test/test-util-inspect.js:2239:9',
' at getActual (node:assert:592:5)',
];
const isNodeCoreFile = [
false, false, true, true, false, true, false, true, false, true,
];
const err = new TypeError('Wonderful message!');
err.stack = stack.join('\n');
util.inspect(err, { colors: true }).split('\n').forEach((line, i) => {
let actual = stack[i].replace(/node_modules\/([a-z]+)/g, (a, m) => {
return `node_modules/\u001b[4m${m}\u001b[24m`;
});
if (isNodeCoreFile[i]) {
actual = `\u001b[90m${actual}\u001b[39m`;
const originalCWD = process.cwd();

for (const os of ['windows', 'unix']) {
process.cwd = () => (os === 'windows' ?
'C:\\workspace\\node-test-binary-windows-js-suites\\node' :
'/home/user/repository/node');

// Use a fake stack to verify the expected colored outcome.
const stack = [
'TypedError: Wonderful message!',
' at A.<anonymous> (/t/node_modules/foo/node_modules/bar/baz.js:2:7)',
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
' at Fancy (node:vm:697:32)',
// This file is not an actual Node.js core file.
' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
// This file is not an actual Node.js core file.
' at Module.require [as weird/name] (node:internal/aaa/loader:735:19)',
' at require (node:internal/modules/cjs/helpers:14:16)',
' at Array.forEach (<anonymous>)',
` at ${process.cwd()}/test/parallel/test-util-inspect.js:2760:12`,
` at Object.<anonymous> (${process.cwd()}/node_modules/hyper_module` +
'/folder/file.js:2753:10)',
' at /test/test-util-inspect.js:2239:9',
' at getActual (node:assert:592:5)',
];
const err = new TypeError('Wonderful message!');
err.stack = stack.join('\n');
if (os === 'windows') {
err.stack = stack.map((frame) => (frame.includes('node:') ?
frame :
frame.replaceAll('/', '\\'))
).join('\n');
}
assert.strictEqual(actual, line);
});
const escapedCWD = util.inspect(process.cwd()).slice(1, -1);
util.inspect(err, { colors: true }).split('\n').forEach((line, i) => {
let expected = stack[i].replace(/node_modules\/([^/]+)/gi, (_, m) => {
return `node_modules/\u001b[4m${m}\u001b[24m`;
}).replaceAll(new RegExp(`(\\(?${escapedCWD})`, 'gi'), (_, m) => {
return `\x1B[90m${m}\x1B[39m`;
});
if (expected.includes(process.cwd()) && expected.endsWith(')')) {
expected = `${expected.slice(0, -1)}\x1B[90m)\x1B[39m`;
}
if (line.includes('node:')) {
if (!line.includes('foo') && !line.includes('aaa')) {
expected = `\u001b[90m${expected}\u001b[39m`;
}
} else if (os === 'windows') {
expected = expected.replaceAll('/', '\\');
}
assert.strictEqual(line, expected);
});
}

process.cwd = originalCWD;
}

{
Expand Down
8 changes: 4 additions & 4 deletions test/pseudo-tty/console_colors.out
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ string q
Error: test
at abc (../fixtures/node_modules/bar.js:4:4)
foobar
at * (*console_colors.js:*:*)
at *[90m(*console_colors.js:*:*[90m)*[39m
*[90m at * (node:internal*:*:*)*[39m
*[90m at *[39m
*[90m at *[39m
Expand All @@ -14,22 +14,22 @@ foobar
*[90m at *[39m

Error: Should not ever get here.
at * (*node_modules*[4m*node_modules*[24m*bar.js:*:*)
at *[90m(*node_modules*[4m*node_modules*[24m*bar.js:*:*[90m)*[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
at * (*console_colors.js:*:*)
at *[90m(*console_colors.js:*:*[90m)*[39m
*[90m at *[39m
*[90m at *[39m

Error
at evalmachine.<anonymous>:*:*
*[90m at Script.runInThisContext (node:vm:*:*)*[39m
*[90m at Object.runInThisContext (node:vm:*:*)*[39m
at * (*console_colors.js:*:*)
at *[90m(*console_colors.js:*:*[90m)*[39m
*[90m at *[39m
*[90m at *[39m
*[90m at *[39m
Expand Down
2 changes: 1 addition & 1 deletion test/pseudo-tty/test-fatal-error.out
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ throw err;
^

TypeError: foobar
at Object.<anonymous> (*test-fatal-error.js:*)
at Object.<anonymous> [90m(*test-fatal-error.js:*[90m)[39m
*[90m at *(node:internal*loader:*:*)*[39m
*[90m at *(node:internal*loader:*:*)*[39m
*[90m at *(node:internal*loader:*:*)*[39m
Expand Down

0 comments on commit 73bcb9f

Please sign in to comment.