Unverified Commit 13670aea authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

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

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
-----
```

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent dfd18f97
...@@ -4586,7 +4586,9 @@ dependencies = [ ...@@ -4586,7 +4586,9 @@ dependencies = [
"rustc-hash 2.1.0", "rustc-hash 2.1.0",
"serde", "serde",
"serde_json", "serde_json",
"supports-hyperlinks",
"swc_core", "swc_core",
"terminal_hyperlink",
"tokio", "tokio",
"tracing", "tracing",
"tracing-chrome", "tracing-chrome",
...@@ -6959,6 +6961,12 @@ version = "2.5.0" ...@@ -6959,6 +6961,12 @@ version = "2.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index" source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc" checksum = "81cdd64d312baedb58e21336b31bc043b77e01cc99033ce76ef539f78e965ebc"
[[package]]
name = "supports-hyperlinks"
version = "3.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "804f44ed3c63152de6a9f90acbea1a110441de43006ea51bcce8f436196a288b"
[[package]] [[package]]
name = "swc" name = "swc"
version = "16.1.1" version = "16.1.1"
...@@ -8484,6 +8492,12 @@ dependencies = [ ...@@ -8484,6 +8492,12 @@ dependencies = [
"winapi-util", "winapi-util",
] ]
[[package]]
name = "terminal_hyperlink"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "db7bfa7333cbbfca467f40042a640d3c86063d6e4085cef4a6effdbad7005d50"
[[package]] [[package]]
name = "terminal_size" name = "terminal_size"
version = "0.3.0" version = "0.3.0"
......
...@@ -66,6 +66,8 @@ rand = { workspace = true } ...@@ -66,6 +66,8 @@ rand = { workspace = true }
rustc-hash = { workspace = true } rustc-hash = { workspace = true }
serde = "1" serde = "1"
serde_json = "1" serde_json = "1"
supports-hyperlinks = "3.1.0"
terminal_hyperlink = "0.1.0"
tracing = { workspace = true } tracing = { workspace = true }
tracing-subscriber = { workspace = true } tracing-subscriber = { workspace = true }
tracing-chrome = "0.5.0" tracing-chrome = "0.5.0"
......
...@@ -34,12 +34,8 @@ DEALINGS IN THE SOFTWARE. ...@@ -34,12 +34,8 @@ DEALINGS IN THE SOFTWARE.
#[macro_use] #[macro_use]
extern crate napi_derive; extern crate napi_derive;
use std::{ use std::sync::{Arc, Once};
panic::set_hook,
sync::{Arc, Once},
};
use backtrace::Backtrace;
use napi::bindgen_prelude::*; use napi::bindgen_prelude::*;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{ use swc_core::{
...@@ -76,13 +72,6 @@ fn init() { ...@@ -76,13 +72,6 @@ fn init() {
use tokio::runtime::Builder; use tokio::runtime::Builder;
use turbo_tasks_malloc::TurboMalloc; 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() let rt = Builder::new_multi_thread()
.enable_all() .enable_all()
.on_thread_stop(|| { .on_thread_stop(|| {
......
...@@ -464,9 +464,8 @@ pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send, ...@@ -464,9 +464,8 @@ pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + Send,
let status = func.call( let status = func.call(
result.map_err(|e| { result.map_err(|e| {
let error = PrettyPrintError(&e).to_string(); log_internal_error_and_inform(&e);
log_internal_error_and_inform(&error); napi::Error::from_reason(PrettyPrintError(&e).to_string())
napi::Error::from_reason(error)
}), }),
ThreadsafeFunctionCallMode::NonBlocking, ThreadsafeFunctionCallMode::NonBlocking,
); );
......
...@@ -40,8 +40,10 @@ use anyhow::anyhow; ...@@ -40,8 +40,10 @@ use anyhow::anyhow;
use napi::bindgen_prelude::{External, Status}; use napi::bindgen_prelude::{External, Status};
use once_cell::sync::Lazy; use once_cell::sync::Lazy;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
use terminal_hyperlink::Hyperlink;
use tracing_chrome::{ChromeLayerBuilder, FlushGuard}; use tracing_chrome::{ChromeLayerBuilder, FlushGuard};
use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer}; use tracing_subscriber::{filter, prelude::*, util::SubscriberInitExt, Layer};
use turbopack_core::error::PrettyPrintError;
static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None); static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None);
static LOG_DIVIDER: &str = "---------------------------"; static LOG_DIVIDER: &str = "---------------------------";
...@@ -51,7 +53,7 @@ static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| { ...@@ -51,7 +53,7 @@ static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
path 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) if cfg!(debug_assertions)
|| env::var("SWC_DEBUG") == Ok("1".to_string()) || env::var("SWC_DEBUG") == Ok("1".to_string())
|| env::var("CI").is_ok_and(|v| !v.is_empty()) || env::var("CI").is_ok_and(|v| !v.is_empty())
...@@ -61,7 +63,7 @@ pub fn log_internal_error_and_inform(err_info: &str) { ...@@ -61,7 +63,7 @@ pub fn log_internal_error_and_inform(err_info: &str) {
eprintln!( eprintln!(
"{}: An unexpected Turbopack error occurred:\n{}", "{}: An unexpected Turbopack error occurred:\n{}",
"FATAL".red().bold(), "FATAL".red().bold(),
err_info PrettyPrintError(internal_error)
); );
return; return;
} }
...@@ -122,8 +124,37 @@ pub fn log_internal_error_and_inform(err_info: &str) { ...@@ -122,8 +124,37 @@ pub fn log_internal_error_and_inform(err_info: &str) {
.open(PANIC_LOG.as_path()) .open(PANIC_LOG.as_path())
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy())); .unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
writeln!(log_file, "{}\n{}", LOG_DIVIDER, err_info).unwrap(); let internal_error_str: String = PrettyPrintError(internal_error).to_string();
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()); 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] #[napi]
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment