Skip to content

Commit

Permalink
move Asset::ident to more specific traits (#5528)
Browse files Browse the repository at this point in the history
### Description

* `ident()` is no longer on Asset, but on `Module`, `Source`,
`OutputAsset` or `Chunk`
* On the way, more AssetVc types needed to be switched to more specific
traits

next.js PR: vercel/next.js#52683
  • Loading branch information
sokra committed Jul 17, 2023
1 parent b069545 commit 02f55d9
Show file tree
Hide file tree
Showing 98 changed files with 853 additions and 485 deletions.
27 changes: 12 additions & 15 deletions crates/node-file-trace/src/lib.rs
Expand Up @@ -40,13 +40,14 @@ use turbopack::{
};
use turbopack_cli_utils::issue::{ConsoleUi, IssueSeverityCliOption, LogOptions};
use turbopack_core::{
asset::{Asset, Assets},
compile_time_info::CompileTimeInfo,
context::AssetContext,
environment::{Environment, ExecutionEnvironment, NodeJsEnvironment},
file_source::FileSource,
issue::{IssueContextExt, IssueReporter, IssueSeverity},
reference::all_assets,
module::{Module, Modules},
output::OutputAsset,
reference::all_modules,
resolve::options::{ImportMapping, ResolvedMap},
};

Expand Down Expand Up @@ -207,23 +208,23 @@ async fn create_fs(name: &str, context: &str, watch: bool) -> Result<Vc<Box<dyn
async fn add_glob_results(
context: Vc<Box<dyn AssetContext>>,
result: Vc<ReadGlobResult>,
list: &mut Vec<Vc<Box<dyn Asset>>>,
list: &mut Vec<Vc<Box<dyn Module>>>,
) -> Result<()> {
let result = result.await?;
for entry in result.results.values() {
if let DirectoryEntry::File(path) = entry {
let source = Vc::upcast(FileSource::new(*path));
list.push(Vc::upcast(context.process(
list.push(context.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
)));
));
}
}
for result in result.inner.values() {
fn recurse<'a>(
context: Vc<Box<dyn AssetContext>>,
result: Vc<ReadGlobResult>,
list: &'a mut Vec<Vc<Box<dyn Asset>>>,
list: &'a mut Vec<Vc<Box<dyn Module>>>,
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'a>> {
Box::pin(add_glob_results(context, result, list))
}
Expand All @@ -242,7 +243,7 @@ async fn input_to_modules(
context: String,
module_options: TransientInstance<ModuleOptionsContext>,
resolve_options: TransientInstance<ResolveOptionsContext>,
) -> Result<Vc<Assets>> {
) -> Result<Vc<Modules>> {
let root = fs.root();
let process_cwd = process_cwd
.clone()
Expand All @@ -259,13 +260,9 @@ async fn input_to_modules(
for input in input {
if exact {
let source = Vc::upcast(FileSource::new(root.join(input)));
list.push(Vc::upcast(
context
.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
)
.into(),
list.push(context.process(
source,
Value::new(turbopack_core::reference_type::ReferenceType::Undefined),
));
} else {
let glob = Glob::new(input);
Expand Down Expand Up @@ -564,7 +561,7 @@ async fn main_operation(
)
.await?;
for module in modules.iter() {
let set = all_assets(*module)
let set = all_modules(*module)
.issue_context(module.ident().path(), "gathering list of assets")
.await?;
for asset in set.await?.iter() {
Expand Down
19 changes: 10 additions & 9 deletions crates/node-file-trace/src/nft_json.rs
Expand Up @@ -5,44 +5,45 @@ use turbo_tasks_fs::{File, FileSystem};
use turbopack_core::{
asset::{Asset, AssetContent},
ident::AssetIdent,
module::Module,
output::OutputAsset,
reference::all_assets,
reference::all_modules,
};

#[turbo_tasks::value(shared)]
pub struct NftJsonAsset {
entry: Vc<Box<dyn Asset>>,
entry: Vc<Box<dyn Module>>,
}

#[turbo_tasks::value_impl]
impl NftJsonAsset {
#[turbo_tasks::function]
pub fn new(entry: Vc<Box<dyn Asset>>) -> Vc<Self> {
Self::cell(NftJsonAsset { entry })
pub fn new(entry: Vc<Box<dyn Module>>) -> Vc<Self> {
NftJsonAsset { entry }.cell()
}
}

#[turbo_tasks::value_impl]
impl OutputAsset for NftJsonAsset {}

#[turbo_tasks::value_impl]
impl Asset for NftJsonAsset {
impl OutputAsset for NftJsonAsset {
#[turbo_tasks::function]
async fn ident(&self) -> Result<Vc<AssetIdent>> {
let path = self.entry.ident().path().await?;
Ok(AssetIdent::from_path(
path.fs.root().join(format!("{}.nft.json", path.path)),
))
}
}

#[turbo_tasks::value_impl]
impl Asset for NftJsonAsset {
#[turbo_tasks::function]
async fn content(&self) -> Result<Vc<AssetContent>> {
let context = self.entry.ident().path().parent().await?;
// For clippy -- This explicit deref is necessary
let entry_path = &*self.entry.ident().path().await?;
let mut result = Vec::new();
if let Some(self_path) = context.get_relative_path_to(entry_path) {
let set = all_assets(self.entry);
let set = all_modules(self.entry);
for asset in set.await?.iter() {
let path = asset.ident().path().await?;
if let Some(rel_path) = context.get_relative_path_to(&path) {
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-build/src/chunking_context.rs
Expand Up @@ -6,10 +6,10 @@ use turbo_tasks::{
};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
asset::Asset,
chunk::{Chunk, ChunkableModule, ChunkingContext, Chunks, EvaluatableAssets},
environment::Environment,
ident::AssetIdent,
module::Module,
output::{OutputAsset, OutputAssets},
};
use turbopack_css::chunk::CssChunk;
Expand Down Expand Up @@ -225,15 +225,15 @@ impl ChunkingContext for BuildChunkingContext {
}

#[turbo_tasks::function]
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn Asset>>) -> Vc<bool> {
fn reference_chunk_source_maps(&self, _chunk: Vc<Box<dyn OutputAsset>>) -> Vc<bool> {
Vc::cell(true)
}

#[turbo_tasks::function]
async fn can_be_in_same_chunk(
&self,
asset_a: Vc<Box<dyn Asset>>,
asset_b: Vc<Box<dyn Asset>>,
asset_a: Vc<Box<dyn Module>>,
asset_b: Vc<Box<dyn Module>>,
) -> Result<Vc<bool>> {
let parent_dir = asset_a.ident().path().parent().await?;

Expand Down
10 changes: 5 additions & 5 deletions crates/turbopack-build/src/ecmascript/node/chunk.rs
Expand Up @@ -3,7 +3,7 @@ use indexmap::IndexSet;
use turbo_tasks::{ValueToString, Vc};
use turbopack_core::{
asset::{Asset, AssetContent},
chunk::ChunkingContext,
chunk::{Chunk, ChunkingContext},
ident::AssetIdent,
introspect::{Introspectable, IntrospectableChildren},
output::OutputAsset,
Expand Down Expand Up @@ -62,16 +62,16 @@ impl EcmascriptBuildNodeChunk {
}

#[turbo_tasks::value_impl]
impl OutputAsset for EcmascriptBuildNodeChunk {}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeChunk {
impl OutputAsset for EcmascriptBuildNodeChunk {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
let ident = self.chunk.ident().with_modifier(modifier());
AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string()))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand Down
3 changes: 2 additions & 1 deletion crates/turbopack-build/src/ecmascript/node/content.rs
Expand Up @@ -5,8 +5,9 @@ use indoc::writedoc;
use turbo_tasks::{TryJoinIterExt, Value, Vc};
use turbo_tasks_fs::File;
use turbopack_core::{
asset::{Asset, AssetContent},
asset::AssetContent,
code_builder::{Code, CodeBuilder},
output::OutputAsset,
source_map::{GenerateSourceMap, OptionSourceMap},
};
use turbopack_ecmascript::{
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-build/src/ecmascript/node/entry/chunk.rs
Expand Up @@ -180,15 +180,15 @@ fn chunk_reference_description() -> Vc<String> {
}

#[turbo_tasks::value_impl]
impl OutputAsset for EcmascriptBuildNodeEntryChunk {}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeEntryChunk {
impl OutputAsset for EcmascriptBuildNodeEntryChunk {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
AssetIdent::from_path(self.path)
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeEntryChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-build/src/ecmascript/node/entry/runtime.rs
Expand Up @@ -86,10 +86,7 @@ impl ValueToString for EcmascriptBuildNodeRuntimeChunk {
}

#[turbo_tasks::value_impl]
impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeRuntimeChunk {
impl OutputAsset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
fn ident(&self) -> Vc<AssetIdent> {
let ident = AssetIdent::from_path(
Expand All @@ -100,7 +97,10 @@ impl Asset for EcmascriptBuildNodeRuntimeChunk {

AssetIdent::from_path(self.chunking_context.chunk_path(ident, ".js".to_string()))
}
}

#[turbo_tasks::value_impl]
impl Asset for EcmascriptBuildNodeRuntimeChunk {
#[turbo_tasks::function]
async fn references(self: Vc<Self>) -> Result<Vc<AssetReferences>> {
let this = self.await?;
Expand Down
7 changes: 4 additions & 3 deletions crates/turbopack-cli-utils/src/runtime_entry.rs
Expand Up @@ -2,10 +2,10 @@ use anyhow::{bail, Result};
use turbo_tasks::{ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
asset::Asset,
chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
context::AssetContext,
issue::{IssueSeverity, OptionIssueSource},
module::{convert_asset_to_module, Module},
resolve::{origin::PlainResolveOrigin, parse::Request},
source::Source,
};
Expand Down Expand Up @@ -43,12 +43,13 @@ impl RuntimeEntry {
.await?;

let mut runtime_entries = Vec::with_capacity(assets.len());
for asset in &assets {
for &asset in &assets {
if let Some(entry) =
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(*asset).await?
Vc::try_resolve_sidecast::<Box<dyn EvaluatableAsset>>(asset).await?
{
runtime_entries.push(entry);
} else {
let asset = convert_asset_to_module(asset);
bail!(
"runtime reference resolved to an asset ({}) that cannot be evaluated",
asset.ident().to_string().await?
Expand Down
5 changes: 0 additions & 5 deletions crates/turbopack-core/src/asset.rs
Expand Up @@ -6,7 +6,6 @@ use turbo_tasks_fs::{
};

use crate::{
ident::AssetIdent,
reference::AssetReferences,
version::{VersionedAssetContent, VersionedContent},
};
Expand All @@ -32,10 +31,6 @@ impl Assets {
/// An asset. It also forms a graph when following [Asset::references].
#[turbo_tasks::value_trait]
pub trait Asset {
/// The identifier of the [Asset]. It's expected to be unique and capture
/// all properties of the [Asset].
fn ident(self: Vc<Self>) -> Vc<AssetIdent>;

/// The content of the [Asset].
fn content(self: Vc<Self>) -> Vc<AssetContent>;

Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-core/src/chunk/availability_info.rs
@@ -1,23 +1,23 @@
use turbo_tasks::Vc;

use super::available_assets::AvailableAssets;
use crate::asset::Asset;
use crate::module::Module;

#[turbo_tasks::value(serialization = "auto_for_input")]
#[derive(PartialOrd, Ord, Hash, Clone, Copy, Debug)]
pub enum AvailabilityInfo {
Untracked,
Root {
current_availability_root: Vc<Box<dyn Asset>>,
current_availability_root: Vc<Box<dyn Module>>,
},
Inner {
available_assets: Vc<AvailableAssets>,
current_availability_root: Vc<Box<dyn Asset>>,
current_availability_root: Vc<Box<dyn Module>>,
},
}

impl AvailabilityInfo {
pub fn current_availability_root(&self) -> Option<Vc<Box<dyn Asset>>> {
pub fn current_availability_root(&self) -> Option<Vc<Box<dyn Module>>> {
match self {
Self::Untracked => None,
Self::Root {
Expand Down

0 comments on commit 02f55d9

Please sign in to comment.