Skip to content

Commit 8e67813

Browse files
committed
Turbopack panics: create discussions with pre-filled errors
This: - Prompts users to report turbopack internal errors as new GitHub discussions instead of issues - Encodes the error message and debug information into the discussion title and body respectively - Since these encoded urls can be lengthy, it uses [Terminal Hyperlinks](https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda) to present things concisely without excessive wrapping. If the terminal does not support this, only the error message is sent. - Removes specific panic handling in favor of the `subscribe` napi error handler. This seems to be the vast majority of panics. Test Plan: In a terminal with hyperlink support `FORCE_HYPERLINK=1 pnpm next dev --turbo examples/basic-css/`: ``` ----- FATAL: An unexpected Turbopack error occurred. A panic log has been written to /var/folders/sk/zy7023wd30j0dxpmdv3nylm80000gn/T/next-panic-bf6bdb325db0f5ed5599cf332d8e364a.log. To help make Turbopack better, report this error by clicking here. ----- ``` Where the link generated is [clicking here.](https://github.com/vercel/next.js/discussions/new?category=error-report&title=Turbopack%20Error%3A%20Panic%20in%20AppProject%3A%3Anew%3A%20Some%28%22oh%20no%20new%20app%21%22%29&body=Error%20message%3A%0A%60%60%60%0APanic%20in%20AppProject%3A%3Anew%3A%20Some%28%22oh%20no%20new%20app%21%22%29%0A%0ADebug%20info%3A%0A-%20Execution%20of%20get_entrypoints_with_issues_operation%20failed%0A-%20Execution%20of%20EntrypointsOperation%3A%3Anew%20failed%0A-%20Execution%20of%20Project%3A%3Aentrypoints%20failed%0A-%20Execution%20of%20Project%3A%3Aapp_project%20failed%0A-%20Panic%20in%20AppProject%3A%3Anew%3A%20Some%28%22oh%20no%20new%20app%21%22%29%0A%60%60%60&labels=Turbopack,Turbopack%20Panic%20Backtrace) In a terminal without hyperlink support `FORCE_HYPERLINK=0 pnpm next dev --turbo examples/basic-css/`: ``` ----- FATAL: An unexpected Turbopack error occurred. A panic log has been written to /var/folders/sk/zy7023wd30j0dxpmdv3nylm80000gn/T/next-panic-3c5a833c682727cba1d51cc97ee0c16.log. To help make Turbopack better, report this error by clicking here: https://github.com/vercel/next.js/discussions/new?category=error-report&title=Turbopack%20Error%3A%20Panic%20in%20AppProject%3A%3Anew%3A%20Some%28%22oh%20no%20new%20app%21%22%29&body=Error%20message%3A%0A%60%60%60%0ATurbopack%20Error%3A%20Panic%20in%20AppProject%3A%3Anew%3A%20Some%28%22oh%20no%20new%20app%21%22%29%0A%60%60%60&labels=Turbopack,Turbopack%20Panic%20Backtrace ----- ```
1 parent 910b07b commit 8e67813

File tree

5 files changed

+53
-19
lines changed

5 files changed

+53
-19
lines changed

Cargo.lock

+14
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/napi/Cargo.toml

+2
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ rustc-hash = { workspace = true }
6767
serde = "1"
6868
serde_json = "1"
6969
shadow-rs = { workspace = true }
70+
supports-hyperlinks = "3.1.0"
71+
terminal_hyperlink = "0.1.0"
7072
tracing = { workspace = true }
7173
tracing-subscriber = { workspace = true }
7274
tracing-chrome = "0.5.0"

crates/napi/src/lib.rs

+1-12
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ DEALINGS IN THE SOFTWARE.
3434
#[macro_use]
3535
extern crate napi_derive;
3636

37-
use std::{
38-
panic::set_hook,
39-
sync::{Arc, Once},
40-
};
37+
use std::sync::{Arc, Once};
4138

42-
use backtrace::Backtrace;
4339
use napi::bindgen_prelude::*;
4440
use rustc_hash::{FxHashMap, FxHashSet};
4541
use swc_core::{
@@ -79,13 +75,6 @@ fn init() {
7975
use tokio::runtime::Builder;
8076
use turbo_tasks_malloc::TurboMalloc;
8177

82-
set_hook(Box::new(|panic_info| {
83-
util::log_internal_error_and_inform(&format!(
84-
"Panic: {}\nBacktrace: {:?}",
85-
panic_info,
86-
Backtrace::new()
87-
));
88-
}));
8978
let rt = Builder::new_multi_thread()
9079
.enable_all()
9180
.on_thread_stop(|| {

crates/napi/src/next_api/utils.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -477,9 +477,8 @@ pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send,
477477

478478
let status = func.call(
479479
result.map_err(|e| {
480-
let error = PrettyPrintError(&e).to_string();
481-
log_internal_error_and_inform(&error);
482-
napi::Error::from_reason(error)
480+
log_internal_error_and_inform(&e);
481+
napi::Error::from_reason(PrettyPrintError(&e).to_string())
483482
}),
484483
ThreadsafeFunctionCallMode::NonBlocking,
485484
);

crates/napi/src/util.rs

+34-4
Original file line numberDiff line numberDiff line change
@@ -40,8 +40,10 @@ use anyhow::anyhow;
4040
use napi::bindgen_prelude::{External, Status};
4141
use once_cell::sync::Lazy;
4242
use owo_colors::OwoColorize;
43+
use terminal_hyperlink::Hyperlink;
4344
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
4445
use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer};
46+
use turbopack_core::error::PrettyPrintError;
4547

4648
static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None);
4749
static LOG_DIVIDER: &str = "---------------------------";
@@ -51,7 +53,7 @@ static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
5153
path
5254
});
5355

54-
pub fn log_internal_error_and_inform(err_info: &str) {
56+
pub fn log_internal_error_and_inform(internal_error: &anyhow::Error) {
5557
if cfg!(debug_assertions)
5658
|| env::var("SWC_DEBUG") == Ok("1".to_string())
5759
|| env::var("CI").is_ok_and(|v| !v.is_empty())
@@ -61,7 +63,7 @@ pub fn log_internal_error_and_inform(err_info: &str) {
6163
eprintln!(
6264
"{}: An unexpected Turbopack error occurred:\n{}",
6365
"FATAL".red().bold(),
64-
err_info
66+
internal_error.to_string()
6567
);
6668
return;
6769
}
@@ -122,8 +124,36 @@ pub fn log_internal_error_and_inform(err_info: &str) {
122124
.open(PANIC_LOG.as_path())
123125
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
124126

125-
writeln!(log_file, "{}\n{}", LOG_DIVIDER, err_info).unwrap();
126-
eprintln!("{}: An unexpected Turbopack error occurred. Please report the content of {}, along with a description of what you were doing when the error occurred, to https://github.com/vercel/next.js/issues/new?template=1.bug_report.yml", "FATAL".red().bold(), PANIC_LOG.to_string_lossy());
127+
let internal_error_str: String = PrettyPrintError(internal_error).to_string();
128+
writeln!(log_file, "{}\n{}", LOG_DIVIDER, &internal_error_str).unwrap();
129+
130+
let title = format!(
131+
"Turbopack Error: {}",
132+
internal_error_str.lines().next().unwrap_or("Unknown")
133+
);
134+
let new_discussion_url = if supports_hyperlinks::supports_hyperlinks() {
135+
"clicking here.".hyperlink(
136+
format!(
137+
"https://github.com/vercel/next.js/discussions/new?category=error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
138+
&urlencoding::encode(&title),
139+
&urlencoding::encode(&format!("Error message:\n```\n{}\n```", &internal_error_str))
140+
)
141+
)
142+
} else {
143+
format!(
144+
"clicking here: https://github.com/vercel/next.js/discussions/new?category=error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
145+
&urlencoding::encode(&title),
146+
&urlencoding::encode(&format!("Error message:\n```\n{}\n```", title))
147+
)
148+
};
149+
150+
eprintln!(
151+
"\n-----\n{}: An unexpected Turbopack error occurred. A panic log has been written to \
152+
{}.\n\nTo help make Turbopack better, report this error by {}\n-----\n",
153+
"FATAL".red().bold(),
154+
PANIC_LOG.to_string_lossy(),
155+
&new_discussion_url
156+
);
127157
}
128158

129159
#[napi]

0 commit comments

Comments
 (0)