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

Report Turbopack error to error overlay #69876

Open
wants to merge 6 commits into
base: canary
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions crates/napi/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
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.
Expand Down Expand Up @@ -736,8 +736,7 @@

let update = hmr_update(project, identifier.clone(), state)
.strongly_consistent()
.await
.inspect_err(|e| log_panic_and_inform(e))?;
.await?;
let HmrUpdateWithIssues {
update,
issues,
Expand Down Expand Up @@ -910,7 +909,7 @@
}
}

/// Subscribes to lifecycle events of the compilation.

Check warning on line 912 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`

Check warning on line 912 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 912 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::End`

Check warning on line 912 in crates/napi/src/next_api/project.rs

View workflow job for this annotation

GitHub Actions / rustdoc check / build

public documentation for `project_update_info_subscribe` links to private item `UpdateMessage::Start`
/// Emits an [UpdateMessage::Start] event when any computation starts.
/// Emits an [UpdateMessage::End] event when there was no computation for the
/// specified time (`aggregation_ms`). The [UpdateMessage::End] event contains
Expand Down
8 changes: 7 additions & 1 deletion crates/napi/src/next_api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use turbopack_core::{
source_pos::SourcePos,
};

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
#[derive(Clone)]
Expand Down Expand Up @@ -312,7 +314,11 @@ pub fn subscribe<T: 'static + Send + Sync, F: Future<Output = Result<T>> + 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| {
let error = PrettyPrintError(&e).to_string();
log_internal_error_and_inform(&error);
napi::Error::from_reason(error)
}),
ThreadsafeFunctionCallMode::NonBlocking,
);
if !matches!(status, Status::Ok) {
Expand Down
7 changes: 3 additions & 4 deletions crates/napi/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ DEALINGS IN THE SOFTWARE.
use std::{
cell::RefCell,
env,
fmt::Debug,
fs::OpenOptions,
io::{self, BufRead, Write},
path::PathBuf,
Expand All @@ -52,13 +51,13 @@ static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
path
});

pub fn log_panic_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
);
Expand Down Expand Up @@ -121,7 +120,7 @@ pub fn log_panic_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());
}

Expand Down
18 changes: 18 additions & 0 deletions packages/next/src/server/dev/hot-reloader-turbopack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ import {
isWellKnownError,
printNonFatalIssue,
normalizedPageToTurbopackStructureRoute,
TurbopackInternalError,
} from './turbopack-utils'
import {
propagateServerField,
Expand Down Expand Up @@ -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
Expand Down
Loading