Skip to content

Commit

Permalink
Move turbopack panic log to tmpdir (#67930)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
wbinnssmith authored and ForsakenHarmony committed Aug 16, 2024
1 parent f2f2b32 commit 50d884b
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 10 deletions.
28 changes: 19 additions & 9 deletions crates/napi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,15 @@ use std::{
env,
io::prelude::*,
panic::set_hook,
path::PathBuf,
sync::{Arc, Mutex, Once},
time::Instant,
};

use backtrace::Backtrace;
use fxhash::FxHashSet;
use napi::bindgen_prelude::*;
use once_cell::sync::Lazy;
use owo_colors::OwoColorize;
use swc_core::{
base::{Compiler, TransformOutput},
Expand Down Expand Up @@ -76,7 +78,11 @@ shadow_rs::shadow!(build);
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;

static LOG_THROTTLE: Mutex<Option<Instant>> = Mutex::new(None);
static LOG_FILE_PATH: &str = ".next/turbopack.log";
static PANIC_LOG: Lazy<PathBuf> = Lazy::new(|| {
let mut path = env::temp_dir();
path.push(format!("next-panic-{:x}.log", rand::random::<u128>()));
path
});

#[cfg(feature = "__internal_dhat-heap")]
#[global_allocator]
Expand Down Expand Up @@ -104,15 +110,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()
Expand All @@ -128,8 +136,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 {
Expand All @@ -147,11 +157,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());
}
}));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 50d884b

Please sign in to comment.