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