Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack panics: create discussions with pre-filled errors #76850

Merged
merged 4 commits into from
Mar 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions crates/napi/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ rand = { workspace = true }
rustc-hash = { workspace = true }
serde = "1"
serde_json = "1"
supports-hyperlinks = "3.1.0"
terminal_hyperlink = "0.1.0"
tracing = { workspace = true }
tracing-subscriber = { workspace = true }
tracing-chrome = "0.5.0"
Expand Down
13 changes: 1 addition & 12 deletions crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,8 @@ DEALINGS IN THE SOFTWARE.
#[macro_use]
extern crate napi_derive;

use std::{
panic::set_hook,
sync::{Arc, Once},
};
use std::sync::{Arc, Once};

use backtrace::Backtrace;
use napi::bindgen_prelude::*;
use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{
Expand Down Expand Up @@ -76,13 +72,6 @@ fn init() {
use tokio::runtime::Builder;
use turbo_tasks_malloc::TurboMalloc;

set_hook(Box::new(|panic_info| {
util::log_internal_error_and_inform(&format!(
"Panic: {}\nBacktrace: {:?}",
panic_info,
Backtrace::new()
));
}));
let rt = Builder::new_multi_thread()
.enable_all()
.on_thread_stop(|| {
Expand Down
5 changes: 2 additions & 3 deletions crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -464,9 +464,8 @@ pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send,

let status = func.call(
result.map_err(|e| {
let error = PrettyPrintError(&e).to_string();
log_internal_error_and_inform(&error);
napi::Error::from_reason(error)
log_internal_error_and_inform(&e);
napi::Error::from_reason(PrettyPrintError(&e).to_string())
}),
ThreadsafeFunctionCallMode::NonBlocking,
);
Expand Down
39 changes: 35 additions & 4 deletions crates/napi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ use anyhow::anyhow;
use napi::bindgen_prelude::{External, Status};
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use terminal_hyperlink::Hyperlink;
use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer};
use turbopack_core::error::PrettyPrintError;

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

pub fn log_internal_error_and_inform(err_info: &str) {
pub fn log_internal_error_and_inform(internal_error: &anyhow::Error) {
if cfg!(debug_assertions)
|| env::var("SWC_DEBUG") == Ok("1".to_string())
|| env::var("CI").is_ok_and(|v| !v.is_empty())
Expand All @@ -61,7 +63,7 @@ pub fn log_internal_error_and_inform(err_info: &str) {
eprintln!(
"{}: An unexpected Turbopack error occurred:\n{}",
"FATAL".red().bold(),
err_info
PrettyPrintError(internal_error)
);
return;
}
Expand Down Expand Up @@ -122,8 +124,37 @@ pub fn log_internal_error_and_inform(err_info: &str) {
.open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));

writeln!(log_file, "{}\n{}", LOG_DIVIDER, err_info).unwrap();
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());
let internal_error_str: String = PrettyPrintError(internal_error).to_string();
writeln!(log_file, "{}\n{}", LOG_DIVIDER, &internal_error_str).unwrap();

let title = format!(
"Turbopack Error: {}",
internal_error_str.lines().next().unwrap_or("Unknown")
);
let version_str = format!("Turbopack version: `{}`", env!("VERGEN_GIT_DESCRIBE"));
let new_discussion_url = if supports_hyperlinks::supports_hyperlinks() {
"clicking here.".hyperlink(
format!(
"https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &internal_error_str))
)
)
} else {
format!(
"clicking here: https://github.com/vercel/next.js/discussions/new?category=turbopack-error-report&title={}&body={}&labels=Turbopack,Turbopack%20Panic%20Backtrace",
&urlencoding::encode(&title),
&urlencoding::encode(&format!("{}\n\nError message:\n```\n{}\n```", &version_str, &title))
)
};

eprintln!(
"\n-----\n{}: An unexpected Turbopack error occurred. A panic log has been written to \
{}.\n\nTo help make Turbopack better, report this error by {}\n-----\n",
"FATAL".red().bold(),
PANIC_LOG.to_string_lossy(),
&new_discussion_url
);
}

#[napi]
Expand Down
Loading