Skip to content

Commit db70330

Browse files
authored
Move turbopack panic log to tmpdir (#67930)
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.
1 parent 2b45e95 commit db70330

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

crates/napi/src/lib.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ use std::{
3737
env,
3838
io::prelude::*,
3939
panic::set_hook,
40+
path::PathBuf,
4041
sync::{Arc, Mutex, Once},
4142
time::Instant,
4243
};
4344

4445
use backtrace::Backtrace;
4546
use fxhash::FxHashSet;
4647
use napi::bindgen_prelude::*;
48+
use once_cell::sync::Lazy;
4749
use owo_colors::OwoColorize;
4850
use swc_core::{
4951
base::{Compiler, TransformOutput},
@@ -76,7 +78,11 @@ shadow_rs::shadow!(build);
7678
static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
7779

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

8187
#[cfg(feature = "__internal_dhat-heap")]
8288
#[global_allocator]
@@ -104,15 +110,17 @@ fn init() {
104110
if cfg!(debug_assertions) || env::var("SWC_DEBUG") == Ok("1".to_string()) {
105111
eprintln!("{}", info);
106112
} else {
107-
let size = std::fs::metadata(LOG_FILE_PATH).map(|m| m.len());
113+
let size = std::fs::metadata(PANIC_LOG.as_path()).map(|m| m.len());
108114
if let Ok(size) = size {
109115
if size > 512 * 1024 {
110116
// Truncate the earliest error from log file if it's larger than 512KB
111117
let new_lines = {
112118
let log_read = OpenOptions::new()
113119
.read(true)
114-
.open(LOG_FILE_PATH)
115-
.unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH));
120+
.open(PANIC_LOG.as_path())
121+
.unwrap_or_else(|_| {
122+
panic!("Failed to open {}", PANIC_LOG.to_string_lossy())
123+
});
116124

117125
io::BufReader::new(&log_read)
118126
.lines()
@@ -128,8 +136,10 @@ fn init() {
128136
.create(true)
129137
.truncate(true)
130138
.write(true)
131-
.open(LOG_FILE_PATH)
132-
.unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH));
139+
.open(PANIC_LOG.as_path())
140+
.unwrap_or_else(|_| {
141+
panic!("Failed to open {}", PANIC_LOG.to_string_lossy())
142+
});
133143

134144
for line in new_lines {
135145
match line {
@@ -147,11 +157,11 @@ fn init() {
147157
let mut log_file = OpenOptions::new()
148158
.create(true)
149159
.append(true)
150-
.open(LOG_FILE_PATH)
151-
.unwrap_or_else(|_| panic!("Failed to open {}", LOG_FILE_PATH));
160+
.open(PANIC_LOG.as_path())
161+
.unwrap_or_else(|_| panic!("Failed to open {}", PANIC_LOG.to_string_lossy()));
152162

153163
writeln!(log_file, "{}", info).unwrap();
154-
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);
164+
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());
155165
}
156166
}));
157167
}

packages/next/src/client/components/react-dev-overlay/internal/helpers/nodeStackFrames.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export function getServerError(error: Error, type: ErrorSourceType): Error {
2929
// If this is an internal Turbopack error we shouldn't show internal details
3030
// to the user. These are written to a log file instead.
3131
const turbopackInternalError = new Error(
32-
'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'
32+
'An unexpected Turbopack error occurred. Please see the output of `next dev` for more details.'
3333
)
3434
decorateServerError(turbopackInternalError, type)
3535
return turbopackInternalError

0 commit comments

Comments
 (0)