-
Notifications
You must be signed in to change notification settings - Fork 29.5k
Turbopack: allow gzip compression on trace files #84685
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
base: sokra/update-deps
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
use std::{borrow::Cow, io::Write, path::PathBuf, sync::Arc, thread, time::Duration}; | ||
|
||
use anyhow::{Context, Result, anyhow, bail}; | ||
use flate2::write::GzEncoder; | ||
use futures_util::TryFutureExt; | ||
use napi::{ | ||
Env, JsFunction, JsObject, Status, | ||
|
@@ -363,21 +364,33 @@ pub fn project_new( | |
} | ||
|
||
if let Some(mut trace) = trace { | ||
// Trace presets | ||
match trace.as_str() { | ||
"overview" | "1" => { | ||
trace = TRACING_NEXT_OVERVIEW_TARGETS.join(","); | ||
} | ||
"next" => { | ||
trace = TRACING_NEXT_TARGETS.join(","); | ||
} | ||
"turbopack" => { | ||
trace = TRACING_NEXT_TURBOPACK_TARGETS.join(","); | ||
} | ||
"turbo-tasks" => { | ||
trace = TRACING_NEXT_TURBO_TASKS_TARGETS.join(","); | ||
} | ||
_ => {} | ||
trace = trace | ||
.split(",") | ||
.map(|item| { | ||
// Trace presets | ||
match item { | ||
"overview" | "1" => Cow::Owned(TRACING_NEXT_OVERVIEW_TARGETS.join(",")), | ||
"next" => Cow::Owned(TRACING_NEXT_TARGETS.join(",")), | ||
"turbopack" => Cow::Owned(TRACING_NEXT_TURBOPACK_TARGETS.join(",")), | ||
"turbo-tasks" => Cow::Owned(TRACING_NEXT_TURBO_TASKS_TARGETS.join(",")), | ||
_ => Cow::Borrowed(item), | ||
} | ||
}) | ||
.intersperse_with(|| Cow::Borrowed(",")) | ||
.collect::<String>(); | ||
|
||
enum Compression { | ||
None, | ||
GzipFast, | ||
GzipBest, | ||
} | ||
let mut compress = Compression::None; | ||
if trace.ends_with(",gz") { | ||
trace.drain(trace.len() - 3..); | ||
compress = Compression::GzipFast; | ||
} else if trace.ends_with(",gz-best") { | ||
trace.drain(trace.len() - 8..); | ||
compress = Compression::GzipBest; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should we just compress by default? what is the tradeoff we are asking users to make? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the compression uses some extra cpu time, so it skews the results of tracing There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. would it be better then to just compress the file at the end of the process after closing it? |
||
} | ||
|
||
let subscriber = Registry::default(); | ||
|
@@ -396,9 +409,26 @@ pub fn project_new( | |
std::fs::create_dir_all(&internal_dir) | ||
.context("Unable to create .next directory") | ||
.unwrap(); | ||
let trace_file = internal_dir.join("trace-turbopack"); | ||
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap(); | ||
let (trace_writer, trace_writer_guard) = TraceWriter::new(trace_writer); | ||
let trace_file; | ||
let (trace_writer, trace_writer_guard) = match compress { | ||
Compression::None => { | ||
trace_file = internal_dir.join("trace-turbopack"); | ||
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap(); | ||
TraceWriter::new(trace_writer) | ||
} | ||
Compression::GzipFast => { | ||
trace_file = internal_dir.join("trace-turbopack"); | ||
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap(); | ||
let trace_writer = GzEncoder::new(trace_writer, flate2::Compression::fast()); | ||
TraceWriter::new(trace_writer) | ||
} | ||
Compression::GzipBest => { | ||
trace_file = internal_dir.join("trace-turbopack"); | ||
let trace_writer = std::fs::File::create(trace_file.clone()).unwrap(); | ||
let trace_writer = GzEncoder::new(trace_writer, flate2::Compression::best()); | ||
TraceWriter::new(trace_writer) | ||
} | ||
}; | ||
let subscriber = subscriber.with(RawTraceLayer::new(trace_writer)); | ||
|
||
exit.on_exit(async move { | ||
|
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -75,7 +75,7 @@ impl ObjectSafeTraceFormat for ErasedTraceFormat { | |||||
|
||||||
#[derive(Default)] | ||||||
enum TraceFile { | ||||||
Raw(File), | ||||||
Raw(BufReader<File>), | ||||||
Zstd(zstd::Decoder<'static, BufReader<File>>), | ||||||
Gz(GzDecoder<BufReader<File>>), | ||||||
#[default] | ||||||
|
@@ -112,7 +112,7 @@ impl TraceFile { | |||||
|
||||||
fn size(&mut self) -> io::Result<u64> { | ||||||
match self { | ||||||
Self::Raw(file) => file.metadata().map(|m| m.len()), | ||||||
Self::Raw(file) => file.get_ref().metadata().map(|m| m.len()), | ||||||
Self::Zstd(decoder) => decoder.get_mut().get_ref().metadata().map(|m| m.len()), | ||||||
Self::Gz(decoder) => decoder.get_mut().get_ref().metadata().map(|m| m.len()), | ||||||
Self::Unloaded => unreachable!(), | ||||||
|
@@ -145,13 +145,21 @@ impl TraceReader { | |||||
|
||||||
fn trace_file_from_file(&self, file: File) -> io::Result<TraceFile> { | ||||||
let path = &self.path.to_string_lossy(); | ||||||
Ok(if path.ends_with(".zst") { | ||||||
TraceFile::Zstd(zstd::Decoder::new(file)?) | ||||||
} else if path.ends_with(".gz") { | ||||||
TraceFile::Gz(GzDecoder::new(BufReader::new(file))) | ||||||
} else { | ||||||
TraceFile::Raw(file) | ||||||
}) | ||||||
let mut file = BufReader::with_capacity( | ||||||
// zstd max block size (1 << 17) + block header (3) + magic bytes (4) | ||||||
(1 << 17) + 7, | ||||||
file, | ||||||
); | ||||||
let magic_bytes = file.peek(4)?; | ||||||
Ok( | ||||||
if path.ends_with(".zst") || magic_bytes == [0x27, 0xb5, 0x2f, 0xfd] { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Incorrect zstd magic byte in compression auto-detection: the first byte should be View DetailsAnalysisIncorrect zstd magic byte prevents auto-detection of compressed trace filesWhat fails: How to reproduce: # Create a zstd-compressed file without .zst extension
echo "test data" | zstd > trace-turbopack
hexdump -C trace-turbopack | head -1
# Shows: 00000000 28 b5 2f fd ...
# The code will fail to detect this as zstd because:
# - Path check fails (no .zst extension)
# - Magic byte check fails (0x27 != 0x28) Result: File is treated as raw/uncompressed, causing decompression to fail. Files without extensions (like Expected: According to RFC 8878 Section 3.1.1, zstd magic number is |
||||||
TraceFile::Zstd(zstd::Decoder::with_buffer(file)?) | ||||||
} else if path.ends_with(".gz") || matches!(magic_bytes, [0x1f, 0x8b, _, _]) { | ||||||
TraceFile::Gz(GzDecoder::new(file)) | ||||||
} else { | ||||||
TraceFile::Raw(file) | ||||||
}, | ||||||
) | ||||||
} | ||||||
|
||||||
fn try_read(&mut self) -> bool { | ||||||
|
@@ -293,7 +301,9 @@ impl TraceReader { | |||||
} | ||||||
} | ||||||
Err(err) => { | ||||||
if err.kind() == io::ErrorKind::UnexpectedEof { | ||||||
if err.kind() == io::ErrorKind::UnexpectedEof | ||||||
|| err.kind() == io::ErrorKind::InvalidInput | ||||||
{ | ||||||
if let Some(value) = self.wait_for_more_data( | ||||||
&mut file, | ||||||
&mut initial_read, | ||||||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Compression flags (
gz
orgz-best
) are only detected when they appear at the very end of the trace string. If specified alone or in the middle of multiple presets, they are silently ignored and incorrectly treated as tracing filter directives.View Details
📝 Patch Details
Analysis
Compression flags silently ignored unless at end of trace string
What fails: In
crates/napi/src/next_api/project.rs
, compression flags (gz
orgz-best
) in theNEXT_TURBOPACK_TRACING
environment variable are only detected when appearing at the very end of the trace string, after all preset expansion.How to reproduce:
Result: When compression flags appear standalone or in the middle, the code uses
ends_with(",gz")
check which fails, so compression is not enabled and the flag is passed to the tracing filter layer as an invalid target directive (no error shown).Expected: Compression flags should be detected regardless of position in the comma-separated list, per the logic used for other preset flags like "overview" and "turbopack".
Root cause: The code expands presets and joins them with commas using
intersperse_with
(lines 367-381), then checkstrace.ends_with(",gz")
(lines 388-393). This only works ifgz
is literally at the end after joining.