Commit 12f8e665ea9 for nodejs
commit 12f8e665ea915b0c791688dcd4d8c8aca225e23b
Author: James M Snell <jasnell@gmail.com>
Date: Sun Nov 22 23:17:42 2026 +0000
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: https://github.com/nodejs/node/pull/67715
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Marco Ippolito <marcoippolito54@gmail.com>
diff --git a/src/node_report.cc b/src/node_report.cc
index 28dd0ff3ca2..58a1f4e0045 100644
--- a/src/node_report.cc
+++ b/src/node_report.cc
@@ -568,23 +568,23 @@ static void PrintJavaScriptErrorStack(JSONWriter* writer,
return;
}
- int line = ss.find('\n');
- if (line == -1) {
- writer->json_keyvalue("message", ss);
+ std::string_view rest = ss;
+ size_t line_end = rest.find('\n');
+ if (line_end == std::string_view::npos) {
+ writer->json_keyvalue("message", rest);
} else {
- std::string l = ss.substr(0, line);
- writer->json_keyvalue("message", l);
+ writer->json_keyvalue("message", rest.substr(0, line_end));
writer->json_arraystart("stack");
- ss = ss.substr(line + 1);
- line = ss.find('\n');
- while (line != -1) {
- l = ss.substr(0, line);
- l.erase(l.begin(), std::ranges::find_if(l, [](int ch) {
- return !std::iswspace(ch);
- }));
+ rest.remove_prefix(line_end + 1);
+ while (!rest.empty()) {
+ line_end = rest.find('\n');
+ std::string_view l = rest.substr(0, line_end);
+ rest.remove_prefix(line_end == std::string_view::npos ? rest.size()
+ : line_end + 1);
+ const auto first = std::ranges::find_if(
+ l, [](unsigned char ch) { return !std::iswspace(ch); });
+ l.remove_prefix(first - l.begin());
writer->json_element(l);
- ss = ss.substr(line + 1);
- line = ss.find('\n');
}
writer->json_arrayend();
}
diff --git a/test/parallel/test-hide-internal-stack-frames.js b/test/parallel/test-hide-internal-stack-frames.js
index 1380ef467cd..ba0a0a6d0d1 100644
--- a/test/parallel/test-hide-internal-stack-frames.js
+++ b/test/parallel/test-hide-internal-stack-frames.js
@@ -36,16 +36,18 @@ const checkHidden = {
assert.match(output, /\[eval\]:4:/);
// The generated [eval]-wrapper frame is folded in with the internals.
assert.match(output, /\[eval\]:4:\d+\)?\n {4}at <node:internal> \(\d+ frames hidden\)\n/);
- }, 3),
+ }, process.config.variables.node_without_node_options ? 2 : 3),
};
spawnSyncAndAssert(process.execPath,
['--hide-internal-stack-frames', '-e', script],
checkHidden);
-spawnSyncAndAssert(process.execPath, ['-e', script], {
- env: { ...process.env, NODE_OPTIONS: '--hide-internal-stack-frames' },
-}, checkHidden);
+if (!process.config.variables.node_without_node_options) {
+ spawnSyncAndAssert(process.execPath, ['-e', script], {
+ env: { ...process.env, NODE_OPTIONS: '--hide-internal-stack-frames' },
+ }, checkHidden);
+}
// Works together with --enable-source-maps.
spawnSyncAndAssert(process.execPath,
diff --git a/test/report/test-report-error-stack.js b/test/report/test-report-error-stack.js
new file mode 100644
index 00000000000..9c401a3c655
--- /dev/null
+++ b/test/report/test-report-error-stack.js
@@ -0,0 +1,40 @@
+'use strict';
+
+// Tests that every line of error.stack after the message, including the
+// last one, is included in the report's javascriptStack.stack.
+
+require('../common');
+const assert = require('assert');
+
+function stackOf(stack) {
+ const err = new Error('boom');
+ err.stack = stack;
+ return process.report.getReport(err).javascriptStack;
+}
+
+{
+ const { message, stack } = stackOf(
+ 'Error: boom\n at a (file.js:1:1)\n at b (file.js:2:2)');
+ assert.strictEqual(message, 'Error: boom');
+ assert.deepStrictEqual(stack, ['at a (file.js:1:1)', 'at b (file.js:2:2)']);
+}
+
+{
+ // A trailing newline does not produce an empty frame.
+ const { stack } = stackOf('Error: boom\n at a (file.js:1:1)\n');
+ assert.deepStrictEqual(stack, ['at a (file.js:1:1)']);
+}
+
+{
+ const { message, stack } = stackOf('Error: boom');
+ assert.strictEqual(message, 'Error: boom');
+ assert.strictEqual(stack, undefined);
+}
+
+{
+ // A real stack is reported in full, including its last frame.
+ const err = new Error('real');
+ const lines = err.stack.split('\n').slice(1).map((line) => line.trim());
+ const { stack } = process.report.getReport(err).javascriptStack;
+ assert.deepStrictEqual(stack, lines);
+}