Skip to content

Commit 12f8e66

Browse files
committed
src: fix node_report stacks to include last frame
The loop that splits error.stack into the report's javascriptStack.stack only emitted a line once it found the following newline, so the final frame was always dropped. Signed-off-by: James M Snell <jasnell@gmail.com> Assisted-By: Claude PR-URL: #67715 Reviewed-By: Matteo Collina <matteo.collina@gmail.com> Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
1 parent 8ec2665 commit 12f8e66

3 files changed

Lines changed: 60 additions & 18 deletions

File tree

‎src/node_report.cc‎

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -568,23 +568,23 @@ static void PrintJavaScriptErrorStack(JSONWriter* writer,
568568
return;
569569
}
570570

571-
int line = ss.find('\n');
572-
if (line == -1) {
573-
writer->json_keyvalue("message", ss);
571+
std::string_view rest = ss;
572+
size_t line_end = rest.find('\n');
573+
if (line_end == std::string_view::npos) {
574+
writer->json_keyvalue("message", rest);
574575
} else {
575-
std::string l = ss.substr(0, line);
576-
writer->json_keyvalue("message", l);
576+
writer->json_keyvalue("message", rest.substr(0, line_end));
577577
writer->json_arraystart("stack");
578-
ss = ss.substr(line + 1);
579-
line = ss.find('\n');
580-
while (line != -1) {
581-
l = ss.substr(0, line);
582-
l.erase(l.begin(), std::ranges::find_if(l, [](int ch) {
583-
return !std::iswspace(ch);
584-
}));
578+
rest.remove_prefix(line_end + 1);
579+
while (!rest.empty()) {
580+
line_end = rest.find('\n');
581+
std::string_view l = rest.substr(0, line_end);
582+
rest.remove_prefix(line_end == std::string_view::npos ? rest.size()
583+
: line_end + 1);
584+
const auto first = std::ranges::find_if(
585+
l, [](unsigned char ch) { return !std::iswspace(ch); });
586+
l.remove_prefix(first - l.begin());
585587
writer->json_element(l);
586-
ss = ss.substr(line + 1);
587-
line = ss.find('\n');
588588
}
589589
writer->json_arrayend();
590590
}

‎test/parallel/test-hide-internal-stack-frames.js‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,18 @@ const checkHidden = {
3636
assert.match(output, /\[eval\]:4:/);
3737
// The generated [eval]-wrapper frame is folded in with the internals.
3838
assert.match(output, /\[eval\]:4:\d+\)?\n {4}at <node:internal> \(\d+ frames hidden\)\n/);
39-
}, 3),
39+
}, process.config.variables.node_without_node_options ? 2 : 3),
4040
};
4141

4242
spawnSyncAndAssert(process.execPath,
4343
['--hide-internal-stack-frames', '-e', script],
4444
checkHidden);
4545

46-
spawnSyncAndAssert(process.execPath, ['-e', script], {
47-
env: { ...process.env, NODE_OPTIONS: '--hide-internal-stack-frames' },
48-
}, checkHidden);
46+
if (!process.config.variables.node_without_node_options) {
47+
spawnSyncAndAssert(process.execPath, ['-e', script], {
48+
env: { ...process.env, NODE_OPTIONS: '--hide-internal-stack-frames' },
49+
}, checkHidden);
50+
}
4951

5052
// Works together with --enable-source-maps.
5153
spawnSyncAndAssert(process.execPath,
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict';
2+
3+
// Tests that every line of error.stack after the message, including the
4+
// last one, is included in the report's javascriptStack.stack.
5+
6+
require('../common');
7+
const assert = require('assert');
8+
9+
function stackOf(stack) {
10+
const err = new Error('boom');
11+
err.stack = stack;
12+
return process.report.getReport(err).javascriptStack;
13+
}
14+
15+
{
16+
const { message, stack } = stackOf(
17+
'Error: boom\n at a (file.js:1:1)\n at b (file.js:2:2)');
18+
assert.strictEqual(message, 'Error: boom');
19+
assert.deepStrictEqual(stack, ['at a (file.js:1:1)', 'at b (file.js:2:2)']);
20+
}
21+
22+
{
23+
// A trailing newline does not produce an empty frame.
24+
const { stack } = stackOf('Error: boom\n at a (file.js:1:1)\n');
25+
assert.deepStrictEqual(stack, ['at a (file.js:1:1)']);
26+
}
27+
28+
{
29+
const { message, stack } = stackOf('Error: boom');
30+
assert.strictEqual(message, 'Error: boom');
31+
assert.strictEqual(stack, undefined);
32+
}
33+
34+
{
35+
// A real stack is reported in full, including its last frame.
36+
const err = new Error('real');
37+
const lines = err.stack.split('\n').slice(1).map((line) => line.trim());
38+
const { stack } = process.report.getReport(err).javascriptStack;
39+
assert.deepStrictEqual(stack, lines);
40+
}

0 commit comments

Comments
 (0)