From 8b774531c290dac5eeb35cd62b7bce517676a2e5 Mon Sep 17 00:00:00 2001 From: Peter Steinberger Date: Mon, 5 Oct 2026 03:11:01 -0700 Subject: [PATCH] fix(runtime): preserve syntax-selected stack positions --- CHANGELOG.md | 1 + docs/runtime/nodejs-compat.mdx | 2 +- src/ast/e.rs | 7 ++ src/ast/expr.rs | 4 + src/ast/fold_string_addition.rs | 1 + src/ast/known_global.rs | 7 +- .../generateCodeForLazyExport.rs | 1 + src/js_parser/lower/lower_decorators.rs | 2 + src/js_parser/p.rs | 20 +++ src/js_parser/parse/parse_prefix.rs | 3 +- src/js_parser/parse/parse_suffix.rs | 37 +++++- src/js_parser/parse/parse_typescript.rs | 1 + src/js_parser/visit/mod.rs | 1 + src/js_parser/visit/visit_expr.rs | 9 +- src/js_parser/visit/visit_stmt.rs | 2 + src/js_printer/lib.rs | 18 +++ src/jsc/RuntimeTranspilerCache.rs | 4 +- src/jsc/bindings/ErrorStackFrame.cpp | 19 +-- src/jsc/bindings/ErrorStackFrame.h | 4 +- src/jsc/bindings/ErrorStackTrace.cpp | 2 +- src/jsc/bindings/FormatStackTraceForJS.cpp | 2 +- src/jsc/bindings/ZigException.cpp | 2 +- src/react_compiler/codegen.rs | 8 ++ test/bundler/bundler_minify.test.ts | 40 +++--- .../bun/jsc/webkit-upgrade-7b485a76e9.test.ts | 4 +- .../internal-sourcemap-roundtrip.test.ts | 2 +- .../bun/sourcemap/internal-sourcemap.test.ts | 10 +- test/js/bun/util/inspect.test.js | 2 +- .../js/node/module/node-module-module.test.js | 2 +- .../v8/callsite-async-position-fixture.mjs | 12 ++ .../v8/callsite-syntax-positions-fixture.js | 116 ++++++++++++++++++ .../callsite-typescript-positions-fixture.ts | 63 ++++++++++ test/js/node/v8/capture-stack-trace.test.js | 36 +++++- .../v8/error-prepare-stack-default-fixture.js | 4 +- .../v8/stack-position-regressions-fixture.js | 104 ++++++++++++++++ .../__snapshots__/vm-sourceUrl.test.ts.snap | 4 +- 36 files changed, 494 insertions(+), 62 deletions(-) create mode 100644 test/js/node/v8/callsite-async-position-fixture.mjs create mode 100644 test/js/node/v8/callsite-syntax-positions-fixture.js create mode 100644 test/js/node/v8/callsite-typescript-positions-fixture.ts create mode 100644 test/js/node/v8/stack-position-regressions-fixture.js diff --git a/CHANGELOG.md b/CHANGELOG.md index 4b5fe2f2bc99..47747ba11afa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -195,3 +195,4 @@ - Sync upstream nightly through `d4928764f23213ecf3cd61fa0b5b4a44369a5096`, configuring fetch TLS once per connection so pooled sockets can be reused during renegotiation without repeating session setup. - Sync upstream nightly through `9bd19c98eacc01530a4e7609bc427abffa87d77e`, preserving PostgreSQL query ordering through errors and limiting MySQL row-decoding failures to the affected query. - Support real byte-based `node:inspector` HeapProfiler allocation sampling with allocation-site trees, live profiles, and major/minor GC inclusion flags (requires the OpenClaw WebKit allocation sampler). +- Preserve built-in error constructor syntax and returning arrow frames, and use JSC syntax-selected call and property-read stack positions. Retain runtime callee parentheses and computed access, and map call, bracket, and template delimiters back to their original source. Continues the source-position fixes from [oven-sh/bun#35179](https://github.com/oven-sh/bun/pull/35179), [oven-sh/bun#37396](https://github.com/oven-sh/bun/pull/37396), and [oven-sh/bun#41580](https://github.com/oven-sh/bun/pull/41580). Thanks @robobun! diff --git a/docs/runtime/nodejs-compat.mdx b/docs/runtime/nodejs-compat.mdx index a368e2eb0736..1ed2a3dc9404 100644 --- a/docs/runtime/nodejs-compat.mdx +++ b/docs/runtime/nodejs-compat.mdx @@ -21,7 +21,7 @@ Module namespaces have a null prototype. `require(esm)` adds an own enumerable ` `Error.prepareStackTrace` supports deletion, descriptor restoration, and accessor replacement. Nested stack formatting and throwing formatters leave later captures usable. -`CallSite.getLineNumber()` and `getColumnNumber()` return one-based positions, or `null` when a frame has no source position. Stack strings include column 1 and use the same constructor positions as CallSites, pointing to the `new` keyword, including across line breaks and in `node:vm` scripts. Some expression positions still differ from Node.js because JavaScriptCore and Bun's transpiler select different syntax locations. +`CallSite.getLineNumber()` and `getColumnNumber()` return one-based positions, or `null` when a frame has no source position. Stack strings include column 1 and use the same constructor positions as CallSites, pointing to the `new` keyword, including across line breaks and in `node:vm` scripts. The matching OpenClaw engine supplies syntax-selected positions for calls, constructors, property reads, and async continuations. Built-in error constructors retain `new`, including in returning arrows. Runtime transpilation retains callee parentheses and computed access and maps opening call, bracket, and template delimiters to the original source. File-name and eval-origin formatting still follow Bun's existing conventions. `Intl.Segments.containing()` respects surrogate-pair boundaries for grapheme, word, and sentence segmentation with the pinned OpenClaw WebKit. diff --git a/src/ast/e.rs b/src/ast/e.rs index 520c12f6f077..6db08699ea5e 100644 --- a/src/ast/e.rs +++ b/src/ast/e.rs @@ -259,7 +259,9 @@ pub struct Call { pub args: ExprNodeList, pub optional_chain: Option, pub is_direct_eval: bool, + pub open_paren_loc: crate::Loc, pub close_paren_loc: crate::Loc, + pub preserve_parenthesized_target: bool, /// True if there is a comment containing "@__PURE__" or "#__PURE__" preceding /// this call expression. This is an annotation used for tree shaking, and @@ -281,7 +283,9 @@ impl Default for Call { args: bun_alloc::AstAlloc::vec(), optional_chain: None, is_direct_eval: false, + open_paren_loc: crate::Loc::EMPTY, close_paren_loc: crate::Loc::EMPTY, + preserve_parenthesized_target: false, can_be_unwrapped_if_unused: CallUnwrap::Never, was_jsx_element: false, } @@ -334,6 +338,7 @@ impl Default for Dot { } } pub struct Index { + pub open_bracket_loc: crate::Loc, pub index: ExprNodeIndex, pub target: ExprNodeIndex, pub optional_chain: Option, @@ -2114,6 +2119,7 @@ pub struct TemplatePart { } pub struct Template { + pub head_loc: crate::Loc, pub tag: Option, /// Arena-owned mutable slice. Stored as a /// `StoreSlice` so writers (`substitute_single_use_symbol_in_expr`, the @@ -2335,6 +2341,7 @@ impl Template { // provenance for downstream mutators. Expr::init( Template { + head_loc: crate::Loc::EMPTY, tag: None, parts: crate::StoreSlice::from_bump(parts), head: TemplateContents::Cooked( diff --git a/src/ast/expr.rs b/src/ast/expr.rs index cfedeb4f9bf8..f24c2239008b 100644 --- a/src/ast/expr.rs +++ b/src/ast/expr.rs @@ -2000,7 +2000,9 @@ impl Data { .try_deep_clone_with(|e| e.deep_clone_no_detach(bump))?, optional_chain: el.optional_chain, is_direct_eval: el.is_direct_eval, + open_paren_loc: el.open_paren_loc, close_paren_loc: el.close_paren_loc, + preserve_parenthesized_target: el.preserve_parenthesized_target, can_be_unwrapped_if_unused: el.can_be_unwrapped_if_unused, was_jsx_element: el.was_jsx_element, }); @@ -2020,6 +2022,7 @@ impl Data { } Data::EIndex(el) => { let item = bump.alloc(E::Index { + open_bracket_loc: el.open_bracket_loc, target: el.target.deep_clone_no_detach(bump)?, index: el.index.deep_clone_no_detach(bump)?, optional_chain: el.optional_chain, @@ -2080,6 +2083,7 @@ impl Data { } Data::ETemplate(el) => { let item = bump.alloc(E::Template { + head_loc: el.head_loc, tag: match &el.tag { Some(tag) => Some(tag.deep_clone_no_detach(bump)?), None => None, diff --git a/src/ast/fold_string_addition.rs b/src/ast/fold_string_addition.rs index e096e0a3f02a..85fb1615bca9 100644 --- a/src/ast/fold_string_addition.rs +++ b/src/ast/fold_string_addition.rs @@ -93,6 +93,7 @@ pub fn fold_string_addition( if right.head.is_utf8() { return Some(Expr::init( E::Template { + head_loc: crate::Loc::EMPTY, tag: None, parts: right.parts, head: e::TemplateContents::Cooked(join_strings( diff --git a/src/ast/known_global.rs b/src/ast/known_global.rs index 15c439d79221..a69b7697593e 100644 --- a/src/ast/known_global.rs +++ b/src/ast/known_global.rs @@ -103,7 +103,7 @@ impl KnownGlobal { let constructor = lookup(original_name)?; match constructor { - // Error constructors can be called without 'new' with identical behavior + // Removing `new` changes stack positions and makes returned errors eligible for tail calls. KnownGlobal::Error | KnownGlobal::TypeError | KnownGlobal::SyntaxError @@ -111,10 +111,7 @@ impl KnownGlobal { | KnownGlobal::ReferenceError | KnownGlobal::EvalError | KnownGlobal::URIError - | KnownGlobal::AggregateError => { - // Convert `new Error(...)` to `Error(...)` to save bytes - Some(Self::call_from_new(e, loc)) - } + | KnownGlobal::AggregateError => None, KnownGlobal::Object => { let n = e.args.len_u32(); diff --git a/src/bundler/linker_context/generateCodeForLazyExport.rs b/src/bundler/linker_context/generateCodeForLazyExport.rs index 44c9a36142c8..ff5d0edf6df6 100644 --- a/src/bundler/linker_context/generateCodeForLazyExport.rs +++ b/src/bundler/linker_context/generateCodeForLazyExport.rs @@ -344,6 +344,7 @@ pub(crate) fn generate_code_for_lazy_export( bun_ast::StoreSlice::new_mut(arena.alloc_slice_fill_iter(template_parts)); value = Expr::init( E::Template { + head_loc: bun_ast::Loc::EMPTY, tag: None, parts: parts_slice, head: E::TemplateContents::Cooked(E::String::init(b"")), diff --git a/src/js_parser/lower/lower_decorators.rs b/src/js_parser/lower/lower_decorators.rs index c285fbfb3a8e..dff0909ce300 100644 --- a/src/js_parser/lower/lower_decorators.rs +++ b/src/js_parser/lower/lower_decorators.rs @@ -1142,6 +1142,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let this = p.new_expr(E::This {}, loc); let member = p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: this, index: key, optional_chain: None, @@ -1438,6 +1439,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O ); self.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: object, index: key, optional_chain: None, diff --git a/src/js_parser/p.rs b/src/js_parser/p.rs index 7da4223ff592..ae67d9a15023 100644 --- a/src/js_parser/p.rs +++ b/src/js_parser/p.rs @@ -178,6 +178,8 @@ pub(crate) struct ParserSnapshot<'a> { should_fold_typescript_constant_expressions: bool, fn_or_arrow_data_parse: FnOrArrowDataParse, latest_arrow_arg_loc: bun_ast::Loc, + parenthesized_target_loc: bun_ast::Loc, + parenthesized_suffix_loc: bun_ast::Loc, forbid_suffix_after_as_loc: bun_ast::Loc, after_arrow_body_loc: bun_ast::Loc, esm_import_keyword: bun_ast::Range, @@ -271,6 +273,8 @@ pub struct P<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> { // allocated_names: ListManaged(string) = ListManaged(string).init(bun.default_allocator), // allocated_names_pool: ?*AllocatedNamesPool.Node = null, pub(crate) latest_arrow_arg_loc: bun_ast::Loc, + pub(crate) parenthesized_target_loc: bun_ast::Loc, + pub(crate) parenthesized_suffix_loc: bun_ast::Loc, pub(crate) forbid_suffix_after_as_loc: bun_ast::Loc, pub(crate) current_scope: js_ast::StoreRef, pub(crate) scopes_for_current_part: List<'a, *mut js_ast::Scope>, @@ -5486,7 +5490,16 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O }); } + pub(crate) fn preserve_parenthesized_call_target(&self, expr: Expr) -> bool { + self.options.features.dont_bundle_twice + && self.parenthesized_target_loc.eql(expr.loc) + && self.parenthesized_suffix_loc.eql(self.lexer.loc()) + } + pub(crate) fn mark_expr_as_parenthesized(&mut self, expr: &mut Expr) { + // Runtime loads retain the callee parentheses that select JSC's call position. + self.parenthesized_target_loc = expr.loc; + self.parenthesized_suffix_loc = self.lexer.loc(); match &mut expr.data { js_ast::ExprData::EArray(ex) => ex.is_parenthesized = true, js_ast::ExprData::EObject(ex) => ex.is_parenthesized = true, @@ -7564,6 +7577,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O } _ => self.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target, index: key, optional_chain: None, @@ -8351,6 +8365,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O .should_fold_typescript_constant_expressions, fn_or_arrow_data_parse: self.fn_or_arrow_data_parse.clone(), latest_arrow_arg_loc: self.latest_arrow_arg_loc, + parenthesized_target_loc: self.parenthesized_target_loc, + parenthesized_suffix_loc: self.parenthesized_suffix_loc, forbid_suffix_after_as_loc: self.forbid_suffix_after_as_loc, after_arrow_body_loc: self.after_arrow_body_loc, esm_import_keyword: self.esm_import_keyword, @@ -8385,6 +8401,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O snapshot.should_fold_typescript_constant_expressions; self.fn_or_arrow_data_parse = snapshot.fn_or_arrow_data_parse; self.latest_arrow_arg_loc = snapshot.latest_arrow_arg_loc; + self.parenthesized_target_loc = snapshot.parenthesized_target_loc; + self.parenthesized_suffix_loc = snapshot.parenthesized_suffix_loc; self.forbid_suffix_after_as_loc = snapshot.forbid_suffix_after_as_loc; self.after_arrow_body_loc = snapshot.after_arrow_body_loc; self.esm_import_keyword = snapshot.esm_import_keyword; @@ -9841,6 +9859,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O fn_only_data_visit: FnOnlyDataVisit::default(), allocated_names: BumpVec::new_in(arena), latest_arrow_arg_loc: bun_ast::Loc::EMPTY, + parenthesized_target_loc: bun_ast::Loc::EMPTY, + parenthesized_suffix_loc: bun_ast::Loc::EMPTY, forbid_suffix_after_as_loc: bun_ast::Loc::EMPTY, scopes_for_current_part: BumpVec::new_in(arena), symbols: BumpVec::with_capacity_in(estimated_symbol_count, arena), diff --git a/src/js_parser/parse/parse_prefix.rs b/src/js_parser/parse/parse_prefix.rs index 5c4d54f737c4..2ff66c10406f 100644 --- a/src/js_parser/parse/parse_prefix.rs +++ b/src/js_parser/parse/parse_prefix.rs @@ -54,8 +54,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O p.allow_in = true; let mut value = p.parse_expr(Level::Lowest)?; - p.mark_expr_as_parenthesized(&mut value); p.lexer.expect(T::TCloseParen)?; + p.mark_expr_as_parenthesized(&mut value); p.allow_in = old_allow_in; return Ok(value); @@ -295,6 +295,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O Ok(p.new_expr( E::Template { + head_loc: loc, tag: None, head: E::TemplateContents::Cooked(head), parts, diff --git a/src/js_parser/parse/parse_suffix.rs b/src/js_parser/parse/parse_suffix.rs index d6b6bd1571ac..856d7d135b3b 100644 --- a/src/js_parser/parse/parse_suffix.rs +++ b/src/js_parser/parse/parse_suffix.rs @@ -83,6 +83,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let index = p.new_expr(E::PrivateIdentifier { ref_ }, name_loc); *left = p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target, index, optional_chain: old_optional_chain, @@ -140,6 +141,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O match p.lexer.token { T::TOpenBracket => { // "a?.[b]" + let open_bracket_loc = p.lexer.loc(); p.lexer.next()?; // allow "in" inside the brackets; @@ -155,6 +157,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let target = *left; *left = p.new_expr( E::Index { + open_bracket_loc, target, index, optional_chain: optional_start, @@ -170,6 +173,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O return Ok(Continuation::Done); } + let open_paren_loc = p.lexer.loc(); + let preserve_parenthesized_target = p.preserve_parenthesized_call_target(*left); let list_loc = p.parse_call_args()?; let loc = left.loc; let target = *left; @@ -177,7 +182,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O E::Call { target, args: list_loc.list, + open_paren_loc, close_paren_loc: list_loc.loc, + preserve_parenthesized_target, optional_chain: optional_start, ..Default::default() }, @@ -200,6 +207,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O return Ok(Continuation::Done); } + let open_paren_loc = p.lexer.loc(); + let preserve_parenthesized_target = p.preserve_parenthesized_call_target(*left); let list_loc = p.parse_call_args()?; let loc = left.loc; let target = *left; @@ -207,7 +216,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O E::Call { target, args: list_loc.list, + open_paren_loc, close_paren_loc: list_loc.loc, + preserve_parenthesized_target, optional_chain: optional_start, ..Default::default() }, @@ -226,6 +237,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let index = p.new_expr(E::PrivateIdentifier { ref_ }, name_loc); *left = p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target, index, optional_chain: optional_start, @@ -281,6 +293,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O ); } // p.markSyntaxFeature(compat.TemplateLiteral, p.lexer.Range()); + let head_loc = p.lexer.loc(); let head = E::Str::new(p.lexer.raw_template_contents()); p.lexer.next()?; @@ -288,6 +301,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let tag = *left; *left = p.new_expr( E::Template { + head_loc, tag: Some(tag), head: E::TemplateContents::Raw(head), parts: E::Template::empty_parts(), @@ -312,12 +326,14 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O ); } // p.markSyntaxFeature(compat.TemplateLiteral, p.lexer.Range()); + let head_loc = p.lexer.loc(); let head = E::Str::new(p.lexer.raw_template_contents()); let (parts, _tail_loc) = p.parse_template_parts(true)?; let tag = *left; let loc = left.loc; *left = p.new_expr( E::Template { + head_loc, tag: Some(tag), head: E::TemplateContents::Raw(head), parts, @@ -346,6 +362,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O return Ok(Continuation::Done); } + let open_bracket_loc = p.lexer.loc(); p.lexer.next()?; // Allow "in" inside the brackets @@ -362,6 +379,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let target = *left; *left = p.new_expr( E::Index { + open_bracket_loc, target, index, optional_chain: old_optional_chain, @@ -384,6 +402,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O return Ok(Continuation::Done); } + let open_paren_loc = p.lexer.loc(); + let preserve_parenthesized_target = p.preserve_parenthesized_call_target(*left); let list_loc = p.parse_call_args()?; let loc = left.loc; let target = *left; @@ -391,7 +411,9 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O E::Call { target, args: list_loc.list, + open_paren_loc, close_paren_loc: list_loc.loc, + preserve_parenthesized_target, optional_chain: old_optional_chain, ..Default::default() }, @@ -486,6 +508,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O p: &mut Self, optional_chain: &mut Option, old_optional_chain: Option, + left: &Expr, ) -> CResult { // Skip over TypeScript non-null assertions if p.lexer.has_newline_before { @@ -497,7 +520,11 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O return Err(crate::Error::SyntaxError); } + let parenthesized = p.preserve_parenthesized_call_target(*left); p.lexer.next()?; + if parenthesized { + p.parenthesized_suffix_loc = p.lexer.loc(); + } *optional_chain = old_optional_chain; Ok(Continuation::Next) @@ -878,8 +905,12 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O // TypeScript allows type arguments to be specified with angle brackets // inside an expression. Unlike in other languages, this unfortunately // appears to require backtracking to parse. + let parenthesized = p.preserve_parenthesized_call_target(*left); if Self::IS_TYPESCRIPT_ENABLED && p.try_skip_type_script_type_arguments_with_backtracking() { + if parenthesized { + p.parenthesized_suffix_loc = p.lexer.loc(); + } *optional_chain = old_optional_chain; return Ok(Continuation::Next); } @@ -969,8 +1000,12 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O // TypeScript allows type arguments to be specified with angle brackets // inside an expression. Unlike in other languages, this unfortunately // appears to require backtracking to parse. + let parenthesized = p.preserve_parenthesized_call_target(*left); if Self::IS_TYPESCRIPT_ENABLED && p.try_skip_type_script_type_arguments_with_backtracking() { + if parenthesized { + p.parenthesized_suffix_loc = p.lexer.loc(); + } *optional_chain = old_optional_chain; return Ok(Continuation::Next); } @@ -1555,7 +1590,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O T::TSlash => Self::sfx_t_slash(p, level, left), T::TSlashEquals => Self::sfx_t_slash_equals(p, level, left), T::TExclamation => { - Self::sfx_t_exclamation(p, &mut optional_chain, old_optional_chain) + Self::sfx_t_exclamation(p, &mut optional_chain, old_optional_chain, left) } T::TBarBar => Self::sfx_t_bar_bar(p, level, left, flags), T::TAmpersandAmpersand => Self::sfx_t_ampersand_ampersand(p, level, left, flags), diff --git a/src/js_parser/parse/parse_typescript.rs b/src/js_parser/parse/parse_typescript.rs index eb4d49f92c69..7acac3eddc10 100644 --- a/src/js_parser/parse/parse_typescript.rs +++ b/src/js_parser/parse/parse_typescript.rs @@ -136,6 +136,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let index = p.new_expr(E::PrivateIdentifier { ref_ }, name_loc); expr = p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: expr, index, optional_chain: None, diff --git a/src/js_parser/visit/mod.rs b/src/js_parser/visit/mod.rs index e12fd1455c88..ae13f46cdf41 100644 --- a/src/js_parser/visit/mod.rs +++ b/src/js_parser/visit/mod.rs @@ -1350,6 +1350,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O } _ => self.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: this_target, index: key, optional_chain: None, diff --git a/src/js_parser/visit/visit_expr.rs b/src/js_parser/visit/visit_expr.rs index 23c08903f202..c5e0d61ab622 100644 --- a/src/js_parser/visit/visit_expr.rs +++ b/src/js_parser/visit/visit_expr.rs @@ -855,8 +855,8 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O let is_template_tag = matches!(p.template_tag, Data::EIndex(tag) if core::ptr::eq(&raw const *e_, &raw const *tag)); let is_delete_target = matches!(p.delete_target, Data::EIndex(dt) if core::ptr::eq(&raw const *e_, &raw const *dt)); - // "a['b']" => "a.b" - if p.options.features.minify_syntax { + // Runtime stack positions distinguish computed access from dot access. + if p.options.features.minify_syntax && !p.options.features.dont_bundle_twice { if let Some(mut s) = e_.index.data.e_string() { if !s.is_utf16 && s.is_identifier(p.arena) { let dot = p.new_expr( @@ -985,7 +985,10 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O // "a['b' + '']" => "a.b" // "enum A { B = 'b' }; a[A.B]" => "a.b" - if p.options.features.minify_syntax && s.is_identifier(p.arena) { + if p.options.features.minify_syntax + && !p.options.features.dont_bundle_twice + && s.is_identifier(p.arena) + { let dot = p.new_expr( E::Dot { name: s.data, diff --git a/src/js_parser/visit/visit_stmt.rs b/src/js_parser/visit/visit_stmt.rs index f399550709e3..33a4cca180c3 100644 --- a/src/js_parser/visit/visit_stmt.rs +++ b/src/js_parser/visit/visit_stmt.rs @@ -2332,6 +2332,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O Expr::assign( p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: Expr::init_identifier(data.arg, value.loc), index: name_as_e_string.unwrap(), optional_chain: None, @@ -2353,6 +2354,7 @@ impl<'a, const TYPESCRIPT: bool, const SCAN_ONLY: bool> P<'a, TYPESCRIPT, SCAN_O value_exprs.push(Expr::assign( p.new_expr( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: Expr::init_identifier(data.arg, value.loc), index: assign_target, optional_chain: None, diff --git a/src/js_printer/lib.rs b/src/js_printer/lib.rs index 4040862321d0..764a2900c7b6 100644 --- a/src/js_printer/lib.rs +++ b/src/js_printer/lib.rs @@ -3502,6 +3502,9 @@ pub(crate) mod __gated_printer { && self.is_unbound_eval_identifier(e.target) && e.optional_chain.is_none(); + if e.preserve_parenthesized_target { + self.print(b"("); + } if is_unbound_eval { self.print(b"(0,"); self.print_space(); @@ -3513,9 +3516,15 @@ pub(crate) mod __gated_printer { self.print_expr(e.target, Level::Postfix, target_flags); } + if e.preserve_parenthesized_target { + self.print(b")"); + } if e.optional_chain == Some(js_ast::OptionalChain::Start) { self.print(b"?."); } + if !e.open_paren_loc.is_empty() { + self.add_source_mapping(e.open_paren_loc); + } self.print(b"("); let args = e.args.slice(); @@ -3765,6 +3774,9 @@ pub(crate) mod __gated_printer { self.print_symbol(priv_.ref_); } _ => { + if !e.open_bracket_loc.is_empty() { + self.add_source_mapping(e.open_bracket_loc); + } self.print(b"["); self.add_source_mapping(e.index.loc); self.print_expr(e.index, Level::Lowest, ExprFlag::none()); @@ -4063,6 +4075,7 @@ pub(crate) mod __gated_printer { // would be a cross-thread data race. Re-prints recompute // the identical fold, so emitted output is unchanged. let mut e = E::Template { + head_loc: e.head_loc, tag: e.tag, parts: e.parts, head: match &e.head { @@ -4128,6 +4141,7 @@ pub(crate) mod __gated_printer { // parts slice stays valid past this block. let parts_slice = js_ast::StoreSlice::from_bump(replaced); let mut copy = E::Template { + head_loc: e.head_loc, tag: e.tag, parts: parts_slice, head: match &e.head { @@ -4157,6 +4171,7 @@ pub(crate) mod __gated_printer { // fields out is safe even though `t` itself may // point at `copy`. e = E::Template { + head_loc: t.head_loc, tag: t.tag, parts: t.parts, head: match &t.head { @@ -4204,6 +4219,9 @@ pub(crate) mod __gated_printer { self.add_source_mapping(expr.loc); } + if !e.head_loc.is_empty() { + self.add_source_mapping(e.head_loc); + } self.print(b"`"); match &e.head { E::TemplateContents::Raw(raw) => self.print_raw_template_literal(raw), diff --git a/src/jsc/RuntimeTranspilerCache.rs b/src/jsc/RuntimeTranspilerCache.rs index a9bb155f298d..2bdfb0bf7cf2 100644 --- a/src/jsc/RuntimeTranspilerCache.rs +++ b/src/jsc/RuntimeTranspilerCache.rs @@ -66,7 +66,9 @@ bun_core::declare_scope!(cache, visible); /// Version 34: Discard output containing a previous runtime plugin's resolved import paths. /// Version 35: Preserve inferred names and inline type-only runtime evaluation edges. /// Version 36: Runtime plugin imports retain their original specifiers and resolve when executed. -const EXPECTED_VERSION: u32 = 36; +/// Version 37: Preserve builtin error constructors and their stack positions and frames. +/// Version 38: Retain runtime call syntax and map opening call/property delimiters. +const EXPECTED_VERSION: u32 = 38; /// Source files smaller than this are not written to / read from the on-disk /// transpiler cache. Originally 50 KiB, which excluded almost every file in a diff --git a/src/jsc/bindings/ErrorStackFrame.cpp b/src/jsc/bindings/ErrorStackFrame.cpp index 14ec9fd8d9b8..9f8c36167db8 100644 --- a/src/jsc/bindings/ErrorStackFrame.cpp +++ b/src/jsc/bindings/ErrorStackFrame.cpp @@ -22,12 +22,13 @@ static bool isConstruct(JSC::CodeBlock* code, JSC::BytecodeIndex bc) } } -ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc) +ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync) { auto expr = code->expressionInfoForBytecodeIndex(bc); - auto offset = expr.divot; - // Constructors point to `new`; other calls retain JSC's syntax location. - if (isConstruct(code, bc)) + auto callSitePosition = code->callSitePositionForBytecodeIndex(bc, isAsync); + auto offset = callSitePosition.value_or(expr.divot); + // Older or implicit bytecode can lack a syntax-selected stack position. + if (!callSitePosition && isConstruct(code, bc)) offset -= std::min(offset, expr.startOffset); auto lineColumn = code->source().provider()->documentLineColumnForOffset(offset); @@ -38,16 +39,18 @@ ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC:: }; } -ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc) +ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync) { if (isConstruct(code, bc)) { - auto position = getAdjustedPositionForBytecode(code, bc); + auto position = getAdjustedPositionForBytecode(code, bc, isAsync); position.byte_position = -1; return position; } - // Keep JSC's cached lookup for frames that do not need an expression-range adjustment. - auto lineColumn = code->lineColumnForBytecodeIndex(bc); + auto callSitePosition = code->callSitePositionForBytecodeIndex(bc, isAsync); + auto lineColumn = callSitePosition + ? code->source().provider()->documentLineColumnForOffset(*callSitePosition) + : code->lineColumnForBytecodeIndex(bc); return { .line_zero_based = OrdinalNumber::fromOneBasedInt(lineColumn.line).zeroBasedInt(), .column_zero_based = OrdinalNumber::fromOneBasedInt(lineColumn.column).zeroBasedInt(), diff --git a/src/jsc/bindings/ErrorStackFrame.h b/src/jsc/bindings/ErrorStackFrame.h index a1c3167877ac..70f1b2faf02c 100644 --- a/src/jsc/bindings/ErrorStackFrame.h +++ b/src/jsc/bindings/ErrorStackFrame.h @@ -4,8 +4,8 @@ namespace Bun { -ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc); +ZigStackFramePosition getAdjustedPositionForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync); -ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc); +ZigStackFramePosition getAdjustedLineColumnForBytecode(JSC::CodeBlock* code, JSC::BytecodeIndex bc, bool isAsync); } // namespace Bun diff --git a/src/jsc/bindings/ErrorStackTrace.cpp b/src/jsc/bindings/ErrorStackTrace.cpp index 3550bb52ba76..866d011cb404 100644 --- a/src/jsc/bindings/ErrorStackTrace.cpp +++ b/src/jsc/bindings/ErrorStackTrace.cpp @@ -341,7 +341,7 @@ bool JSCStackFrame::calculateSourcePositions() return false; } - auto location = Bun::getAdjustedLineColumnForBytecode(m_codeBlock, m_bytecodeIndex); + auto location = Bun::getAdjustedLineColumnForBytecode(m_codeBlock, m_bytecodeIndex, isAsync()); m_sourcePositions.line = location.line(); m_sourcePositions.column = location.column(); diff --git a/src/jsc/bindings/FormatStackTraceForJS.cpp b/src/jsc/bindings/FormatStackTraceForJS.cpp index 60aca2e78221..403bf1316a80 100644 --- a/src/jsc/bindings/FormatStackTraceForJS.cpp +++ b/src/jsc/bindings/FormatStackTraceForJS.cpp @@ -321,7 +321,7 @@ WTF::String formatStackTrace( if (!frame.hasLineAndColumnInfo()) continue; if (frame.codeBlock() && frame.hasBytecodeIndex()) { - auto position = Bun::getAdjustedLineColumnForBytecode(frame.codeBlock(), frame.bytecodeIndex()); + auto position = Bun::getAdjustedLineColumnForBytecode(frame.codeBlock(), frame.bytecodeIndex(), frame.isAsyncFrame()); originalLineColumns[i] = { static_cast(position.line().oneBasedInt()), static_cast(position.column().oneBasedInt()) }; if (auto overrideLine = frame.codeBlock()->ownerExecutable()->overrideLineNumber(vm)) originalLineColumns[i].line = *overrideLine; diff --git a/src/jsc/bindings/ZigException.cpp b/src/jsc/bindings/ZigException.cpp index cfd1059c2e78..b7442cb3aab2 100644 --- a/src/jsc/bindings/ZigException.cpp +++ b/src/jsc/bindings/ZigException.cpp @@ -143,7 +143,7 @@ static void populateStackFramePosition(const JSC::StackFrame& stackFrame, BunStr return; } - auto location = Bun::getAdjustedPositionForBytecode(code, stackFrame.bytecodeIndex()); + auto location = Bun::getAdjustedPositionForBytecode(code, stackFrame.bytecodeIndex(), stackFrame.isAsyncFrame()); memcpy(&position, &location, sizeof(ZigStackFramePosition)); if (flags == PopulateStackTraceFlags::OnlyPosition) return; diff --git a/src/react_compiler/codegen.rs b/src/react_compiler/codegen.rs index 473b14c9d2ed..5b3f1704b34b 100644 --- a/src/react_compiler/codegen.rs +++ b/src/react_compiler/codegen.rs @@ -669,6 +669,7 @@ fn codegen_reactive_scope( let cache_slot = |index: u32| { Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: cache_ident(), index: Expr::init(E::Number::new(index as f64), loc), optional_chain: None, @@ -2005,6 +2006,7 @@ fn codegen_base_instruction_value( let prop = codegen_place_to_expression(cx, property)?; Ok(Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: obj, index: prop, optional_chain: None, @@ -2027,6 +2029,7 @@ fn codegen_base_instruction_value( op: OpCode::BinAssign, left: Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: obj, index: prop, optional_chain: None, @@ -2049,6 +2052,7 @@ fn codegen_base_instruction_value( op: OpCode::UnDelete, value: Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: obj, index: prop, optional_chain: None, @@ -2179,6 +2183,7 @@ fn codegen_base_instruction_value( let tag_expr = codegen_place_to_expression(cx, tag)?; Ok(Expr::init( E::Template { + head_loc: bun_ast::Loc::EMPTY, tag: Some(tag_expr), head: E::TemplateContents::Raw(value.raw), parts: StoreSlice::EMPTY, @@ -2204,6 +2209,7 @@ fn codegen_base_instruction_value( } Ok(Expr::init( E::Template { + head_loc: bun_ast::Loc::EMPTY, tag: None, head, parts: StoreSlice::new_mut(parts.leak()), @@ -2369,6 +2375,7 @@ fn codegen_function_expression( }); let wrapped = Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target: Expr::init( E::Object { properties: props, @@ -3311,6 +3318,7 @@ fn property_access_expr( ), PropertyLiteral::Number(n) => Expr::init( E::Index { + open_bracket_loc: bun_ast::Loc::EMPTY, target, index: Expr::init(E::Number::new(n.value()), loc), optional_chain, diff --git a/test/bundler/bundler_minify.test.ts b/test/bundler/bundler_minify.test.ts index 052aa49253ba..0d629beee86e 100644 --- a/test/bundler/bundler_minify.test.ts +++ b/test/bundler/bundler_minify.test.ts @@ -1229,7 +1229,7 @@ describe("bundler", () => { }, }); - itBundled("minify/ErrorConstructorOptimization", { + itBundled("minify/ErrorConstructorPositions", { files: { "/entry.js": /* js */ ` // Test all Error constructors @@ -1272,25 +1272,25 @@ describe("bundler", () => { `, }, capture: [ - "Error()", - 'Error("message")', - 'Error("message", { cause: "cause" })', - "TypeError()", - 'TypeError("type error")', - "SyntaxError()", - 'SyntaxError("syntax error")', - "RangeError()", - 'RangeError("range error")', - "ReferenceError()", - 'ReferenceError("ref error")', - "EvalError()", - 'EvalError("eval error")', - "URIError()", - 'URIError("uri error")', - 'AggregateError([], "aggregate error")', - 'AggregateError([Error("e1")], "multiple")', - "Error(msg)", - "TypeError(getErrorMessage())", + "new Error", + 'new Error("message")', + 'new Error("message", { cause: "cause" })', + "new TypeError", + 'new TypeError("type error")', + "new SyntaxError", + 'new SyntaxError("syntax error")', + "new RangeError", + 'new RangeError("range error")', + "new ReferenceError", + 'new ReferenceError("ref error")', + "new EvalError", + 'new EvalError("eval error")', + "new URIError", + 'new URIError("uri error")', + 'new AggregateError([], "aggregate error")', + 'new AggregateError([new Error("e1")], "multiple")', + "new Error(msg)", + "new TypeError(getErrorMessage())", "/* @__PURE__ */ new Date", "/* @__PURE__ */ new Map", "/* @__PURE__ */ new Set", diff --git a/test/js/bun/jsc/webkit-upgrade-7b485a76e9.test.ts b/test/js/bun/jsc/webkit-upgrade-7b485a76e9.test.ts index 27805cb4fda9..42f6196bf986 100644 --- a/test/js/bun/jsc/webkit-upgrade-7b485a76e9.test.ts +++ b/test/js/bun/jsc/webkit-upgrade-7b485a76e9.test.ts @@ -150,8 +150,8 @@ describe("WebKit 7b485a76e9 upgrade", () => { const frames = [...stack.matchAll(/positions\.js:(\d+):(\d+)/g)].map(m => [Number(m[1]), Number(m[2])]); expect(frames).toEqual([ [6, 28], - [4, 15], - [7, 6], + [4, 10], + [7, 1], ]); // lineOffset and columnOffset move the first line. columnOffset does not move later lines. diff --git a/test/js/bun/sourcemap/internal-sourcemap-roundtrip.test.ts b/test/js/bun/sourcemap/internal-sourcemap-roundtrip.test.ts index ba26e5112af1..e5d1ebd8850c 100644 --- a/test/js/bun/sourcemap/internal-sourcemap-roundtrip.test.ts +++ b/test/js/bun/sourcemap/internal-sourcemap-roundtrip.test.ts @@ -486,7 +486,7 @@ describe.concurrent("sourcemap of a source with a truncated trailing UTF-8 seque const map = await Bun.file(path.join(String(dir), "out", "in.js.map")).json(); expect(map).toMatchObject({ sources: ["../in.js"], - mappings: ";AAAA,QAAQ,IAAI,CAAC;", + mappings: ";AAAA,QAAQ,GAAG,CAAC,CAAC;", }); }); }); diff --git a/test/js/bun/sourcemap/internal-sourcemap.test.ts b/test/js/bun/sourcemap/internal-sourcemap.test.ts index b8efdae7770b..63c78056e358 100644 --- a/test/js/bun/sourcemap/internal-sourcemap.test.ts +++ b/test/js/bun/sourcemap/internal-sourcemap.test.ts @@ -76,9 +76,9 @@ describe("InternalSourceMap", () => { const positions = extractPositions(stdout); expect(positions).toEqual([ - "6:13", // throw new Error("boom") + "6:9", // throw new Error("boom") "24:3", // gamma() at top level - "31:17", // new Error("here").stack + "31:13", // new Error("here").stack "34:13", // captureViaCaptureStackTrace() ]); @@ -103,10 +103,12 @@ describe("InternalSourceMap", () => { const { stdout, stderr, exited } = await run({ "index.ts": lines.join("\n") + "\n" }); expect(stderr).toBe(""); - // Each frame must point at its line's `Error` in 1-based UTF-16 columns, + // Each frame must point at its line's `new` keyword in 1-based UTF-16 columns, // `a1` being the all-ASCII control. const frames = [...stdout.matchAll(/at ([a-d]1) \(.*index\.ts:(\d+):(\d+)\)/g)].map(m => `${m[1]} ${m[2]}:${m[3]}`); - expect(frames).toEqual(["a1", "b1", "c1", "d1"].map((fn, i) => `${fn} ${i + 1}:${lines[i].indexOf("Error(") + 1}`)); + expect(frames).toEqual( + ["a1", "b1", "c1", "d1"].map((fn, i) => `${fn} ${i + 1}:${lines[i].indexOf("new Error(") + 1}`), + ); expect(exited).toBe(0); }); diff --git a/test/js/bun/util/inspect.test.js b/test/js/bun/util/inspect.test.js index 3549700bb920..3b2ce770940c 100644 --- a/test/js/bun/util/inspect.test.js +++ b/test/js/bun/util/inspect.test.js @@ -892,7 +892,7 @@ it("ErrorEvent", () => { NNN | lineno: 42, NNN | colno: 10, NNN | error: new Error("Test error"), - ^ + ^ error: Test error at (file:NN:NN) , diff --git a/test/js/node/module/node-module-module.test.js b/test/js/node/module/node-module-module.test.js index a5bc03c0f5fa..2bee38b3360a 100644 --- a/test/js/node/module/node-module-module.test.js +++ b/test/js/node/module/node-module-module.test.js @@ -1781,7 +1781,7 @@ console.log("survived", require("./late.js"));`, expect(stderr).toMatchInlineSnapshot(` "1 | require("module").runMain = () => { 2 | throw new RangeError("from the override"); - ^ + ^ RangeError: from the override at (file:NN:NN) diff --git a/test/js/node/v8/callsite-async-position-fixture.mjs b/test/js/node/v8/callsite-async-position-fixture.mjs new file mode 100644 index 000000000000..84cfc6db7910 --- /dev/null +++ b/test/js/node/v8/callsite-async-position-fixture.mjs @@ -0,0 +1,12 @@ +async function leaf() { +await Promise.resolve(); +return new Error().stack; +} +async function parent() { +return await leaf(); +} +const trace = await parent(); +const parentFrame = trace.split("\n").find(line => /\bparent(?:\s|@|\()/.test(line)); +const position = /:(\d+):(\d+)\)?$/.exec(parentFrame); +if (!position || +position[1] !== 6 || +position[2] !== 8) + throw new Error(JSON.stringify({ expected: [6, 8], trace })); diff --git a/test/js/node/v8/callsite-syntax-positions-fixture.js b/test/js/node/v8/callsite-syntax-positions-fixture.js new file mode 100644 index 000000000000..32eed3eed0db --- /dev/null +++ b/test/js/node/v8/callsite-syntax-positions-fixture.js @@ -0,0 +1,116 @@ +// Keep call positions independent of exception-expression divots. +let trace; +function capture() { const stack = new Error().stack; trace = stack; return stack; } +function Capture() { this.stack = new Error().stack; trace = this.stack; } +function factory() { return capture; } +const object = { capture }; +const name = "capture"; +const cases = [ + [function () { +capture() + }, 10, 1, "capture()"], + [function () { +capture /* comment */ () + }, 13, 1, "capture /* comment */ ()"], + [function () { +capture +() + }, 16, 1, "capture\n()"], + [function () { +factory()() + }, 20, 10, "factory()()"], + [function () { +factory() +() + }, 24, 1, "factory()\n()"], + [function () { +factory() /* comment */ () + }, 27, 25, "factory() /* comment */ ()"], + [function () { +object.capture() + }, 30, 8, "object.capture()"], + [function () { +object +.capture() + }, 34, 2, "object\n.capture()"], + [function () { +object . capture() + }, 37, 10, "object . capture()"], + [function () { +object. /* comment */ capture() + }, 40, 23, "object. /* comment */ capture()"], + [function () { +object.\u0063apture() + }, 43, 8, "object.\\u0063apture()"], + [function () { +object[name]() + }, 46, 13, "object[name]()"], + [function () { +object["capture"]() + }, 49, 18, "object[\"capture\"]()"], + [function () { +object[ +name +] +() + }, 55, 1, "object[\nname\n]\n()"], + [function () { +(capture)() + }, 58, 10, "(capture)()"], + [function () { +((capture))() + }, 61, 12, "((capture))()"], + [function () { +(object.capture)() + }, 64, 17, "(object.capture)()"], + [function () { +(object[name])() + }, 67, 15, "(object[name])()"], + [function () { +(0, capture)() + }, 70, 13, "(0, capture)()"], + [function () { +capture?.() + }, 73, 10, "capture?.()"], + [function () { +object?.capture() + }, 76, 9, "object?.capture()"], + [function () { +object.capture?.() + }, 79, 17, "object.capture?.()"], + [function () { +object?.capture?.() + }, 82, 18, "object?.capture?.()"], + [function () { +this() + }, 85, 5, "this()"], + [function () { +capture`text` + }, 88, 8, "capture`text`"], + [function () { +Reflect.apply(capture, null, []) + }, 91, 9, "Reflect.apply(capture, null, [])"], + [function () { +new Capture().stack + }, 94, 1, "new Capture().stack"], + [function () { +new +Capture().stack + }, 97, 1, "new\nCapture().stack"], + [function () { +new (Capture)().stack + }, 101, 1, "new (Capture)().stack"], + [function () { +Reflect.construct(Capture, []).stack + }, 104, 9, "Reflect.construct(Capture, []).stack"], +]; +for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { + for (const [fn, line, column, body] of cases) { + trace = ""; + fn.call(capture); + const frames = trace.split("\n").filter(line => /:(\d+):(\d+)\)?$/.test(line)); + const position = /:(\d+):(\d+)\)?$/.exec(frames[1]); + if (!position || +position[1] !== line || +position[2] !== column) + throw new Error(JSON.stringify({ body, expected: [line, column], trace })); + } +} diff --git a/test/js/node/v8/callsite-typescript-positions-fixture.ts b/test/js/node/v8/callsite-typescript-positions-fixture.ts new file mode 100644 index 000000000000..1bc876b8372e --- /dev/null +++ b/test/js/node/v8/callsite-typescript-positions-fixture.ts @@ -0,0 +1,63 @@ +import assert from "node:assert/strict"; +let frame: NodeJS.CallSite; +function capture(..._args: unknown[]) { + const target: { stack?: NodeJS.CallSite[] } = {}; + const previous = Error.prepareStackTrace; + try { + Error.prepareStackTrace = (_, frames) => frames; + Error.captureStackTrace(target, capture); + frame = target.stack![0]; + } finally { + Error.prepareStackTrace = previous; + } +} +const object = { capture }; +const cases = [ + [function () { +(object).capture(); + }, 17, 10, "receiver-parentheses"], + [function () { +(object.capture)(); + }, 20, 25, "callee-parentheses"], + [function () { +((capture))(); + }, 23, 20, "nested-parentheses"], + [function () { +(capture as typeof capture)(); + }, 26, 36, "asserted-callee"], + [function () { +object["capture"](); + }, 29, 26, "computed-callee"], + [function () { +object?.["capture"](); + }, 32, 28, "optional-computed-callee"], + [function () { +const marker = "🎉"; (capture)(); + }, 35, 39, "astral-prefix"], + [function () { +(capture); object.capture(); + }, 38, 19, "prior-parenthesized-expression"], + [function () { +(capture)<() => T>(); + }, 41, 22, "generic-function-type"], + [function () { +(capture)!(); + }, 44, 11, "non-null-callee"], + [function () { +(capture)!(); + }, 47, 19, "generic-non-null-callee"], + [function () { +(capture)!!(); + }, 50, 12, "repeated-non-null-callee"], + [function () { +(object)!.capture(); + }, 53, 11, "non-null-receiver"], + [function () { +capture`text`; + }, 56, 16, "tagged-template"], + ] as const; +for (const [fn, line, column, label] of cases) { + fn(); + assert.deepEqual([frame!.getLineNumber(), frame!.getColumnNumber()], [line, column], label); + assert.deepEqual(/:(\d+):(\d+)\)?$/.exec(frame!.toString())!.slice(1).map(Number), [line, column], label); +} diff --git a/test/js/node/v8/capture-stack-trace.test.js b/test/js/node/v8/capture-stack-trace.test.js index ac527424f0b2..fa239c56843c 100644 --- a/test/js/node/v8/capture-stack-trace.test.js +++ b/test/js/node/v8/capture-stack-trace.test.js @@ -206,8 +206,8 @@ test.concurrent("CallSite columns preserve async continuation metadata", async ( expect(stderr).toBe(""); expect(JSON.parse(stdout)).toEqual([ { position: [4, 7], textPosition: [4, 7], async: false, eval: false, native: false, file: true }, - // JSC selects the awaited call; V8 selects `await`. Preserve the location while fixing its units. - { position: [8, 14], textPosition: [8, 14], async: true, eval: false, native: false, file: true }, + // Async frames point at `await`, as in Node. + { position: [8, 8], textPosition: [8, 8], async: true, eval: false, native: false, file: true }, ]); expect(exitCode).toBe(0); }); @@ -2091,7 +2091,7 @@ test.concurrent( "define stack": error => (Object.defineProperty(error, "stack", { value: "defined" }), error.stack), // A clone is made from the complete string: it must not get a second first line. "structuredClone": error => structuredClone(error).stack.split("\\n").filter(line => line.includes("boom")), - "Bun.inspect": error => Bun.inspect(error).split("\\n").filter(line => line.includes("boom")).map(line => line.trim()), + "Bun.inspect": error => Bun.inspect(error).split("\\n").map(line => line.trim()).filter(line => line === "TypeError: boom"), "JSON.stringify": error => JSON.stringify(error), }; const rows = {}; @@ -2412,7 +2412,7 @@ test.concurrent.each([[{}], [{ BUN_JSC_useSourceProviderCache: "0" }]])( }); const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); expect(stderr).toBe(""); - expect(stdout.trim()).toEndWith("[eval]:5:14)"); // 2:18 when the lexer resumed on the template literal's first line + expect(stdout.trim()).toEndWith("[eval]:5:10)"); // Line 2 when the lexer resumed on the template literal's first line expect(exitCode).toBe(0); }, ); @@ -2456,3 +2456,31 @@ test.skipIf(totalmem() < 10 * 1024 ** 3)( }, 30_000, ); + +test("syntax positions preserve built-in constructors, arrow frames, and property reads", async () => { + await using proc = Bun.spawn({ + cmd: [bunExe(), import.meta.dir + "/stack-position-regressions-fixture.js"], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect({ stdout, stderr, exitCode }).toEqual({ stdout: "", stderr: "", exitCode: 0 }); +}); + +for (const fixture of [ + "callsite-syntax-positions-fixture.js", + "callsite-async-position-fixture.mjs", + "callsite-typescript-positions-fixture.ts", +]) { + test(`syntax-selected stack positions: ${fixture}`, async () => { + await using proc = Bun.spawn({ + cmd: [bunExe(), import.meta.dir + "/" + fixture], + env: bunEnv, + stdout: "pipe", + stderr: "pipe", + }); + const [stdout, stderr, exitCode] = await Promise.all([proc.stdout.text(), proc.stderr.text(), proc.exited]); + expect({ stdout, stderr, exitCode }).toEqual({ stdout: "", stderr: "", exitCode: 0 }); + }); +} diff --git a/test/js/node/v8/error-prepare-stack-default-fixture.js b/test/js/node/v8/error-prepare-stack-default-fixture.js index 17df9c6d9dc8..5e3ce31b89e5 100644 --- a/test/js/node/v8/error-prepare-stack-default-fixture.js +++ b/test/js/node/v8/error-prepare-stack-default-fixture.js @@ -30,8 +30,8 @@ function oneWrapperLevel() { oneWrapperLevel(); // The native line column numbers might differ a bit here. -const stackIgnoringLineAndColumn = stack.replaceAll(":12:26", ":NN:NN").replaceAll(/native:.*$/gm, "native)"); -const stack2IgnoringLineAndColumn = stack2.replaceAll(":17:26", ":NN:NN").replaceAll(/native:.*$/gm, "native)"); +const stackIgnoringLineAndColumn = stack.replaceAll(":12:9", ":NN:NN").replaceAll(/native:.*$/gm, "native)"); +const stack2IgnoringLineAndColumn = stack2.replaceAll(":17:9", ":NN:NN").replaceAll(/native:.*$/gm, "native)"); if (stackIgnoringLineAndColumn !== stack2IgnoringLineAndColumn) { console.log("\n-----\n"); console.log(stackIgnoringLineAndColumn); diff --git a/test/js/node/v8/stack-position-regressions-fixture.js b/test/js/node/v8/stack-position-regressions-fixture.js new file mode 100644 index 000000000000..fe71747d9f95 --- /dev/null +++ b/test/js/node/v8/stack-position-regressions-fixture.js @@ -0,0 +1,104 @@ +"use strict"; +const name = "x"; +const cases = [ + ['Error', function () { +const error = new Error("test"); return error; + }, 5, 15], + ['TypeError', function () { +const error = new TypeError("test"); return error; + }, 8, 15], + ['SyntaxError', function () { +const error = new SyntaxError("test"); return error; + }, 11, 15], + ['RangeError', function () { +const error = new RangeError("test"); return error; + }, 14, 15], + ['ReferenceError', function () { +const error = new ReferenceError("test"); return error; + }, 17, 15], + ['EvalError', function () { +const error = new EvalError("test"); return error; + }, 20, 15], + ['URIError', function () { +const error = new URIError("test"); return error; + }, 23, 15], + ['AggregateError', function () { +const error = new AggregateError([], "test"); return error; + }, 26, 15], + ['arrow-new', () => new Error("arrow"), 28, 25], + ['arrow-type', () => new TypeError("arrow"), 29, 26], + ['arrow-alias', () => new globalThis.Error("arrow"), 30, 27], + ['multiline-new', function () { +const error = new + Error("multiline"); return error; + }, 32, 15], + ['constructor-comment', function () { +const error = new /* comment */ Error("comment"); return error; + }, 36, 15], + ['null-dot', function () { +try { null.x; } catch (error) { return error; } + }, 39, 12], + ['undefined-dot', function () { +try { undefined.x; } catch (error) { return error; } + }, 42, 17], + ['null-length', function () { +try { null.length; } catch (error) { return error; } + }, 45, 12], + ['null-comment', function () { +try { null . /* comment */ x; } catch (error) { return error; } + }, 48, 28], + ['null-escape', function () { +try { null.\u0078; } catch (error) { return error; } + }, 51, 12], + ['null-multiline', function () { +try { null + .x; } catch (error) { return error; } + }, 55, 6], + ['null-bracket', function () { +try { null["x"]; } catch (error) { return error; } + }, 58, 11], + ['null-number', function () { +try { null[1]; } catch (error) { return error; } + }, 61, 11], + ['null-computed', function () { +try { null[name]; } catch (error) { return error; } + }, 64, 11], + ['null-call', function () { +try { null.x(); } catch (error) { return error; } + }, 67, 12], + ['null-parenthesized-call', function () { +try { (null.x)(); } catch (error) { return error; } + }, 70, 13], + ['null-bracket-call', function () { +try { null["x"](); } catch (error) { return error; } + }, 73, 11], + ['null-call-method', function () { +try { null.call(); } catch (error) { return error; } + }, 76, 12], + ['null-apply-method', function () { +try { null.apply(null, []); } catch (error) { return error; } + }, 79, 12], + ['null-has-own', function () { +try { null.hasOwnProperty(name); } catch (error) { return error; } + }, 82, 12], + ]; +function checkPosition(error, line, column, shape, structured) { + const frames = structured ? error.stack : error.stack.split("\n").filter(frame => /:(\d+):(\d+)\)?$/.test(frame)); + const actual = structured ? [frames[0].getLineNumber(), frames[0].getColumnNumber()] : /:(\d+):(\d+)\)?$/.exec(frames[0])?.slice(1).map(Number); + if (!actual || actual[0] !== line || actual[1] !== column) + throw new Error(JSON.stringify({ shape, structured, expected: [line, column], actual, trace: structured ? frames.map(String) : error.stack })); +} +for (let iteration = 0; iteration < Math.min(typeof testLoopCount === "number" ? testLoopCount : 20, 100); ++iteration) { + for (const [shape, create, line, column] of cases) { + checkPosition(create(), line, column, shape, false); + if (typeof process !== "undefined") { + const previous = Error.prepareStackTrace; + try { + Error.prepareStackTrace = (_, frames) => frames; + checkPosition(create(), line, column, shape, true); + } finally { + Error.prepareStackTrace = previous; + } + } + } +} diff --git a/test/js/node/vm/__snapshots__/vm-sourceUrl.test.ts.snap b/test/js/node/vm/__snapshots__/vm-sourceUrl.test.ts.snap index bbc85154f8ee..43f86730c886 100644 --- a/test/js/node/vm/__snapshots__/vm-sourceUrl.test.ts.snap +++ b/test/js/node/vm/__snapshots__/vm-sourceUrl.test.ts.snap @@ -16,7 +16,7 @@ exports[`can get sourceURL inside node:vm 1`] = ` ^ error: hello at hello (hellohello.js:4:24) - at hellohello.js:7:6 + at hellohello.js:7:1 at (:21:15) " `; @@ -26,7 +26,7 @@ exports[`eval sourceURL is correct 1`] = ` ^ error: hello at hello (hellohello.js:4:24) - at eval (hellohello.js:7:6) + at eval (hellohello.js:7:1) at (:39:15) " `;