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

Move turbopack panic log to tmpdir #67930

Merged
merged 3 commits into from
Aug 5, 2024
Merged
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
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 turbopack_binding::swc::core::{
base::{Compiler, TransformOutput},
Expand Down Expand Up @@ -77,7 +79,11 @@ static ALLOC: turbopack_binding::turbo::malloc::TurboMalloc =
turbopack_binding::turbo::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 @@ -105,15 +111,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 @@ -129,8 +137,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 @@ -148,11 +158,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
Loading