Skip to content

Commit a6bbaac

Browse files
committed
Update the messaging UX on messaging for timing writing files to disk
Additional Changes: * Changed severity of "Starting the compilation events sever" to `info` to match other messaging like it * Began printing all compilation events now that it has been proven to work correctly * Added a `TimingEvent` for these types of messages - Made the messaging follow the rules for timing information from how Next.js formats timing info
1 parent f8bbb5e commit a6bbaac

File tree

3 files changed

+79
-17
lines changed

3 files changed

+79
-17
lines changed

crates/napi/src/next_api/project.rs

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ use turbo_rcstr::RcStr;
3131
use turbo_tasks::{
3232
Completion, Effects, FxIndexSet, OperationVc, ReadRef, ResolvedVc, TransientInstance,
3333
TryJoinIterExt, UpdateInfo, Vc, get_effects,
34-
message_queue::{CompilationEvent, DiagnosticEvent, Severity},
34+
message_queue::{CompilationEvent, DiagnosticEvent, Severity, TimingEvent},
3535
};
3636
use turbo_tasks_fs::{
3737
DiskFileSystem, FileContent, FileSystem, FileSystemPath, get_relative_path_to,
@@ -405,8 +405,8 @@ pub async fn project_new(
405405
)?;
406406

407407
turbo_tasks.send_compilation_event(Arc::new(DiagnosticEvent::new(
408-
"Starting the compilation events server...".to_owned(),
409-
Severity::Trace,
408+
Severity::Info,
409+
"Starting the compilation events server ...".to_owned(),
410410
)));
411411

412412
let stats_path = std::env::var_os("NEXT_TURBOPACK_TASK_STATISTICS");
@@ -850,21 +850,14 @@ pub async fn project_write_all_entrypoints_to_disk(
850850

851851
// Start timing writing the files to disk
852852
let now = Instant::now();
853-
compilation_event_sender.send_compilation_event(Arc::new(DiagnosticEvent::new(
854-
"Starting to write all entrypoints to disk...".to_owned(),
855-
Severity::Event,
856-
)));
857853

858854
// Write the files to disk
859855
effects.apply().await?;
860856

861857
// Send a compilation event to indicate that the files have been written to disk
862-
compilation_event_sender.send_compilation_event(Arc::new(DiagnosticEvent::new(
863-
format!(
864-
"Finished writing all entrypoints to disk in {:?}",
865-
now.elapsed()
866-
),
867-
Severity::Event,
858+
compilation_event_sender.send_compilation_event(Arc::new(TimingEvent::new(
859+
"Finished writing all entrypoints to disk".to_owned(),
860+
now.elapsed(),
868861
)));
869862

870863
Ok((entrypoints.clone(), issues.clone(), diagnostics.clone()))

packages/next/src/build/turbopack-build/impl.ts

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,27 @@ export async function turbopackBuild(): Promise<{
9191
try {
9292
;(async function logCompilationEvents() {
9393
for await (const event of project.compilationEventsSubscribe()) {
94-
if (event.severity === 'EVENT') {
95-
Log.event(event.message)
94+
switch (event.severity) {
95+
case 'EVENT':
96+
Log.event(event.message)
97+
break
98+
case 'TRACE':
99+
Log.trace(event.message)
100+
break
101+
case 'INFO':
102+
Log.info(event.message)
103+
break
104+
case 'WARNING':
105+
console.warn(event.message)
106+
break
107+
case 'ERROR':
108+
Log.error(event.message)
109+
break
110+
case 'FATAL':
111+
Log.error(event.message)
112+
break
113+
default:
114+
break
96115
}
97116
}
98117
})()

turbopack/crates/turbo-tasks/src/message_queue.rs

Lines changed: 52 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{any::Any, collections::VecDeque, fmt::Display, sync::Arc};
1+
use std::{any::Any, collections::VecDeque, fmt::Display, sync::Arc, time::Duration};
22

33
use dashmap::DashMap;
44
use tokio::sync::{Mutex, mpsc};
@@ -141,14 +141,64 @@ impl Display for Severity {
141141
}
142142
}
143143

144+
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
145+
/// Compilation event that is used to log the duration of a task
146+
pub struct TimingEvent {
147+
/// Message of the event without the timing information
148+
///
149+
/// Example:
150+
/// ```rust
151+
/// let event = TimingEvent::new("Compiled successfully".to_string(), Duration::from_millis(100));
152+
/// let message = event.message();
153+
/// assert_eq!(message, "Compiled successfully in 100ms");
154+
/// ```
155+
pub message: String,
156+
/// Duration in milliseconds
157+
pub duration: Duration,
158+
}
159+
160+
impl TimingEvent {
161+
pub fn new(message: String, duration: Duration) -> Self {
162+
Self { message, duration }
163+
}
164+
}
165+
166+
impl CompilationEvent for TimingEvent {
167+
fn type_name(&self) -> &'static str {
168+
"TimingEvent"
169+
}
170+
171+
fn severity(&self) -> Severity {
172+
Severity::Event
173+
}
174+
175+
fn message(&self) -> String {
176+
let duration_secs = self.duration.as_secs_f64();
177+
let duration_string = if duration_secs > 120.0 {
178+
format!("{:.1}min", duration_secs / 60.0)
179+
} else if duration_secs > 40.0 {
180+
format!("{:.0}s", duration_secs)
181+
} else if duration_secs > 2.0 {
182+
format!("{:.1}s", duration_secs)
183+
} else {
184+
format!("{:.0}ms", duration_secs * 1000.0)
185+
};
186+
format!("{} in {}", self.message, duration_string)
187+
}
188+
189+
fn to_json(&self) -> String {
190+
serde_json::to_string(self).unwrap()
191+
}
192+
}
193+
144194
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
145195
pub struct DiagnosticEvent {
146196
pub message: String,
147197
pub severity: Severity,
148198
}
149199

150200
impl DiagnosticEvent {
151-
pub fn new(message: String, severity: Severity) -> Self {
201+
pub fn new(severity: Severity, message: String) -> Self {
152202
Self { message, severity }
153203
}
154204
}

0 commit comments

Comments
 (0)