Unverified Commit 73049d6f authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

Turbopack: Fix some eventually consistent reads at the top level in dev-server…

Turbopack: Fix some eventually consistent reads at the top level in dev-server and snapshot tests (#90792)

This fixes

```
UPDATE=1 cargo test -p turbopack-tests
```

when a snapshot test output is modified or deleted.  
  
We must get the `Effects` inside of an operation, as that involves reading `Vc`s, but then we should apply them outside of the operation (to guarantee that they only run once). The Next.js code follows a similar pattern here.
parent 3beb4665
...@@ -16,12 +16,9 @@ use tokio::task_local; ...@@ -16,12 +16,9 @@ use tokio::task_local;
use tracing::Instrument; use tracing::Instrument;
use crate::{ use crate::{
self as turbo_tasks, CollectiblesSource, NonLocalValue, ReadRef, ResolvedVc, TryJoinIterExt, self as turbo_tasks, CollectiblesSource, ReadRef, ResolvedVc, TryJoinIterExt, emit,
debug::ValueDebugFormat,
emit,
event::{Event, EventListener}, event::{Event, EventListener},
spawn, spawn,
trace::TraceRawVcs,
util::SharedError, util::SharedError,
}; };
...@@ -214,7 +211,8 @@ pub async fn get_effects(source: impl CollectiblesSource) -> Result<Effects> { ...@@ -214,7 +211,8 @@ pub async fn get_effects(source: impl CollectiblesSource) -> Result<Effects> {
/// Captured effects from an operation. This struct can be used to return Effects from a turbo-tasks /// Captured effects from an operation. This struct can be used to return Effects from a turbo-tasks
/// function and apply them later. /// function and apply them later.
#[derive(TraceRawVcs, Default, ValueDebugFormat, NonLocalValue)] #[derive(Default)]
#[turbo_tasks::value(shared, eq = "manual", serialization = "none")]
pub struct Effects { pub struct Effects {
#[turbo_tasks(trace_ignore, debug_ignore)] #[turbo_tasks(trace_ignore, debug_ignore)]
effects: Vec<ReadRef<EffectInstance>>, effects: Vec<ReadRef<EffectInstance>>,
......
...@@ -7,8 +7,9 @@ use serde::{Deserialize, Serialize}; ...@@ -7,8 +7,9 @@ use serde::{Deserialize, Serialize};
pub use turbo_tasks_macros::OperationValue; pub use turbo_tasks_macros::OperationValue;
use crate::{ use crate::{
CollectiblesSource, RawVc, ReadVcFuture, ResolvedVc, TaskInput, UpcastStrict, Vc, VcValueTrait, CollectiblesSource, IntoTraitRef, RawVc, ReadVcFuture, ResolvedVc, TaskInput, TraitRef,
VcValueType, marker_trait::impl_auto_marker_trait, trace::TraceRawVcs, UpcastStrict, Vc, VcValueTrait, VcValueType, marker_trait::impl_auto_marker_trait,
trace::TraceRawVcs,
}; };
/// A "subtype" (can be converted via [`.connect()`]) of [`Vc`] that /// A "subtype" (can be converted via [`.connect()`]) of [`Vc`] that
...@@ -140,6 +141,17 @@ impl<T: ?Sized> OperationVc<T> { ...@@ -140,6 +141,17 @@ impl<T: ?Sized> OperationVc<T> {
.strongly_consistent() .strongly_consistent()
.into() .into()
} }
/// [Connects the `OperationVc`][Self::connect] and returns a [strongly
/// consistent][crate::ReadConsistency::Strong] read of the value.
///
/// This ensures that all internal tasks are finished before the read is returned.
pub async fn read_trait_strongly_consistent(self) -> Result<TraitRef<T>>
where
T: VcValueTrait,
{
self.connect().into_trait_ref().strongly_consistent().await
}
} }
impl<T> Copy for OperationVc<T> where T: ?Sized {} impl<T> Copy for OperationVc<T> where T: ?Sized {}
......
...@@ -32,7 +32,7 @@ use socket2::{Domain, Protocol, Socket, Type}; ...@@ -32,7 +32,7 @@ use socket2::{Domain, Protocol, Socket, Type};
use tokio::task::JoinHandle; use tokio::task::JoinHandle;
use tracing::{Instrument, Level, Span, event, info_span}; use tracing::{Instrument, Level, Span, event, info_span};
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, OperationVc, PrettyPrintError, TurboTasksApi, Vc, apply_effects, Effects, NonLocalValue, OperationVc, PrettyPrintError, TurboTasksApi, Vc, get_effects,
run_once_with_reason, trace::TraceRawVcs, util::FormatDuration, run_once_with_reason, trace::TraceRawVcs, util::FormatDuration,
}; };
use turbopack_core::issue::{IssueReporter, IssueSeverity, handle_issues}; use turbopack_core::issue::{IssueReporter, IssueSeverity, handle_issues};
...@@ -57,6 +57,21 @@ where ...@@ -57,6 +57,21 @@ where
} }
} }
#[turbo_tasks::value(serialization = "none")]
struct ContentSourceWithIssues {
source_op: OperationVc<Box<dyn ContentSource>>,
effects: Effects,
}
#[turbo_tasks::function(operation)]
async fn get_source_with_issues_operation(
source_op: OperationVc<Box<dyn ContentSource>>,
) -> Result<Vc<ContentSourceWithIssues>> {
let _ = source_op.resolve_strongly_consistent().await?;
let effects = get_effects(source_op).await?;
Ok(ContentSourceWithIssues { source_op, effects }.cell())
}
#[derive(TraceRawVcs, Debug, NonLocalValue)] #[derive(TraceRawVcs, Debug, NonLocalValue)]
pub struct DevServerBuilder { pub struct DevServerBuilder {
#[turbo_tasks(trace_ignore)] #[turbo_tasks(trace_ignore)]
...@@ -208,12 +223,13 @@ impl DevServerBuilder { ...@@ -208,12 +223,13 @@ impl DevServerBuilder {
let uri = request.uri(); let uri = request.uri();
let path = uri.path().to_string(); let path = uri.path().to_string();
let source_op = source_provider.get_source(); let source_with_issues_op =
// HACK: Resolve `source` now so that we can get any issues on it get_source_with_issues_operation(source_provider.get_source());
let _ = source_op.resolve_strongly_consistent().await?; let ContentSourceWithIssues { source_op, effects } =
apply_effects(source_op).await?; &*source_with_issues_op.read_strongly_consistent().await?;
effects.apply().await?;
handle_issues( handle_issues(
source_op, source_with_issues_op,
issue_reporter, issue_reporter,
IssueSeverity::Fatal, IssueSeverity::Fatal,
Some(&path), Some(&path),
...@@ -229,7 +245,7 @@ impl DevServerBuilder { ...@@ -229,7 +245,7 @@ impl DevServerBuilder {
// It's unlikely (the calls happen one-after-another), but this // It's unlikely (the calls happen one-after-another), but this
// could cause inconsistency between the reported issues and // could cause inconsistency between the reported issues and
// the generated HTTP response. // the generated HTTP response.
source_op, *source_op,
request, request,
issue_reporter, issue_reporter,
) )
......
...@@ -11,7 +11,7 @@ use rustc_hash::FxHashSet; ...@@ -11,7 +11,7 @@ use rustc_hash::FxHashSet;
use serde::Deserialize; use serde::Deserialize;
use serde_json::json; use serde_json::json;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TurboTasks, ValueToString, Vc, apply_effects}; use turbo_tasks::{Effects, ResolvedVc, TurboTasks, ValueToString, Vc, get_effects};
use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage}; use turbo_tasks_backend::{BackendOptions, TurboTasksBackend, noop_backing_storage};
use turbo_tasks_env::DotenvProcessEnv; use turbo_tasks_env::DotenvProcessEnv;
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -222,30 +222,31 @@ async fn run(resource: PathBuf) -> Result<()> { ...@@ -222,30 +222,31 @@ async fn run(resource: PathBuf) -> Result<()> {
noop_backing_storage(), noop_backing_storage(),
)); ));
tt.run_once(async move { tt.run_once(async move {
let emit_op = run_inner_operation(resource.to_str().unwrap().into()); #[turbo_tasks::function(operation)]
emit_op.read_strongly_consistent().await?; async fn inner_operation(resource: RcStr) -> Result<Vc<Effects>> {
apply_effects(emit_op).await?; let out_op = run_test_operation(resource);
let out_vc = out_op.resolve_strongly_consistent().await?.owned().await?;
Ok(()) let plain_issues = out_op
}) .peek_issues()
.await?; .get_plain_issues(IssueFilter::everything())
.await?;
Ok(()) snapshot_issues(plain_issues, out_vc.join("issues")?, &REPO_ROOT)
} .await
.context("Unable to handle issues")?;
#[turbo_tasks::function(operation)]
async fn run_inner_operation(resource: RcStr) -> Result<()> {
let out_op = run_test_operation(resource);
let out_vc = out_op.resolve_strongly_consistent().await?.owned().await?;
let plain_issues = out_op Ok(get_effects(out_op).await?.cell())
.peek_issues() }
.get_plain_issues(IssueFilter::everything()) inner_operation(resource.to_str().unwrap().into())
.await?; .read_strongly_consistent()
.await?
.apply()
.await?;
snapshot_issues(plain_issues, out_vc.join("issues")?, &REPO_ROOT) Ok(())
.await })
.context("Unable to handle issues")?; .await?;
Ok(()) Ok(())
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment