Unverified Commit 71fba193 authored by Will Binns-Smith's avatar Will Binns-Smith Committed by GitHub

[Turbopack] [HMR] Allow `Option`s to be returned by `VersionedContentMap` (#69079)

Building on #68698, which causes `Error`s propagated from the
`VersionedContentMap` to surface as errors instead of reloading the
page, allow intentionally missing values to be returned.

There are scenarios in which this is desirable and not an error, such as
when the content of entries change and the page must be reloaded.

In these cases, a `Update:Missing` is returned, which causes the page to
be reloaded.

Test Plan: CI

PACK-3188

---------
Co-authored-by: 's avatarTobias Koppers <tobias.koppers@googlemail.com>
parent 28e0488a
...@@ -733,7 +733,8 @@ pub fn project_hmr_events( ...@@ -733,7 +733,8 @@ pub fn project_hmr_events(
async move { async move {
let project = project.project().resolve().await?; let project = project.project().resolve().await?;
let state = project.hmr_version_state(identifier.clone(), session); let state = project.hmr_version_state(identifier.clone(), session);
let update = hmr_update(project, identifier, state)
let update = hmr_update(project, identifier.clone(), state)
.strongly_consistent() .strongly_consistent()
.await .await
.inspect_err(|e| log_panic_and_inform(e))?; .inspect_err(|e| log_panic_and_inform(e))?;
...@@ -743,7 +744,7 @@ pub fn project_hmr_events( ...@@ -743,7 +744,7 @@ pub fn project_hmr_events(
diagnostics, diagnostics,
} = &*update; } = &*update;
match &**update { match &**update {
Update::None => {} Update::Missing | Update::None => {}
Update::Total(TotalUpdate { to }) => { Update::Total(TotalUpdate { to }) => {
state.set(to.clone()).await?; state.set(to.clone()).await?;
} }
...@@ -751,7 +752,7 @@ pub fn project_hmr_events( ...@@ -751,7 +752,7 @@ pub fn project_hmr_events(
state.set(to.clone()).await?; state.set(to.clone()).await?;
} }
} }
Ok((update.clone(), issues.clone(), diagnostics.clone())) Ok((Some(update.clone()), issues.clone(), diagnostics.clone()))
} }
.instrument(tracing::info_span!( .instrument(tracing::info_span!(
"HMR subscription", "HMR subscription",
...@@ -775,14 +776,16 @@ pub fn project_hmr_events( ...@@ -775,14 +776,16 @@ pub fn project_hmr_events(
path: identifier.clone(), path: identifier.clone(),
headers: None, headers: None,
}; };
let update = match &*update { let update = match update.as_deref() {
Update::Total(_) => ClientUpdateInstruction::restart(&identifier, &update_issues), None | Some(Update::Missing) | Some(Update::Total(_)) => {
Update::Partial(update) => ClientUpdateInstruction::partial( ClientUpdateInstruction::restart(&identifier, &update_issues)
}
Some(Update::Partial(update)) => ClientUpdateInstruction::partial(
&identifier, &identifier,
&update.instruction, &update.instruction,
&update_issues, &update_issues,
), ),
Update::None => ClientUpdateInstruction::issues(&identifier, &update_issues), Some(Update::None) => ClientUpdateInstruction::issues(&identifier, &update_issues),
}; };
Ok(vec![TurbopackResult { Ok(vec![TurbopackResult {
...@@ -1030,18 +1033,22 @@ pub async fn project_trace_source( ...@@ -1030,18 +1033,22 @@ pub async fn project_trace_source(
.client_relative_path() .client_relative_path()
.join(chunk_base.into()); .join(chunk_base.into());
let mut map_result = project let mut map = project
.container .container
.get_source_map(server_path, module.clone()) .get_source_map(server_path, module.clone())
.await; .await?;
if map_result.is_err() {
if map.is_none() {
// If the chunk doesn't exist as a server chunk, try a client chunk. // If the chunk doesn't exist as a server chunk, try a client chunk.
// TODO: Properly tag all server chunks and use the `isServer` query param. // TODO: Properly tag all server chunks and use the `isServer` query param.
// 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_result = project.container.get_source_map(client_path, module).await; map = project
.container
.get_source_map(client_path, module)
.await?;
} }
let map = map_result?.context("chunk/module is missing a sourcemap")?; let map = map.context("chunk/module is missing a sourcemap")?;
let Some(line) = frame.line else { let Some(line) = frame.line else {
return Ok(None); return Ok(None);
......
...@@ -49,7 +49,9 @@ use turbopack_core::{ ...@@ -49,7 +49,9 @@ use turbopack_core::{
output::{OutputAsset, OutputAssets}, output::{OutputAsset, OutputAssets},
resolve::{find_context_file, FindContextFileResult}, resolve::{find_context_file, FindContextFileResult},
source_map::OptionSourceMap, source_map::OptionSourceMap,
version::{Update, Version, VersionState, VersionedContent}, version::{
NotFoundVersion, OptionVersionedContent, Update, Version, VersionState, VersionedContent,
},
PROJECT_FILESYSTEM_NAME, PROJECT_FILESYSTEM_NAME,
}; };
use turbopack_node::execution_context::ExecutionContext; use turbopack_node::execution_context::ExecutionContext;
...@@ -1155,12 +1157,10 @@ impl Project { ...@@ -1155,12 +1157,10 @@ impl Project {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
async fn hmr_content( async fn hmr_content(self: Vc<Self>, identifier: RcStr) -> Result<Vc<OptionVersionedContent>> {
self: Vc<Self>,
identifier: RcStr,
) -> Result<Vc<Box<dyn VersionedContent>>> {
if let Some(map) = self.await?.versioned_content_map { if let Some(map) = self.await?.versioned_content_map {
Ok(map.get(self.client_relative_path().join(identifier))) let content = map.get(self.client_relative_path().join(identifier.clone()));
Ok(content)
} else { } else {
bail!("must be in dev mode to hmr") bail!("must be in dev mode to hmr")
} }
...@@ -1168,9 +1168,12 @@ impl Project { ...@@ -1168,9 +1168,12 @@ impl Project {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> { async fn hmr_version(self: Vc<Self>, identifier: RcStr) -> Result<Vc<Box<dyn Version>>> {
let content = self.hmr_content(identifier); let content = self.hmr_content(identifier).await?;
if let Some(content) = &*content {
Ok(content.version()) Ok(content.version())
} else {
Ok(Vc::upcast(NotFoundVersion::new()))
}
} }
/// Get the version state for a session. Initialized with the first seen /// Get the version state for a session. Initialized with the first seen
...@@ -1190,12 +1193,13 @@ impl Project { ...@@ -1190,12 +1193,13 @@ impl Project {
// INVALIDATION: This is intentionally untracked to avoid invalidating this // INVALIDATION: This is intentionally untracked to avoid invalidating this
// function completely. We want to initialize the VersionState with the // function completely. We want to initialize the VersionState with the
// first seen version of the session. // first seen version of the session.
VersionState::new( let state = VersionState::new(
version version
.into_trait_ref_strongly_consistent_untracked() .into_trait_ref_strongly_consistent_untracked()
.await?, .await?,
) )
.await .await?;
Ok(state)
} }
/// Emits opaque HMR events whenever a change is detected in the chunk group /// Emits opaque HMR events whenever a change is detected in the chunk group
...@@ -1207,7 +1211,12 @@ impl Project { ...@@ -1207,7 +1211,12 @@ impl Project {
from: Vc<VersionState>, from: Vc<VersionState>,
) -> Result<Vc<Update>> { ) -> Result<Vc<Update>> {
let from = from.get(); let from = from.get();
Ok(self.hmr_content(identifier).update(from)) let content = self.hmr_content(identifier).await?;
if let Some(content) = *content {
Ok(content.update(from))
} else {
Ok(Update::Missing.cell())
}
} }
/// Gets a list of all HMR identifiers that can be subscribed to. This is /// Gets a list of all HMR identifiers that can be subscribed to. This is
......
...@@ -11,9 +11,9 @@ use turbo_tasks::{ ...@@ -11,9 +11,9 @@ use turbo_tasks::{
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
asset::Asset, asset::Asset,
output::{OutputAsset, OutputAssets}, output::{OptionOutputAsset, OutputAsset, OutputAssets},
source_map::{GenerateSourceMap, OptionSourceMap}, source_map::{GenerateSourceMap, OptionSourceMap},
version::VersionedContent, version::OptionVersionedContent,
}; };
/// An unresolved output assets operation. We need to pass an operation here as /// An unresolved output assets operation. We need to pass an operation here as
...@@ -26,6 +26,7 @@ pub struct OutputAssetsOperation(Vc<OutputAssets>); ...@@ -26,6 +26,7 @@ pub struct OutputAssetsOperation(Vc<OutputAssets>);
struct MapEntry { struct MapEntry {
assets_operation: Vc<OutputAssets>, assets_operation: Vc<OutputAssets>,
side_effects: Vc<Completion>, side_effects: Vc<Completion>,
/// Precomputed map for quick access to output asset by filepath
path_to_asset: HashMap<Vc<FileSystemPath>, Vc<Box<dyn OutputAsset>>>, path_to_asset: HashMap<Vc<FileSystemPath>, Vc<Box<dyn OutputAsset>>>,
} }
...@@ -33,6 +34,7 @@ struct MapEntry { ...@@ -33,6 +34,7 @@ struct MapEntry {
struct OptionMapEntry(Option<MapEntry>); struct OptionMapEntry(Option<MapEntry>);
type PathToOutputOperation = HashMap<Vc<FileSystemPath>, IndexSet<Vc<OutputAssets>>>; type PathToOutputOperation = HashMap<Vc<FileSystemPath>, IndexSet<Vc<OutputAssets>>>;
// A precomputed map for quick access to output asset by filepath
type OutputOperationToComputeEntry = HashMap<Vc<OutputAssets>, Vc<OptionMapEntry>>; type OutputOperationToComputeEntry = HashMap<Vc<OutputAssets>, Vc<OptionMapEntry>>;
#[turbo_tasks::value] #[turbo_tasks::value]
...@@ -67,6 +69,7 @@ impl VersionedContentMap { ...@@ -67,6 +69,7 @@ impl VersionedContentMap {
#[turbo_tasks::function] #[turbo_tasks::function]
pub async fn insert_output_assets( pub async fn insert_output_assets(
self: Vc<Self>, self: Vc<Self>,
// Output assets to emit
assets_operation: Vc<OutputAssetsOperation>, assets_operation: Vc<OutputAssetsOperation>,
node_root: Vc<FileSystemPath>, node_root: Vc<FileSystemPath>,
client_relative_path: Vc<FileSystemPath>, client_relative_path: Vc<FileSystemPath>,
...@@ -88,6 +91,8 @@ impl VersionedContentMap { ...@@ -88,6 +91,8 @@ impl VersionedContentMap {
Ok(entry.side_effects) Ok(entry.side_effects)
} }
/// Creates a ComputEntry (a pre-computed map for optimized lookup) for an output assets
/// operation. When assets change, map_path_to_op is updated.
#[turbo_tasks::function] #[turbo_tasks::function]
async fn compute_entry( async fn compute_entry(
self: Vc<Self>, self: Vc<Self>,
...@@ -148,8 +153,13 @@ impl VersionedContentMap { ...@@ -148,8 +153,13 @@ impl VersionedContentMap {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
pub fn get(self: Vc<Self>, path: Vc<FileSystemPath>) -> Vc<Box<dyn VersionedContent>> { pub async fn get(
self.get_asset(path).versioned_content() self: Vc<Self>,
path: Vc<FileSystemPath>,
) -> Result<Vc<OptionVersionedContent>> {
Ok(Vc::cell(
(*self.get_asset(path).await?).map(|a| a.versioned_content()),
))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
...@@ -158,8 +168,12 @@ impl VersionedContentMap { ...@@ -158,8 +168,12 @@ impl VersionedContentMap {
path: Vc<FileSystemPath>, path: Vc<FileSystemPath>,
section: Option<RcStr>, section: Option<RcStr>,
) -> Result<Vc<OptionSourceMap>> { ) -> Result<Vc<OptionSourceMap>> {
let Some(asset) = &*self.get_asset(path).await? else {
return Ok(Vc::cell(None));
};
if let Some(generate_source_map) = if let Some(generate_source_map) =
Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(self.get_asset(path)).await? Vc::try_resolve_sidecast::<Box<dyn GenerateSourceMap>>(*asset).await?
{ {
Ok(if let Some(section) = section { Ok(if let Some(section) = section {
generate_source_map.by_section(section) generate_source_map.by_section(section)
...@@ -176,7 +190,7 @@ impl VersionedContentMap { ...@@ -176,7 +190,7 @@ impl VersionedContentMap {
pub async fn get_asset( pub async fn get_asset(
self: Vc<Self>, self: Vc<Self>,
path: Vc<FileSystemPath>, path: Vc<FileSystemPath>,
) -> Result<Vc<Box<dyn OutputAsset>>> { ) -> Result<Vc<OptionOutputAsset>> {
let result = self.raw_get(path).await?; let result = self.raw_get(path).await?;
if let Some(MapEntry { if let Some(MapEntry {
assets_operation: _, assets_operation: _,
...@@ -187,17 +201,17 @@ impl VersionedContentMap { ...@@ -187,17 +201,17 @@ impl VersionedContentMap {
side_effects.await?; side_effects.await?;
if let Some(asset) = path_to_asset.get(&path) { if let Some(asset) = path_to_asset.get(&path) {
return Ok(*asset); return Ok(Vc::cell(Some(*asset)));
} else { } else {
let path = path.to_string().await?; let path = path.to_string().await?;
bail!( bail!(
"could not find asset for path {} (asset has been removed)", "could not find asset for path {} (asset has been removed)",
path path,
); );
} }
} }
let path = path.to_string().await?;
bail!("could not find asset for path {}", path); Ok(Vc::cell(None))
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -257,12 +257,8 @@ async fn hmr(tt: &TurboTasks<MemoryBackend>, project: Vc<ProjectContainer>) -> R ...@@ -257,12 +257,8 @@ async fn hmr(tt: &TurboTasks<MemoryBackend>, project: Vc<ProjectContainer>) -> R
let session = session.clone(); let session = session.clone();
async move { async move {
let project = project.project(); let project = project.project();
project let state = project.hmr_version_state(ident.clone(), session);
.hmr_update( project.hmr_update(ident.clone(), state).await?;
ident.clone(),
project.hmr_version_state(ident.clone(), session),
)
.await?;
Ok(Vc::<()>::cell(())) Ok(Vc::<()>::cell(()))
} }
}); });
......
...@@ -122,7 +122,7 @@ pub(super) async fn update_chunk_list( ...@@ -122,7 +122,7 @@ pub(super) async fn update_chunk_list(
}, },
); );
} }
Update::None => {} Update::Missing | Update::None => {}
} }
} else { } else {
chunks.insert(chunk_path.as_ref(), ChunkUpdate::Deleted); chunks.insert(chunk_path.as_ref(), ChunkUpdate::Deleted);
...@@ -156,7 +156,7 @@ pub(super) async fn update_chunk_list( ...@@ -156,7 +156,7 @@ pub(super) async fn update_chunk_list(
Update::Partial(partial) => { Update::Partial(partial) => {
merged.push(partial.instruction.clone()); merged.push(partial.instruction.clone());
} }
Update::None => {} Update::Missing | Update::None => {}
} }
} }
} }
......
...@@ -4,6 +4,9 @@ use turbo_tasks::Vc; ...@@ -4,6 +4,9 @@ use turbo_tasks::Vc;
use crate::{asset::Asset, ident::AssetIdent}; use crate::{asset::Asset, ident::AssetIdent};
#[turbo_tasks::value(transparent)]
pub struct OptionOutputAsset(Option<Vc<Box<dyn OutputAsset>>>);
/// An asset that should be outputted, e. g. written to disk or served from a /// An asset that should be outputted, e. g. written to disk or served from a
/// server. /// server.
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
......
...@@ -10,6 +10,9 @@ use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64}; ...@@ -10,6 +10,9 @@ use turbo_tasks_hash::{encode_hex, hash_xxh3_hash64};
use crate::asset::AssetContent; use crate::asset::AssetContent;
#[turbo_tasks::value(transparent)]
pub struct OptionVersionedContent(Option<Vc<Box<dyn VersionedContent>>>);
/// The content of an [Asset] alongside its version. /// The content of an [Asset] alongside its version.
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait VersionedContent { pub trait VersionedContent {
...@@ -176,6 +179,9 @@ pub enum Update { ...@@ -176,6 +179,9 @@ pub enum Update {
/// specific set of instructions. /// specific set of instructions.
Partial(PartialUpdate), Partial(PartialUpdate),
// The asset is now missing, so it can't be updated. A full reload is required.
Missing,
/// No update required. /// No update required.
None, None,
} }
......
...@@ -143,7 +143,7 @@ impl<P: SourceProvider + Clone + Send + Sync> UpdateServer<P> { ...@@ -143,7 +143,7 @@ impl<P: SourceProvider + Clone + Send + Sync> UpdateServer<P> {
)) ))
.await?; .await?;
} }
Update::Total(_total) => { Update::Missing | Update::Total(_) => {
client client
.send(ClientUpdateInstruction::restart(&resource, &issues)) .send(ClientUpdateInstruction::restart(&resource, &issues))
.await?; .await?;
......
...@@ -234,7 +234,7 @@ impl UpdateStream { ...@@ -234,7 +234,7 @@ impl UpdateStream {
Some(item) Some(item)
} }
// Do not propagate empty updates. // Do not propagate empty updates.
Update::None => { Update::None | Update::Missing => {
if has_issues || issues_changed { if has_issues || issues_changed {
Some(item) Some(item)
} else { } else {
......
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