Unverified Commit b005187a authored by Benjamin Woodruff's avatar Benjamin Woodruff Committed by GitHub

fix(turbopack): Use strongly consistent reads for sourcemaps in napi FFI boundary (#77511)

All turbo-task reads from an FFI boundary must be strongly-consistent.

Otherwise, we might propagate errors due to inconsistent cell state.

We suspect it's the root cause for this: https://vercel.slack.com/archives/C04MKMSEV3K/p1742874851690489
parent 950a20ea
...@@ -304,7 +304,7 @@ impl From<NapiDefineEnv> for DefineEnv { ...@@ -304,7 +304,7 @@ impl From<NapiDefineEnv> for DefineEnv {
pub struct ProjectInstance { pub struct ProjectInstance {
turbo_tasks: NextTurboTasks, turbo_tasks: NextTurboTasks,
container: Vc<ProjectContainer>, container: ResolvedVc<ProjectContainer>,
exit_receiver: tokio::sync::Mutex<Option<ExitReceiver>>, exit_receiver: tokio::sync::Mutex<Option<ExitReceiver>>,
} }
...@@ -434,7 +434,7 @@ pub async fn project_new( ...@@ -434,7 +434,7 @@ pub async fn project_new(
Ok(External::new_with_size_hint( Ok(External::new_with_size_hint(
ProjectInstance { ProjectInstance {
turbo_tasks, turbo_tasks,
container: *container, container,
exit_receiver: tokio::sync::Mutex::new(Some(exit_receiver)), exit_receiver: tokio::sync::Mutex::new(Some(exit_receiver)),
}, },
100, 100,
...@@ -782,10 +782,8 @@ pub async fn project_write_all_entrypoints_to_disk( ...@@ -782,10 +782,8 @@ pub async fn project_write_all_entrypoints_to_disk(
let turbo_tasks = project.turbo_tasks.clone(); let turbo_tasks = project.turbo_tasks.clone();
let (entrypoints, issues, diags) = turbo_tasks let (entrypoints, issues, diags) = turbo_tasks
.run_once(async move { .run_once(async move {
let entrypoints_with_issues_op = get_all_written_entrypoints_with_issues_operation( let entrypoints_with_issues_op =
project.container.to_resolved().await?, get_all_written_entrypoints_with_issues_operation(project.container, app_dir_only);
app_dir_only,
);
let EntrypointsWithIssues { let EntrypointsWithIssues {
entrypoints, entrypoints,
...@@ -879,8 +877,7 @@ pub fn project_entrypoints_subscribe( ...@@ -879,8 +877,7 @@ pub fn project_entrypoints_subscribe(
func, func,
move || { move || {
async move { async move {
let entrypoints_with_issues_op = let entrypoints_with_issues_op = get_entrypoints_with_issues_operation(container);
get_entrypoints_with_issues_operation(container.to_resolved().await?);
let EntrypointsWithIssues { let EntrypointsWithIssues {
entrypoints, entrypoints,
issues, issues,
...@@ -1085,7 +1082,7 @@ pub fn project_hmr_identifiers_subscribe( ...@@ -1085,7 +1082,7 @@ pub fn project_hmr_identifiers_subscribe(
func, func,
move || async move { move || async move {
let hmr_identifiers_with_issues_op = let hmr_identifiers_with_issues_op =
get_hmr_identifiers_with_issues_operation(container.to_resolved().await?); get_hmr_identifiers_with_issues_operation(container);
let HmrIdentifiersWithIssues { let HmrIdentifiersWithIssues {
identifiers, identifiers,
issues, issues,
...@@ -1236,10 +1233,11 @@ pub struct StackFrame { ...@@ -1236,10 +1233,11 @@ pub struct StackFrame {
pub method_name: Option<String>, pub method_name: Option<String>,
} }
#[turbo_tasks::function]
pub async fn get_source_map_rope( pub async fn get_source_map_rope(
container: Vc<ProjectContainer>, container: Vc<ProjectContainer>,
file_path: String, file_path: RcStr,
) -> Result<Option<Vc<OptionStringifiedSourceMap>>> { ) -> Result<Vc<OptionStringifiedSourceMap>> {
let (file, module) = match Url::parse(&file_path) { let (file, module) = match Url::parse(&file_path) {
Ok(url) => match url.scheme() { Ok(url) => match url.scheme() {
"file" => { "file" => {
...@@ -1266,7 +1264,7 @@ pub async fn get_source_map_rope( ...@@ -1266,7 +1264,7 @@ pub async fn get_source_map_rope(
)), )),
) else { ) else {
// File doesn't exist within the dist dir // File doesn't exist within the dist dir
return Ok(None); return Ok(OptionStringifiedSourceMap::none());
}; };
let server_path = container.project().node_root().join(chunk_base.into()); let server_path = container.project().node_root().join(chunk_base.into());
...@@ -1284,24 +1282,29 @@ pub async fn get_source_map_rope( ...@@ -1284,24 +1282,29 @@ pub async fn get_source_map_rope(
// Currently, this is inaccurate as it does not cover RSC server // Currently, this is inaccurate as it does not cover RSC server
// chunks. // chunks.
map = container.get_source_map(client_path, module); map = container.get_source_map(client_path, module);
if map.await?.is_none() {
bail!("chunk/module '{}' is missing a sourcemap", file_path);
}
} }
if map.await?.is_none() { Ok(map)
bail!("chunk/module '{}' is missing a sourcemap", file_path); }
}
Ok(Some(map)) #[turbo_tasks::function(operation)]
pub fn get_source_map_rope_operation(
container: ResolvedVc<ProjectContainer>,
file_path: RcStr,
) -> Vc<OptionStringifiedSourceMap> {
get_source_map_rope(*container, file_path)
} }
pub async fn get_source_map( #[turbo_tasks::function(operation)]
container: Vc<ProjectContainer>, pub fn get_source_map_operation(
file_path: String, container: ResolvedVc<ProjectContainer>,
) -> Result<Option<ReadRef<OptionSourceMap>>> { file_path: RcStr,
let Some(map) = get_source_map_rope(container, file_path).await? else { ) -> Vc<OptionSourceMap> {
return Ok(None); let map = get_source_map_rope(*container, file_path);
}; SourceMap::new_from_rope_cached(map)
let map = SourceMap::new_from_rope_cached(map).await?;
Ok(Some(map))
} }
#[napi] #[napi]
...@@ -1314,10 +1317,10 @@ pub async fn project_trace_source( ...@@ -1314,10 +1317,10 @@ pub async fn project_trace_source(
let container = project.container; let container = project.container;
let traced_frame = turbo_tasks let traced_frame = turbo_tasks
.run_once(async move { .run_once(async move {
let Some(map) = get_source_map(container, frame.file).await? else { let Some(map) = &*get_source_map_operation(container, RcStr::from(frame.file))
return Ok(None); .read_strongly_consistent()
}; .await?
let Some(map) = &*map else { else {
return Ok(None); return Ok(None);
}; };
...@@ -1445,10 +1448,10 @@ pub async fn project_get_source_map( ...@@ -1445,10 +1448,10 @@ pub async fn project_get_source_map(
let source_map = turbo_tasks let source_map = turbo_tasks
.run_once(async move { .run_once(async move {
let Some(map) = get_source_map_rope(container, file_path).await? else { let Some(map) = &*get_source_map_rope_operation(container, RcStr::from(file_path))
return Ok(None); .read_strongly_consistent()
}; .await?
let Some(map) = &*map.await? else { else {
return Ok(None); return Ok(None);
}; };
Ok(Some(map.to_str()?.to_string())) Ok(Some(map.to_str()?.to_string()))
......
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