Skip to content
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
4 changes: 2 additions & 2 deletions crates/next-napi-bindings/src/next_api/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ use tracing_subscriber::{Registry, layer::SubscriberExt, util::SubscriberInitExt
use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{
Effects, FxIndexSet, NonLocalValue, OperationValue, OperationVc, PrettyPrintError, ReadRef,
ResolvedVc, TaskInput, TransientInstance, TryJoinIterExt, TurboTasksApi, UpdateInfo, Vc,
get_effects,
ResolvedVc, TaskInput, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi,
UpdateInfo, Vc, get_effects,
message_queue::{CompilationEvent, Severity},
trace::TraceRawVcs,
};
Expand Down
2 changes: 1 addition & 1 deletion crates/next-napi-bindings/src/next_api/turbopack_ctx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use owo_colors::OwoColorize;
use serde::Serialize;
use terminal_hyperlink::Hyperlink;
use turbo_tasks::{
PrettyPrintError, TurboTasks, TurboTasksApi,
PrettyPrintError, TurboTasks, TurboTasksCallApi,
backend::TurboTasksExecutionError,
message_queue::{CompilationEvent, Severity},
};
Expand Down
118 changes: 98 additions & 20 deletions turbopack/crates/turbo-tasks-backend/src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,19 @@ use turbo_bincode::{TurboBincodeBuffer, new_turbo_bincode_decoder, new_turbo_bin
use turbo_tasks::{
CellId, FxDashMap, RawVc, ReadCellOptions, ReadCellTracking, ReadConsistency,
ReadOutputOptions, ReadTracking, SharedReference, TRANSIENT_TASK_BIT, TaskExecutionReason,
TaskId, TaskPriority, TraitTypeId, TurboTasksBackendApi, ValueTypeId,
TaskId, TaskPriority, TraitTypeId, TurboTasksBackendApi, TurboTasksPanic, ValueTypeId,
backend::{
Backend, CachedTaskType, CellContent, TaskExecutionSpec, TransientTaskType,
TurboTasksExecutionError, TypedCellContent, VerificationMode,
TurboTaskContextError, TurboTaskLocalContextError, TurboTasksError,
TurboTasksExecutionError, TurboTasksExecutionErrorMessage, TypedCellContent,
VerificationMode,
},
event::{Event, EventDescription, EventListener},
message_queue::TimingEvent,
registry::get_value_type,
scope::scope_and_block,
task_statistics::TaskStatisticsApi,
trace::TraceRawVcs,
turbo_tasks,
util::{IdFactoryWithReuse, good_chunk_size, into_chunks},
};

Expand All @@ -66,6 +67,7 @@ use crate::{
ActivenessState, CellRef, CollectibleRef, CollectiblesRef, Dirtyness, InProgressCellState,
InProgressState, InProgressStateInner, OutputValue, TransientTask,
},
error::TaskError,
utils::{
arc_or_owned::ArcOrOwned,
chunked_vec::ChunkedVec,
Expand Down Expand Up @@ -387,6 +389,69 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
self.task_statistics
.map(|stats| stats.increment_cache_miss(task_type.native_fn));
}

/// Reconstructs a full [`TurboTasksExecutionError`] from the compact [`TaskError`] storage
/// representation. For [`TaskError::TaskChain`], this looks up the source error from the last
/// task's output and rebuilds the nested `TaskContext` wrappers with `TurboTasksCallApi`
/// references for lazy name resolution.
fn task_error_to_turbo_tasks_execution_error(
&self,
error: &TaskError,
ctx: &mut impl ExecuteContext<'_>,
) -> TurboTasksExecutionError {
match error {
TaskError::Panic(panic) => TurboTasksExecutionError::Panic(panic.clone()),
TaskError::Error(item) => TurboTasksExecutionError::Error(Arc::new(TurboTasksError {
message: item.message.clone(),
source: item
.source
.as_ref()
.map(|e| self.task_error_to_turbo_tasks_execution_error(e, ctx)),
})),
TaskError::LocalTaskContext(local_task_context) => {
TurboTasksExecutionError::LocalTaskContext(Arc::new(TurboTaskLocalContextError {
name: local_task_context.name.clone(),
source: local_task_context
.source
.as_ref()
.map(|e| self.task_error_to_turbo_tasks_execution_error(e, ctx)),
}))
}
TaskError::TaskChain(chain) => {
let task_id = chain.last().unwrap();
let error = {
let task = ctx.task(*task_id, TaskDataCategory::Meta);
if let Some(OutputValue::Error(error)) = task.get_output() {
Some(error.clone())
} else {
None
}
};
let error = error.map_or_else(
|| {
// Eventual consistency will cause errors to no longer be available
TurboTasksExecutionError::Panic(Arc::new(TurboTasksPanic {
Comment thread
sokra marked this conversation as resolved.
message: TurboTasksExecutionErrorMessage::PIISafe(Cow::Borrowed(
"Error no longer available",
)),
location: None,
}))
},
|e| self.task_error_to_turbo_tasks_execution_error(&e, ctx),
);
let mut current_error = error;
for &task_id in chain.iter().rev() {
current_error =
TurboTasksExecutionError::TaskContext(Arc::new(TurboTaskContextError {
task_id,
source: Some(current_error),
turbo_tasks: ctx.turbo_tasks(),
}));
}
current_error
}
}
}
}

pub(crate) struct OperationGuard<'a, B: BackingStorage> {
Expand Down Expand Up @@ -715,11 +780,9 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let result = match output {
OutputValue::Cell(cell) => Ok(Ok(RawVc::TaskCell(cell.task, cell.cell))),
OutputValue::Output(task) => Ok(Ok(RawVc::TaskOutput(*task))),
OutputValue::Error(error) => Err(error
.with_task_context(task.get_task_type(), Some(task_id))
.into()),
OutputValue::Error(error) => Err(error.clone()),
};
if let Some(mut reader_task) = reader_task
if let Some(mut reader_task) = reader_task.take()
&& options.tracking.should_track(result.is_err())
&& (!task.immutable() || cfg!(feature = "verify_immutable"))
{
Expand Down Expand Up @@ -759,9 +822,15 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
drop(reader_task);

queue.execute(&mut ctx);
} else {
drop(task);
}

return result;
return result.map_err(|error| {
self.task_error_to_turbo_tasks_execution_error(&error, &mut ctx)
.with_task_context(task_id, turbo_tasks.pin())
.into()
});
}
drop(reader_task);

Expand Down Expand Up @@ -962,6 +1031,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
&self,
parent_span: Option<tracing::Id>,
reason: &str,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) -> Option<(Instant, bool)> {
let snapshot_span =
tracing::trace_span!(parent: parent_span.clone(), "snapshot", reason = reason)
Expand Down Expand Up @@ -1087,7 +1157,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
if let Some(ref m) = meta {
task_cache_stats
.lock()
.entry(self.debug_get_task_name(task_id))
.entry(self.get_task_name(task_id, turbo_tasks))
.or_default()
.add_counts(m);
}
Expand All @@ -1107,7 +1177,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
if inner.flags.meta_modified() {
task_cache_stats
.lock()
.entry(self.debug_get_task_name(task_id))
.entry(self.get_task_name(task_id, turbo_tasks))
.or_default()
.add_counts(&inner);
}
Expand Down Expand Up @@ -1143,7 +1213,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
#[cfg(feature = "print_cache_item_size")]
task_cache_stats
.lock()
.entry(self.debug_get_task_name(task_id))
.entry(self.get_task_name(task_id, turbo_tasks))
.or_default()
.add_meta(&meta);
Some(meta)
Expand All @@ -1163,7 +1233,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
#[cfg(feature = "print_cache_item_size")]
task_cache_stats
.lock()
.entry(self.debug_get_task_name(task_id))
.entry(self.get_task_name(task_id, turbo_tasks))
.or_default()
.add_data(&data);
Some(data)
Expand Down Expand Up @@ -1317,7 +1387,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
let elapsed = start.elapsed();
// avoid spamming the event queue with information about fast operations
if elapsed > Duration::from_secs(10) {
turbo_tasks().send_compilation_event(Arc::new(TimingEvent::new(
turbo_tasks.send_compilation_event(Arc::new(TimingEvent::new(
"Finished writing to filesystem cache".to_string(),
elapsed,
)));
Expand Down Expand Up @@ -1366,7 +1436,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
self.verify_aggregation_graph(turbo_tasks, false);
}
if self.should_persist() {
self.snapshot_and_persist(Span::current().into(), "stop");
self.snapshot_and_persist(Span::current().into(), "stop", turbo_tasks);
}
drop_contents(&self.task_cache);
self.storage.drop_contents();
Expand Down Expand Up @@ -1718,9 +1788,13 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
}

#[allow(dead_code)]
fn debug_get_task_name(&self, task_id: TaskId) -> String {
let task = self.storage.access_mut(task_id);
fn get_task_name(
&self,
task_id: TaskId,
turbo_tasks: &dyn TurboTasksBackendApi<TurboTasksBackend<B>>,
) -> String {
let mut ctx = self.execute_context(turbo_tasks);
let task = ctx.task(task_id, TaskDataCategory::Data);
if let Some(value) = task.get_persistent_task_type() {
value.to_string()
} else if let Some(value) = task.get_transient_task_type() {
Expand Down Expand Up @@ -2180,11 +2254,11 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}
Err(err) => {
if let Some(OutputValue::Error(old_error)) = current_output
&& old_error == &err
&& **old_error == err
{
None
} else {
Some(OutputValue::Error(err))
Some(OutputValue::Error(Arc::new((&err).into())))
}
}
};
Expand Down Expand Up @@ -2715,7 +2789,7 @@ impl<B: BackingStorage> TurboTasksBackendInner<B> {
}

let this = self.clone();
let snapshot = this.snapshot_and_persist(None, reason);
let snapshot = this.snapshot_and_persist(None, reason, turbo_tasks);
if let Some((snapshot_start, new_data)) = snapshot {
last_snapshot = snapshot_start;
if !new_data {
Expand Down Expand Up @@ -3527,6 +3601,10 @@ impl<B: BackingStorage> Backend for TurboTasksBackend<B> {
fn is_tracking_dependencies(&self) -> bool {
self.0.options.dependency_tracking
}

fn get_task_name(&self, task: TaskId, turbo_tasks: &dyn TurboTasksBackendApi<Self>) -> String {
self.0.get_task_name(task, turbo_tasks)
}
}

enum DebugTraceTransientTask {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::{
use bincode::{Decode, Encode};
use turbo_tasks::{
CellId, FxIndexMap, TaskExecutionReason, TaskId, TaskPriority, TurboTasksBackendApi,
TypedSharedReference, backend::CachedTaskType,
TurboTasksCallApi, TypedSharedReference, backend::CachedTaskType,
};

use crate::{
Expand Down Expand Up @@ -93,6 +93,7 @@ pub trait ExecuteContext<'e>: Sized {
fn suspending_requested(&self) -> bool;
fn should_track_dependencies(&self) -> bool;
fn should_track_activeness(&self) -> bool;
fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi>;
}

pub trait ChildExecuteContext<'e>: Send + Sized {
Expand Down Expand Up @@ -637,6 +638,10 @@ where
fn should_track_activeness(&self) -> bool {
self.backend.should_track_activeness()
}

fn turbo_tasks(&self) -> Arc<dyn TurboTasksCallApi> {
self.turbo_tasks.pin()
}
}

struct ChildExecuteContextImpl<'e, B: BackingStorage> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,7 +1010,7 @@ mod tests {
fn test_schema_size() {
assert_eq!(
size_of::<TaskStorage>(),
144,
136,
"TaskStorage size changed! If this is intentional, update this test."
);
assert_eq!(
Expand Down
8 changes: 6 additions & 2 deletions turbopack/crates/turbo-tasks-backend/src/data.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::{
fmt::{self, Debug, Display},
pin::Pin,
sync::Arc,
};

use anyhow::Result;
Expand All @@ -9,10 +10,12 @@ use parking_lot::Mutex;
use rustc_hash::FxHashSet;
use turbo_tasks::{
CellId, RawVc, TaskExecutionReason, TaskId, TaskPriority, TraitTypeId,
backend::{TransientTaskRoot, TurboTasksExecutionError},
backend::TransientTaskRoot,
event::{Event, EventDescription, EventListener},
};

use crate::error::TaskError;

// this traits are needed for the transient variants of `CachedDataItem`
// transient variants are never cloned or compared
macro_rules! transient_traits {
Expand Down Expand Up @@ -78,8 +81,9 @@ impl CollectiblesRef {
pub enum OutputValue {
Cell(CellRef),
Output(TaskId),
Error(TurboTasksExecutionError),
Error(Arc<TaskError>),
}

impl OutputValue {
/// Returns true if this output value references a transient task.
///
Expand Down
Loading
Loading