Skip to content

Commit df6cd59

Browse files
authored
Turbopack: Remove turbo_tasks::apply_effects, use Effects::apply instead (#91858)
Having both `apply_effects` and `Effects::apply` is confusing. `apply_effect`'s API seems a bit bad/dangerous because it combines work that should be done inside of a strongly-consistent operation (collecting the effects and reading their `Vc`s) with work that should be done at the top-level outside of any operation or turbo-task function (applying the effects). The first commit in this PR removes the `apply_effect` API and tries to improve documentation, including adding a doctest that compiles. The second commit in this PR is an LLM-driven refactor to remove the `apply_effect` callsites.
1 parent b6ff360 commit df6cd59

19 files changed

Lines changed: 454 additions & 245 deletions

File tree

crates/next-api/src/operation.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use bincode::{Decode, Encode};
33
use turbo_rcstr::RcStr;
44
use turbo_tasks::{
55
CollectiblesSource, FxIndexMap, NonLocalValue, OperationValue, OperationVc, ResolvedVc,
6-
TaskInput, Vc, debug::ValueDebugFormat, get_effects, trace::TraceRawVcs,
6+
TaskInput, Vc, debug::ValueDebugFormat, take_effects, trace::TraceRawVcs,
77
};
88
use turbopack_core::{diagnostics::Diagnostic, issue::CollectibleIssuesExt};
99

@@ -40,7 +40,7 @@ async fn entrypoints_without_collectibles_operation(
4040
let _ = entrypoints.resolve().strongly_consistent().await?;
4141
entrypoints.drop_collectibles::<Box<dyn Diagnostic>>();
4242
entrypoints.drop_issues();
43-
let _ = get_effects(entrypoints).await?;
43+
let _ = take_effects(entrypoints).await?;
4444
Ok(entrypoints.connect())
4545
}
4646

crates/next-build-test/src/lib.rs

Lines changed: 42 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use next_api::{
1212
route::{Endpoint, EndpointOutputPaths, Route, endpoint_write_to_disk},
1313
};
1414
use turbo_rcstr::{RcStr, rcstr};
15-
use turbo_tasks::{ReadConsistency, ResolvedVc, TransientInstance, TurboTasks, Vc, get_effects};
15+
use turbo_tasks::{
16+
Effects, ReadConsistency, ReadRef, ResolvedVc, TransientInstance, TurboTasks, Vc, take_effects,
17+
};
1618
use turbo_tasks_backend::{NoopBackingStorage, TurboTasksBackend};
1719
use turbo_tasks_malloc::TurboMalloc;
1820

@@ -186,21 +188,21 @@ pub async fn render_routes(
186188
html_endpoint,
187189
data_endpoint: _,
188190
} => {
189-
endpoint_write_to_disk_with_effects(*html_endpoint).await?;
191+
endpoint_write_to_disk_with_apply(html_endpoint).await?;
190192
}
191193
Route::PageApi { endpoint } => {
192-
endpoint_write_to_disk_with_effects(*endpoint).await?;
194+
endpoint_write_to_disk_with_apply(endpoint).await?;
193195
}
194196
Route::AppPage(routes) => {
195197
for route in routes {
196-
endpoint_write_to_disk_with_effects(*route.html_endpoint).await?;
198+
endpoint_write_to_disk_with_apply(route.html_endpoint).await?;
197199
}
198200
}
199201
Route::AppRoute {
200202
original_name: _,
201203
endpoint,
202204
} => {
203-
endpoint_write_to_disk_with_effects(*endpoint).await?;
205+
endpoint_write_to_disk_with_apply(endpoint).await?;
204206
}
205207
Route::Conflict => {
206208
tracing::info!("WARN: conflict {}", name);
@@ -243,21 +245,43 @@ pub async fn render_routes(
243245
Ok(stream.len())
244246
}
245247

246-
#[turbo_tasks::function]
247-
async fn endpoint_write_to_disk_with_effects(
248+
async fn endpoint_write_to_disk_with_apply(
248249
endpoint: ResolvedVc<Box<dyn Endpoint>>,
249-
) -> Result<Vc<EndpointOutputPaths>> {
250-
let op = endpoint_write_to_disk_operation(endpoint);
251-
let result = op.resolve().strongly_consistent().await?;
252-
get_effects(op).await?.apply().await?;
253-
Ok(*result)
254-
}
250+
) -> Result<ReadRef<EndpointOutputPaths>> {
251+
#[turbo_tasks::function(operation)]
252+
fn inner_operation(endpoint: ResolvedVc<Box<dyn Endpoint>>) -> Vc<EndpointOutputPaths> {
253+
// we must wrap this in an operation so we can get the Effects collectibles
254+
endpoint_write_to_disk(*endpoint)
255+
}
255256

256-
#[turbo_tasks::function(operation)]
257-
pub fn endpoint_write_to_disk_operation(
258-
endpoint: ResolvedVc<Box<dyn Endpoint>>,
259-
) -> Vc<EndpointOutputPaths> {
260-
endpoint_write_to_disk(*endpoint)
257+
#[turbo_tasks::value(serialization = "none")]
258+
struct WithEffects {
259+
output_paths: ReadRef<EndpointOutputPaths>,
260+
effects: Effects,
261+
}
262+
263+
#[turbo_tasks::function(operation)]
264+
pub async fn inner_operation_with_effects(
265+
endpoint: ResolvedVc<Box<dyn Endpoint>>,
266+
) -> Result<Vc<WithEffects>> {
267+
let op = inner_operation(endpoint);
268+
let output_paths = op.read_strongly_consistent().await?;
269+
let effects = take_effects(op).await?;
270+
Ok(WithEffects {
271+
output_paths,
272+
effects,
273+
}
274+
.cell())
275+
}
276+
277+
let op = inner_operation_with_effects(endpoint);
278+
let WithEffects {
279+
output_paths,
280+
effects,
281+
} = &*op.read_strongly_consistent().await?;
282+
effects.apply().await?;
283+
284+
Ok(output_paths.clone())
261285
}
262286

263287
async fn hmr(

crates/next-napi-bindings/src/next_api/project.rs

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,11 @@ use turbo_rcstr::{RcStr, rcstr};
4949
use turbo_tasks::{
5050
Effects, FxIndexSet, NonLocalValue, OperationValue, OperationVc, PrettyPrintError, ReadRef,
5151
ResolvedVc, TaskInput, TransientInstance, TryJoinIterExt, TurboTasksApi, TurboTasksCallApi,
52-
UpdateInfo, Vc, get_effects,
52+
UpdateInfo, Vc, mark_top_level_task,
5353
message_queue::{CompilationEvent, Severity},
54+
take_effects,
5455
trace::TraceRawVcs,
56+
unmark_top_level_task_may_leak_eventually_consistent_state,
5557
};
5658
use turbo_tasks_backend::{BackingStorage, db_invalidation::invalidation_reasons};
5759
use turbo_tasks_fs::{
@@ -1840,7 +1842,7 @@ async fn hmr_update_with_issues_operation(
18401842
let filter = project.issue_filter();
18411843
let issues = get_issues(update_op, filter).await?;
18421844
let diagnostics = get_diagnostics(update_op).await?;
1843-
let effects = Arc::new(get_effects(update_op).await?);
1845+
let effects = Arc::new(take_effects(update_op).await?);
18441846
Ok(HmrUpdateWithIssues {
18451847
update,
18461848
issues,
@@ -1874,6 +1876,8 @@ pub fn project_hmr_events(
18741876
let chunk_name: RcStr = outer_chunk_name.clone();
18751877
let session = session.clone();
18761878
async move {
1879+
// HACK(bgw): Remove this unmark call
1880+
unmark_top_level_task_may_leak_eventually_consistent_state();
18771881
let project = container.project().to_resolved().await?;
18781882
let state = project
18791883
.hmr_version_state(chunk_name.clone(), hmr_target, session)
@@ -1893,7 +1897,11 @@ pub fn project_hmr_events(
18931897
diagnostics,
18941898
effects,
18951899
} = &*update;
1900+
// HACK(bgw): Remove this mark call
1901+
mark_top_level_task();
18961902
effects.apply().await?;
1903+
// HACK(bgw): Remove this unmark call
1904+
unmark_top_level_task_may_leak_eventually_consistent_state();
18971905
match &**update {
18981906
Update::Missing | Update::None => {}
18991907
Update::Total(TotalUpdate { to }) => {
@@ -1975,7 +1983,7 @@ async fn get_hmr_chunk_names_with_issues_operation(
19751983
let filter = issue_filter_from_container(container);
19761984
let issues = get_issues(hmr_chunk_names_op, filter).await?;
19771985
let diagnostics = get_diagnostics(hmr_chunk_names_op).await?;
1978-
let effects = Arc::new(get_effects(hmr_chunk_names_op).await?);
1986+
let effects = Arc::new(take_effects(hmr_chunk_names_op).await?);
19791987
Ok(HmrChunkNamesWithIssues {
19801988
chunk_names: hmr_chunk_names,
19811989
issues,

crates/next-napi-bindings/src/next_api/utils.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_hash::FxHashMap;
1515
use serde::Serialize;
1616
use turbo_rcstr::RcStr;
1717
use turbo_tasks::{
18-
Effects, OperationVc, ReadRef, TaskId, TryJoinIterExt, Vc, VcValueType, get_effects,
18+
Effects, OperationVc, ReadRef, TaskId, TryJoinIterExt, Vc, VcValueType, take_effects,
1919
};
2020
use turbo_tasks_fs::FileContent;
2121
use turbopack_core::{
@@ -486,7 +486,7 @@ pub async fn strongly_consistent_catch_collectables<R: VcValueType + Send>(
486486
let result = source_op.read_strongly_consistent().await;
487487
let issues = get_issues(source_op, filter).await?;
488488
let diagnostics = get_diagnostics(source_op).await?;
489-
let effects = Arc::new(get_effects(source_op).await?);
489+
let effects = Arc::new(take_effects(source_op).await?);
490490

491491
let result = if result.is_err() && issues.iter().any(|i| i.severity <= IssueSeverity::Error) {
492492
None

0 commit comments

Comments
 (0)