Panic(thread 536264): TODO: Support Template Literals In Macros

by ADMIN 64 views

Introduction

Bun, a fast and lightweight JavaScript runtime, has encountered a critical issue that causes it to crash when encountering template literals in macros. This article aims to provide a detailed explanation of the problem, its reproduction steps, and the relevant log output. We will also explore the stack trace and discuss the possible solutions to this issue.

Reproducing the Crash

To reproduce the crash, follow these steps:

  1. Create a simple function that uses template literals:
export const ico = (name: TemplateStringsArray) => `/svg/spritesheet.svg#${name[0]}`;
  1. Import the function as a macro:
import { ico } from "../src-helpers/icon-path" with { type: "macro" };
  1. Use the macro in a console.log statement:
console.log(ico`hello`);
  1. Bundle the file with Bun using the following command:
bun run js

Relevant Log Output

The relevant log output from the crash is as follows:

❯ bun run js   
$ bun ./scripts/build-js.ts
⚙ Compiling 43 files
File alerts.ts compiled in 5.12 ms
File chapter-reads.ts compiled in 8.66 ms
File comments.ts compiled in 9.95 ms
File cookie-consent.ts compiled in 31.33 ms
File input-progressbars.ts compiled in 33.94 ms
File local-settings.ts compiled in 37.06 ms
File login.ts compiled in 40.78 ms
File navbar.ts compiled in 42.78 ms
File notifications.ts compiled in 61.62 ms
============================================================
Bun v1.2.2 (c1708ea6) Windows x64
Windows v.win10_fe
CPU: sse42 avx avx2 avx512
Args: "C:\Users\kouma\.bun\bin\bun.exe" "./scripts/build-js.ts"
Features: Bun.stderr(2) Bun.stdin(2) Bun.stdout(2) bunfig jsc(2) macros(3) transpiler_cache(2) tsconfig(2) napi_module_register process_dlopen 
Builtins: "bun:main" "node:buffer" "node:child_process" "node:events" "node:fs" "node:os" "node:path" "node:process" "node:string_decoder" "node:tty" "node:util" "node:util/types" 
Elapsed: 197ms | User: 109ms | Sys: 234ms
RSS: 0.33GB | Peak: 0.33GB | Commit: 0.54GB | Faults: 92459

panic(thread 536264): TODO: support template literals in macros
Crashed while visiting F:\Projects\MyCoolProject\MyCoolProject\wwwroot\js\src\passwords.ts
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.2.2/wa1c1708eagDz7/73/B83vtkB2k7moB+gppiBu3q4gBkg93gBm+qweytg0bk1nuiB6x+veytg0bA0eNoL8Xfxt1IoLi0oyC8qUShJzS3ISSxJVcjJLEktSswpVsjMU8hNTC7KLwYAOGIPRg

error: script "js" exited with code 3

Stack Trace (bun.report)

The stack trace from bun.report is as follows:

Bun v1.2.2 ([`c1708ea`](<https://github.com/oven-sh/bun/tree/c1708ea6ab529a4c747a8282a24d125dc20b0a63>)) on windows x86_64 [AutoCommand]

**panic**: TODO: support template literals in macros

- [`js_ast.zig:8461`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_ast.zig#L8461>): `run`
- [`bindings.cpp:5171`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/bun.js/bindings/bindings.cpp#L5171>): `JSC__VM__holdAPILock`
- [`js_ast.zig:8047`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_ast.zig#L8047>): `call`
- [`js_parser.zig:16625`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L16625>): `visitExprInOut`
- [`js_parser.zig:17449`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L17449>): `visitExprInOut`
- [`js_parser.zig:19786`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L19786>): `s_expr`
- [`js_parser.zig:22232`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L22232>): `visitStmts`
- [`js_parser.zig:21768`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L21768>): `visitSingleStmtBlock`
- [`js_parser.zig:19987`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L19987>): `s_if`
- [`js_parser.zig:22232`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L22232>): `visitStmts`

Features: napi\_module\_register, process\_dlopen, Bun.stderr, Bun.stdin, Bun.stdout, bunfig, jsc, macros, transpiler\_cache, tsconfig, tsconfig

<!-- from bun.report: ZXcTT3cnlFdHk9qvRaWW -->

<sub>Sentry Issue: <strong><a href="https://bun-p9.sentry.io/issues/6382618383/">BUN-E0W</a></strong></sub>

Conclusion

Q: What is the panic(thread 536264): TODO: support template literals in macros issue?

A: The panic(thread 536264): TODO: support template literals in macros issue is a critical bug in Bun that causes it to crash when encountering template literals in macros.

Q: What are template literals in macros?

A: Template literals are a feature in JavaScript that allows you to embed expressions inside string literals using the backtick character (`). Macros are a feature in Bun that allows you to define reusable code snippets.

Q: How do I reproduce the crash?

A: To reproduce the crash, follow these steps:

  1. Create a simple function that uses template literals:
export const ico = (name: TemplateStringsArray) => `/svg/spritesheet.svg#${name[0]}`;
  1. Import the function as a macro:
import { ico } from "../src-helpers/icon-path" with { type: "macro" };
  1. Use the macro in a console.log statement:
console.log(ico`hello`);
  1. Bundle the file with Bun using the following command:
bun run js

Q: What is the relevant log output from the crash?

A: The relevant log output from the crash is as follows:

❯ bun run js   
$ bun ./scripts/build-js.ts
⚙ Compiling 43 files
File alerts.ts compiled in 5.12 ms
File chapter-reads.ts compiled in 8.66 ms
File comments.ts compiled in 9.95 ms
File cookie-consent.ts compiled in 31.33 ms
File input-progressbars.ts compiled in 33.94 ms
File local-settings.ts compiled in 37.06 ms
File login.ts compiled in 40.78 ms
File navbar.ts compiled in 42.78 ms
File notifications.ts compiled in 61.62 ms
============================================================
Bun v1.2.2 (c1708ea6) Windows x64
Windows v.win10_fe
CPU: sse42 avx avx2 avx512
Args: "C:\Users\kouma\.bun\bin\bun.exe" "./scripts/build-js.ts"
Features: Bun.stderr(2) Bun.stdin(2) Bun.stdout(2) bunfig jsc(2) macros(3) transpiler_cache(2) tsconfig(2) napi_module_register process_dlopen 
Builtins: "bun:main" "node:buffer" "node:child_process" "node:events" "node:fs" "node:os" "node:path" "node:process" "node:string_decoder" "node:tty" "node:util" "node:util/types" 
Elapsed: 197ms | User: 109ms | Sys: 234ms
RSS: 0.33GB | Peak: 0.33GB | Commit: 0.54GB | Faults: 92459

panic(thread 536264): TODO: support template literals in macros
Crashed while visiting F:\Projects\MyCoolProject\MyCoolProject\wwwroot\js\src\passwords.ts
oh no: Bun has crashed. This indicates a bug in Bun, not your code.

To send a redacted crash report to Bun's team,
please file a GitHub issue using the link below:

 https://bun.report/1.2.2/wa1c1708eagDz7/73/B83vtkB2k7moB+gppiBu3q4gBkg93gBm+qweytg0bk1nuiB6x+veytg0bA0eNoL8Xfxt1IoLi0oyC8qUShJzS3ISSxJVcjJLEktSswpVsjMU8hNTC7KLwYAOGIPRg

error: script "js" exited with code 3

Q: What is the stack trace from bun.report?

A: The stack trace from bun.report is as follows:

Bun v1.2.2 ([`c1708ea`](<https://github.com/oven-sh/bun/tree/c1708ea6ab529a4c747a8282a24d125dc20b0a63>)) on windows x86_64 [AutoCommand]

**panic**: TODO: support template literals in macros

- [`js_ast.zig:8461`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_ast.zig#L8461>): `run`
- [`bindings.cpp:5171`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/bun.js/bindings/bindings.cpp#L5171>): `JSC__VM__holdAPILock`
- [`js_ast.zig:8047`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_ast.zig#L8047>): `call`
- [`js_parser.zig:16625`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L16625>): `visitExprInOut`
- [`js_parser.zig:17449`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L17449>): `visitExprInOut`
- [`js_parser.zig:19786`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L19786>): `s_expr`
- [`js_parser.zig:22232`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L22232>): `visitStmts`
- [`js_parser.zig:21768`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L21768>): `visitSingleStmtBlock`
- [`js_parser.zig:19987`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L19987>): `s_if`
- [`js_parser.zig:22232`](<https://github.com/oven-sh/bun/blob/c1708ea6ab529a4c747a8282a24d125dc20b0a63/src/js_parser.zig#L22232>): `visitStmts`

Features: napi\_module\_register, process\_dlopen, Bun.stderr, Bun.stdin, Bun.stdout, bunfig, jsc, macros, transpiler\_cache, tsconfig, tsconfig

<!-- from bun.report: ZXcTT3cnlFdHk9qvRaWW -->

<sub>Sentry Issue: <strong><a href="https://bun-p9.sentry.io/issues/6382618383/">BUN-E0W</a></strong></sub>

Q: How can I help resolve this issue?

A: You can help resolve this issue by:

  • Reporting the issue on GitHub
  • Providing more information about the crash, such as the version of Bun you are using and the steps you took to reproduce the crash
  • Helping to debug the issue by providing more information about the code that is causing the crash

Q: What is the current status of the issue?

A: The current status of the issue is that it is a TODO item in the Bun codebase. This means that the issue has not been fully resolved and is still being worked on by the Bun team.