From a16605568b37b6845a645a43cc35bb07684b28cd Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Thu, 18 Jul 2024 17:08:59 -0700 Subject: [PATCH 1/2] Move turbopack panic log to tmpdir MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously we used $CWD, which is not reliably the user’s app directory. This also updates the error box text to instruct the user to reference the terminal output. Test Plan: Added a panic in Turbo and verified stderr and error box. --- packages/next-swc/crates/napi/src/lib.rs | 33 ++++++++++++++----- .../internal/helpers/nodeStackFrames.ts | 2 +- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/packages/next-swc/crates/napi/src/lib.rs b/packages/next-swc/crates/napi/src/lib.rs index 58ba389671457..d0852d5a42691 100644 --- a/packages/next-swc/crates/napi/src/lib.rs +++ b/packages/next-swc/crates/napi/src/lib.rs @@ -37,6 +37,7 @@ use std::{ env, io::prelude::*, panic::set_hook, + path::PathBuf, sync::{Arc, Mutex, Once}, time::Instant, }; @@ -44,6 +45,7 @@ use std::{ use backtrace::Backtrace; use fxhash::FxHashSet; use napi::bindgen_prelude::*; +use once_cell::sync::Lazy; use owo_colors::OwoColorize; use turbopack_binding::swc::core::{ base::{Compiler, TransformOutput}, @@ -77,7 +79,16 @@ static ALLOC: turbopack_binding::turbo::malloc::TurboMalloc = turbopack_binding::turbo::malloc::TurboMalloc; static LOG_THROTTLE: Mutex> = Mutex::new(None); -static LOG_FILE_PATH: &str = ".next/turbopack.log"; +static PANIC_LOG: Lazy = Lazy::new(|| { + let mut path = env::temp_dir(); + path.push(format!("next-panic-{:x}.log", rand::random::())); + path +}); + +#[napi] +pub fn get_panic_log_path() -> String { + PANIC_LOG.to_string_lossy().to_string() +} #[cfg(feature = "__internal_dhat-heap")] #[global_allocator] @@ -105,15 +116,17 @@ fn init() { if cfg!(debug_assertions) || env::var("SWC_DEBUG") == Ok("1".to_string()) { eprintln!("{}", info); } else { - let size = std::fs::metadata(LOG_FILE_PATH).map(|m| m.len()); + let size = std::fs::metadata(PANIC_LOG.as_path()).map(|m| m.len()); if let Ok(size) = size { if size > 512 * 1024 { // Truncate the earliest error from log file if it's larger than 512KB let new_lines = { let log_read = OpenOptions::new() .read(true) - .open(LOG_FILE_PATH) - .unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH)); + .open(PANIC_LOG.as_path()) + .unwrap_or_else(|_| { + panic!("Failed to open {}", PANIC_LOG.to_string_lossy()) + }); io::BufReader::new(&log_read) .lines() @@ -129,8 +142,10 @@ fn init() { .create(true) .truncate(true) .write(true) - .open(LOG_FILE_PATH) - .unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH)); + .open(PANIC_LOG.as_path()) + .unwrap_or_else(|_| { + panic!("Failed to open {}", PANIC_LOG.to_string_lossy()) + }); for line in new_lines { match line { @@ -148,11 +163,11 @@ fn init() { let mut log_file = OpenOptions::new() .create(true) .append(true) - .open(LOG_FILE_PATH) - .unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH)); + .open(PANIC_LOG.as_path()) + .unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy())); writeln!(log_file, "{}", 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(), LOG_FILE_PATH); + 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()); } })); } diff --git a/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts b/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts index c3e8ef1db45ef..a42ffb0bfb906 100644 --- a/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts +++ b/packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts @@ -29,7 +29,7 @@ export function getServerError(error: Error, type: ErrorSourceType): Error { // If this is an internal Turbopack error we shouldn't show internal details // to the user. These are written to a log file instead. const turbopackInternalError = new Error( - 'An unexpected Turbopack error occurred. Please report the content of .next/turbopack.log to the Next.js team at https://github.com/vercel/next.js/issues/new' + 'An unexpected Turbopack error occurred. Please see the output of `next dev` for more details.' ) decorateServerError(turbopackInternalError, type) return turbopackInternalError From aa290b40670acc3f48ebfbcc6f215b774d806718 Mon Sep 17 00:00:00 2001 From: Will Binns-Smith Date: Thu, 18 Jul 2024 17:10:42 -0700 Subject: [PATCH 2/2] fixup! Move turbopack panic log to tmpdir --- packages/next-swc/crates/napi/src/lib.rs | 5 ----- 1 file changed, 5 deletions(-) diff --git a/packages/next-swc/crates/napi/src/lib.rs b/packages/next-swc/crates/napi/src/lib.rs index d0852d5a42691..544b46100d23e 100644 --- a/packages/next-swc/crates/napi/src/lib.rs +++ b/packages/next-swc/crates/napi/src/lib.rs @@ -85,11 +85,6 @@ static PANIC_LOG: Lazy = Lazy::new(|| { path }); -#[napi] -pub fn get_panic_log_path() -> String { - PANIC_LOG.to_string_lossy().to_string() -} - #[cfg(feature = "__internal_dhat-heap")] #[global_allocator] static ALLOC: dhat::Alloc = dhat::Alloc;