Skip to content

Truncate source lines in error messages - #9832

Merged
Jarred-Sumner merged 7 commits into
mainfrom
jarred/truncate-source-text-in-errors
Apr 3, 2024
Merged

Jarred-Sumner merged 7 commits into
mainfrom
jarred/truncate-source-text-in-errors

Conversation

@Jarred-Sumner

@Jarred-Sumner Jarred-Sumner commented Apr 2, 2024 •

Copy link
Copy Markdown
Collaborator

What does this PR do?

When a minified file throws an exception in Bun, the source lines can be extremely long

This causes two problems:

  1. The divot line has a comically large amount of indentation
  2. It fills up your terminal with unreadable code

This PR truncates source text to be up to 1024 bytes per line. The divot will no longer appear if the line is > 512 bytes. The goal is to avoid the above two problems without debugging harder. So we use a probably longer than necessary limit.

These numbers are arbitrary and a better solution overall would be to "scroll" within the lines to the relevant parts. I don't trust our source maps are reliable enough right now to do that well, so this is a compromise until then.

After:

image

Before:

image

With lots of scrolling:

image

How did you verify your code works?

There are a couple snapshot tests

@Jarred-Sumner
Jarred-Sumner force-pushed the jarred/truncate-source-text-in-errors branch from bca5993 to b0c293f Compare April 2, 2024 07:40
@github-actions

github-actions Bot commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

@github-actions

github-actions Bot commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

Comment thread CMakeLists.txt
cmake_policy(SET CMP0067 NEW)

set(Bun_VERSION "1.1.0")
set(Bun_VERSION "1.1.1")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've seen a few diff v1_1_1 things or was that a 1_1_0 thing. This should be in a sep PR though as it's versioning related.

Comment thread src/bun.js/javascript.zig
// This is a longer number than necessary because we don't handle this case very well
// At the very least, we shouldn't dump 100 KB of minified code into your terminal.
const max_line_length_with_divot = 512;
const max_line_length = 1024;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with less. Usually a line is like 80 to 120 chars.

Comment thread src/bun.js/javascript.zig
bun.fmt.fmtJavaScript(std.mem.trimRight(u8, std.mem.trim(u8, source.text.slice(), "\n"), "\t "), allow_ansi_color),
},
);
const trimmed = std.mem.trimRight(u8, std.mem.trim(u8, source.text.slice(), "\n"), "\t ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The snip to \n covers \r\n too (is my assumption)

Comment thread src/bun.js/javascript.zig
const text = trimmed[0..@min(trimmed.len, max_line_length)];

if (text.len != trimmed.len) {
const fmt = if (comptime allow_ansi_color) "<r><d> | ... truncated <r>\n" else "\n";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can has ellipsis here? “…” (U+2026). (if it's okay for Windows)

Comment thread src/bun.js/javascript.zig
defer source.text.deinit();
const text = source.text.slice();
const remainder = std.mem.trimRight(u8, std.mem.trim(u8, text, "\n"), "\t ");
const trimmed = std.mem.trimRight(u8, std.mem.trim(u8, text, "\n"), "\t ");

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This timming is done twice (turn into an inlined function?)

Comment thread src/bun.js/javascript.zig
if (!top.position.isInvalid()) {
const indent = max_line_number_pad + " | ".len + @as(u64, @intCast(top.position.column_start));
if (clamped.len != trimmed.len) {
const fmt = if (comptime allow_ansi_color) "<r><d> | ... truncated <r>\n\n" else "\n\n";

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ellipsis?

* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
'use strict';var l=Symbol.for("react.element"),n=Symbol.for("react.portal"),p=Symbol.for("react.fragment"),q=Symbol.for("react.strict_mode"),r=Symbol.for("react.profiler"),t=Symbol.for("react.provider"),u=Symbol.for("react.context"),v=Symbol.for("react.forward_ref"),w=Symbol.for("react.suspense"),x=Symbol.for("react.memo"),y=Symbol.for("react.lazy"),z=Symbol.iterator;function A(a){if(null===a||"object"!==typeof a)return null;a=z&&a[z]||a["@@iterator"];return"function"===typeof a?a:null}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given the xz minified/obfuscated payload test stuff in the headlines for ease of future us and transparency could we just minify our own test file via our API.

test("Error inside minified file (color) ", () => {
try {
require("./inspect-error-fixture.min.js");
expect.unreachable();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing documentation for expect.unreachable(). (would be nice to know which API we are mimicking (e.g. jest, vitest, etc).
#7131

@jdalton

jdalton commented Apr 2, 2024

Copy link
Copy Markdown
Contributor

I love these kinds of enhancements ❤️

@github-actions

github-actions Bot commented Apr 2, 2024 •

Copy link
Copy Markdown
Contributor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants