Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 53 additions & 3 deletions lib/internal/util/inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -1305,13 +1305,47 @@ function identicalSequenceRange(a, b) {
len++;
}
if (len > 3) {
return { len, offset: i };
return [len, i];
}
}
}
}

return { len: 0, offset: 0 };
return [0, 0];
}

function getDuplicateErrorFrameRanges(frames) {
const result = [];
for (let i = 0; i < frames.length - 3; i++) {
// Find the first duplicate frame.
const pos = frames.indexOf(frames[i], i + 1);
const range = pos - i;
if (pos !== -1) {
let duplicateRanges = 0;
// Compare ranges line-by-line
for (let nextStart = i + range; nextStart <= frames.length - range; nextStart += range) {
let allEqual = true;
for (let j = 0; j < range; j++) {
if (frames[i + j] !== frames[nextStart + j]) {
allEqual = false;
break;
}
}
if (allEqual) {
// Matched. We have a duplicate range
duplicateRanges++;
} else {
break;
}
}
if (duplicateRanges * range >= 3) {
result.push([i + range, range, duplicateRanges]);
i += range * duplicateRanges + 2;
}
}
}

return result;
}

function getStackString(ctx, error) {
Expand Down Expand Up @@ -1345,14 +1379,30 @@ function getStackFrames(ctx, err, stack) {
const causeStackStart = StringPrototypeIndexOf(causeStack, '\n at');
if (causeStackStart !== -1) {
const causeFrames = StringPrototypeSplit(StringPrototypeSlice(causeStack, causeStackStart + 1), '\n');
const { len, offset } = identicalSequenceRange(frames, causeFrames);
const { 0: len, 1: offset } = identicalSequenceRange(frames, causeFrames);
if (len > 0) {
const skipped = len - 2;
const msg = ` ... ${skipped} lines matching cause stack trace ...`;
frames.splice(offset + 1, skipped, ctx.stylize(msg, 'undefined'));
}
}
}

// Remove recursive repetitive stack frames in long stacks
if (frames.length > 10) {
const ranges = getDuplicateErrorFrameRanges(frames);

while (ranges.length) {
const { 0: offset, 1: len, 2: duplicateRanges } = ranges.pop();
const msg = ` ... collapsed ${len * duplicateRanges} duplicate lines ` +
'matching above ' +
(duplicateRanges > 1 ?
`${len} lines ${duplicateRanges} times...` :
'lines ...');
frames.splice(offset, len * duplicateRanges, ctx.stylize(msg, 'undefined'));
}
}

return frames;
}

Expand Down
81 changes: 81 additions & 0 deletions test/parallel/test-util-inspect.js
Original file line number Diff line number Diff line change
Expand Up @@ -2920,6 +2920,87 @@ assert.strictEqual(
process.cwd = originalCWD;
}

{
// Use a fake stack to verify the expected colored outcome.
const err = new Error('Hide duplicate frames in long stack');
err.stack = [
'Error: Hide duplicate frames in long stack',
' at A.<anonymous> (/foo/node_modules/bar/baz.js:2:7)',
' at A.<anonymous> (/foo/node_modules/bar/baz.js:2:7)',
' at Module._compile (node:internal/modules/cjs/loader:827:30)',
' at Fancy (node:vm:697:32)',
' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Fancy (node:vm:697:32)',
' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Function.Module._load (node:internal/modules/cjs/loader:621:3)',
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)',
' at require (node:internal/modules/helpers:14:16)',
' at Array.forEach (<anonymous>)',
' at require (node:internal/modules/helpers:14:16)',
' at Array.forEach (<anonymous>)',
` at foobar/test/parallel/test-util-inspect.js:2760:12`,
` at Object.<anonymous> (foobar/node_modules/m/folder/file.js:2753:10)`,
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)',
' at require (node:internal/modules/helpers:14:16)',
' at Array.forEach (<anonymous>)',
` at foobar/test/parallel/test-util-inspect.js:2760:12`,
` at Object.<anonymous> (foobar/node_modules/m/folder/file.js:2753:10)`,
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)',
' at require (node:internal/modules/helpers:14:16)',
' at Array.forEach (<anonymous>)',
` at foobar/test/parallel/test-util-inspect.js:2760:12`,
` at Object.<anonymous> (foobar/node_modules/m/folder/file.js:2753:10)`,
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)',
' at require (node:internal/modules/helpers:14:16)',
' at Array.forEach (<anonymous>)',
` at foobar/test/parallel/test-util-inspect.js:2760:12`,
` at Object.<anonymous> (foobar/node_modules/m/folder/file.js:2753:10)`,
' at /test/test-util-inspect.js:2239:9',
' at getActual (node:assert:592:5)',
' at /test/test-util-inspect.js:2239:9',
' at getActual (node:assert:592:5)',
' at /test/test-util-inspect.js:2239:9',
' at getActual (node:assert:592:5)',
].join('\n');

assert.strictEqual(
util.inspect(err, { colors: true }),
'Error: Hide duplicate frames in long stack\n' +
' at A.<anonymous> (/foo/node_modules/\x1B[4mbar\x1B[24m/baz.js:2:7)\n' +
' at A.<anonymous> (/foo/node_modules/\x1B[4mbar\x1B[24m/baz.js:2:7)\n' +
'\x1B[90m at Module._compile (node:internal/modules/cjs/loader:827:30)\x1B[39m\n' +
'\x1B[90m at Fancy (node:vm:697:32)\x1B[39m\n' +
' at tryModuleLoad (node:internal/modules/cjs/foo:629:12)\n' +
'\x1B[90m at Function.Module._load (node:internal/modules/cjs/loader:621:3)\x1B[39m\n' +
'\x1B[90m ... collapsed 3 duplicate lines matching above lines ...\x1B[39m\n' +
'\x1B[90m at Function.Module._load (node:internal/modules/cjs/loader:621:3)\x1B[39m\n' +
'\x1B[90m ... collapsed 5 duplicate lines matching above 1 lines 5 times...\x1B[39m\n' +
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)\n' +
'\x1B[90m at require (node:internal/modules/helpers:14:16)\x1B[39m\n' +
' at Array.forEach (<anonymous>)\n' +
'\x1B[90m at require (node:internal/modules/helpers:14:16)\x1B[39m\n' +
' at Array.forEach (<anonymous>)\n' +
' at foobar/test/parallel/test-util-inspect.js:2760:12\n' +
' at Object.<anonymous> (foobar/node_modules/\x1B[4mm\x1B[24m/folder/file.js:2753:10)\n' +
' at Module.require [as weird/name] (node:internal/aaaaa/loader:735:19)\n' +
'\x1B[90m ... collapsed 10 duplicate lines matching above 5 lines 2 times...\x1B[39m\n' +
'\x1B[90m at require (node:internal/modules/helpers:14:16)\x1B[39m\n' +
' at Array.forEach (<anonymous>)\n' +
' at foobar/test/parallel/test-util-inspect.js:2760:12\n' +
' at Object.<anonymous> (foobar/node_modules/\x1B[4mm\x1B[24m/folder/file.js:2753:10)\n' +
' at /test/test-util-inspect.js:2239:9\n' +
'\x1B[90m at getActual (node:assert:592:5)\x1B[39m\n' +
'\x1B[90m ... collapsed 4 duplicate lines matching above 2 lines 2 times...\x1B[39m',
);
}

{
// Cross platform checks.
const err = new Error('foo');
Expand Down
Loading