From d707b6282a706b840d5dc497fd17f1a472bca910 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 11 Sep 2024 18:12:50 +0200 Subject: [PATCH 1/6] report error to stderr when an error in a subscribtion happens --- crates/napi/src/next_api/project.rs | 5 ++--- crates/napi/src/next_api/utils.rs | 7 ++++++- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/crates/napi/src/next_api/project.rs b/crates/napi/src/next_api/project.rs index 10889cd39d3b8..cf80a01c723b6 100644 --- a/crates/napi/src/next_api/project.rs +++ b/crates/napi/src/next_api/project.rs @@ -48,7 +48,7 @@ use super::{ TurbopackResult, VcArc, }, }; -use crate::{register, util::log_panic_and_inform}; +use crate::register; /// Used by [`benchmark_file_io`]. This is a noisy benchmark, so set the /// threshold high. @@ -736,8 +736,7 @@ pub fn project_hmr_events( let update = hmr_update(project, identifier.clone(), state) .strongly_consistent() - .await - .inspect_err(|e| log_panic_and_inform(e))?; + .await?; let HmrUpdateWithIssues { update, issues, diff --git a/crates/napi/src/next_api/utils.rs b/crates/napi/src/next_api/utils.rs index 286a8acf01cf3..c0aca199566df 100644 --- a/crates/napi/src/next_api/utils.rs +++ b/crates/napi/src/next_api/utils.rs @@ -17,6 +17,8 @@ use turbopack_core::{ source_pos::SourcePos, }; +use crate::util::log_panic_and_inform; + /// A helper type to hold both a Vc operation and the TurboTasks root process. /// Without this, we'd need to pass both individually all over the place #[derive(Clone)] @@ -312,7 +314,10 @@ pub fn subscribe> + Send, let result = handler().await; let status = func.call( - result.map_err(|e| napi::Error::from_reason(PrettyPrintError(&e).to_string())), + result.map_err(|e| { + log_panic_and_inform(&e); + napi::Error::from_reason(PrettyPrintError(&e).to_string()) + }), ThreadsafeFunctionCallMode::NonBlocking, ); if !matches!(status, Status::Ok) { From f9eb0537d7470a5772b785d344f094871545785e Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 11 Sep 2024 18:15:35 +0200 Subject: [PATCH 2/6] rename method --- crates/napi/src/lib.rs | 2 +- crates/napi/src/next_api/utils.rs | 4 ++-- crates/napi/src/util.rs | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/napi/src/lib.rs b/crates/napi/src/lib.rs index f86b9b698c208..4beaea1f91981 100644 --- a/crates/napi/src/lib.rs +++ b/crates/napi/src/lib.rs @@ -81,7 +81,7 @@ static ALLOC: dhat::Alloc = dhat::Alloc; fn init() { set_hook(Box::new(|panic_info| { - util::log_panic_and_inform(format!( + util::log_internal_error_and_inform(format!( "Panic: {}\nBacktrace: {:?}", panic_info, Backtrace::new() diff --git a/crates/napi/src/next_api/utils.rs b/crates/napi/src/next_api/utils.rs index c0aca199566df..ba32d593613b3 100644 --- a/crates/napi/src/next_api/utils.rs +++ b/crates/napi/src/next_api/utils.rs @@ -17,7 +17,7 @@ use turbopack_core::{ source_pos::SourcePos, }; -use crate::util::log_panic_and_inform; +use crate::util::log_internal_error_and_inform; /// A helper type to hold both a Vc operation and the TurboTasks root process. /// Without this, we'd need to pass both individually all over the place @@ -315,7 +315,7 @@ pub fn subscribe> + Send, let status = func.call( result.map_err(|e| { - log_panic_and_inform(&e); + log_internal_error_and_inform(&e); napi::Error::from_reason(PrettyPrintError(&e).to_string()) }), ThreadsafeFunctionCallMode::NonBlocking, diff --git a/crates/napi/src/util.rs b/crates/napi/src/util.rs index 11ab24e4c5ef4..5609941954850 100644 --- a/crates/napi/src/util.rs +++ b/crates/napi/src/util.rs @@ -52,7 +52,7 @@ static PANIC_LOG: Lazy = Lazy::new(|| { path }); -pub fn log_panic_and_inform(err_info: impl Debug) { +pub fn log_internal_error_and_inform(err_info: impl Debug) { if cfg!(debug_assertions) || env::var("SWC_DEBUG") == Ok("1".to_string()) || env::var("CI").is_ok_and(|v| !v.is_empty()) From c36a3b8e54127aa098d3338e8614652e91858d97 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Wed, 11 Sep 2024 18:16:03 +0200 Subject: [PATCH 3/6] pretty error --- crates/napi/src/next_api/utils.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/crates/napi/src/next_api/utils.rs b/crates/napi/src/next_api/utils.rs index ba32d593613b3..62cefa34b7af5 100644 --- a/crates/napi/src/next_api/utils.rs +++ b/crates/napi/src/next_api/utils.rs @@ -315,8 +315,9 @@ pub fn subscribe> + Send, let status = func.call( result.map_err(|e| { - log_internal_error_and_inform(&e); - napi::Error::from_reason(PrettyPrintError(&e).to_string()) + let error = PrettyPrintError(&e).to_string(); + log_internal_error_and_inform(&error); + napi::Error::from_reason(error) }), ThreadsafeFunctionCallMode::NonBlocking, ); From 18f3254b37c58c93583947347c5f989e15e8aa73 Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 12 Sep 2024 11:48:21 +0200 Subject: [PATCH 4/6] format error correctly --- crates/napi/src/util.rs | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/crates/napi/src/util.rs b/crates/napi/src/util.rs index 5609941954850..c15756be872a2 100644 --- a/crates/napi/src/util.rs +++ b/crates/napi/src/util.rs @@ -29,7 +29,7 @@ DEALINGS IN THE SOFTWARE. use std::{ cell::RefCell, env, - fmt::Debug, + fmt::{Debug, Display}, fs::OpenOptions, io::{self, BufRead, Write}, path::PathBuf, @@ -52,13 +52,13 @@ static PANIC_LOG: Lazy = Lazy::new(|| { path }); -pub fn log_internal_error_and_inform(err_info: impl Debug) { +pub fn log_internal_error_and_inform(err_info: &str) { if cfg!(debug_assertions) || env::var("SWC_DEBUG") == Ok("1".to_string()) || env::var("CI").is_ok_and(|v| !v.is_empty()) { eprintln!( - "{}: An unexpected Turbopack error occurred:\n{:?}", + "{}: An unexpected Turbopack error occurred:\n{}", "FATAL".red().bold(), err_info ); @@ -121,7 +121,7 @@ pub fn log_internal_error_and_inform(err_info: impl Debug) { .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(); + writeln!(log_file, "{}\n{}", LOG_DIVIDER, err_info).unwrap(); eprintln!("{}: An unexpected Turbopack error occurred. Please report the content of {} to https://github.com/vercel/next.js/issues/new", "FATAL".red().bold(), PANIC_LOG.to_string_lossy()); } From 6b306c427f4e548f46827e8b3e3c6a6f00fa29fa Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Thu, 12 Sep 2024 16:48:36 +0200 Subject: [PATCH 5/6] fixup --- crates/napi/src/lib.rs | 2 +- crates/napi/src/util.rs | 1 - 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/crates/napi/src/lib.rs b/crates/napi/src/lib.rs index 4beaea1f91981..c0bf1ffb4a8db 100644 --- a/crates/napi/src/lib.rs +++ b/crates/napi/src/lib.rs @@ -81,7 +81,7 @@ static ALLOC: dhat::Alloc = dhat::Alloc; fn init() { set_hook(Box::new(|panic_info| { - util::log_internal_error_and_inform(format!( + util::log_internal_error_and_inform(&format!( "Panic: {}\nBacktrace: {:?}", panic_info, Backtrace::new() diff --git a/crates/napi/src/util.rs b/crates/napi/src/util.rs index c15756be872a2..dbd54cfe0df21 100644 --- a/crates/napi/src/util.rs +++ b/crates/napi/src/util.rs @@ -29,7 +29,6 @@ DEALINGS IN THE SOFTWARE. use std::{ cell::RefCell, env, - fmt::{Debug, Display}, fs::OpenOptions, io::{self, BufRead, Write}, path::PathBuf, From 68e3618ce681a0d9d7d72c53cffa1eba48b0e27b Mon Sep 17 00:00:00 2001 From: Tobias Koppers Date: Mon, 9 Sep 2024 16:25:50 +0200 Subject: [PATCH 6/6] Report error to error overlay --- .../src/server/dev/hot-reloader-turbopack.ts | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/packages/next/src/server/dev/hot-reloader-turbopack.ts b/packages/next/src/server/dev/hot-reloader-turbopack.ts index ffc2828f29f78..f231814335b5d 100644 --- a/packages/next/src/server/dev/hot-reloader-turbopack.ts +++ b/packages/next/src/server/dev/hot-reloader-turbopack.ts @@ -60,6 +60,7 @@ import { isWellKnownError, printNonFatalIssue, normalizedPageToTurbopackStructureRoute, + TurbopackInternalError, } from './turbopack-utils' import { propagateServerField, @@ -465,6 +466,23 @@ export async function createHotReloaderTurbopack( } } } catch (e) { + if (e instanceof TurbopackInternalError) { + sendToClient(client, { + action: HMR_ACTIONS_SENT_TO_BROWSER.SYNC, + errors: [ + { + message: + 'An unexpected Turbopack error occurred. Please see the output of `next dev` for more details.', + }, + ], + hash: String(++hmrHash), + warnings: [], + versionInfo: await versionInfoPromise, + }) + client.close() + return + } + // The client might be using an HMR session from a previous server, tell them // to fully reload the page to resolve the issue. We can't use // `hotReloader.send` since that would force every connected client to