Skip to content

Commit

Permalink
turbopack-core fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
alexkirsz committed May 26, 2023
1 parent 1bc58d9 commit bda331c
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 45 deletions.
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/chunk/available_assets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ async fn chunkable_assets_set(root: Vc<Box<dyn Asset>>) -> Result<Vc<AssetsSet>>
let mut results = Vec::new();
for reference in asset.references().await?.iter() {
if let Some(chunkable) =
Vc::try_resolve_downcast::<Box<dyn ChunkableAssetReference>>(reference).await?
Vc::try_resolve_downcast::<Box<dyn ChunkableAssetReference>>(*reference).await?
{
if matches!(
&*chunkable.chunking_type().await?,
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/chunk/chunking_context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub trait ChunkingContext {

fn asset_path(
self: Vc<Self>,
content_hash: &str,
content_hash: String,
original_asset_ident: Vc<AssetIdent>,
) -> Vc<FileSystemPath>;

Expand Down
7 changes: 3 additions & 4 deletions crates/turbopack-core/src/chunk/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl ChunkData {
};
let path = path.to_string();

let Some(output_chunk) = Vc::try_resolve_sidecast::<&dyn OutputChunk>(chunk).await? else {
let Some(output_chunk) = Vc::try_resolve_sidecast::<Box<dyn OutputChunk>>(chunk).await? else {
return Ok(Vc::cell(Some(ChunkData {
path,
included: Vec::new(),
Expand Down Expand Up @@ -91,11 +91,10 @@ impl ChunkData {
Ok(output_root.get_path_to(&*chunk_path).map(|path| {
(
path.to_owned(),
SingleAssetReference::new(
Vc::upcast(SingleAssetReference::new(
chunk,
module_chunk_reference_description(),
)
.as_asset_reference(),
)),
)
}))
}
Expand Down
7 changes: 3 additions & 4 deletions crates/turbopack-core/src/chunk/evaluate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,17 @@ use crate::{
#[turbo_tasks::value_trait]
pub trait EvaluatableAsset: Asset + ChunkableAsset {}

#[turbo_tasks::value_trait]
#[async_trait::async_trait]
pub trait EvaluatableAssetExt {
async fn to_evaluatable(
self: Vc<Self>,
context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn EvaluatableAsset>>>;
}

#[turbo_tasks::value_impl]
#[async_trait::async_trait]
impl EvaluatableAssetExt for Box<dyn Asset> {
#[turbo_tasks::function]
pub async fn to_evaluatable(
async fn to_evaluatable(
self: Vc<Self>,
context: Vc<Box<dyn AssetContext>>,
) -> Result<Vc<Box<dyn EvaluatableAsset>>> {
Expand Down
8 changes: 4 additions & 4 deletions crates/turbopack-core/src/chunk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Chunks {
/// Creates a new empty [Vc<Chunks>].
#[turbo_tasks::function]
pub fn empty() -> Vc<Self> {
Self::cell(vec![])
Vc::cell(vec![])
}
}

Expand Down Expand Up @@ -239,7 +239,7 @@ impl ValueToString for ChunkGroupReference {
}

pub struct ChunkContentResult<I> {
pub chunk_items: Vec<Vc<I>>,
pub chunk_items: Vec<I>,
pub chunks: Vec<Vc<Box<dyn Chunk>>>,
pub async_chunk_group_entries: Vec<Vc<Box<dyn Chunk>>>,
pub external_asset_references: Vec<Vc<Box<dyn AssetReference>>>,
Expand Down Expand Up @@ -524,7 +524,7 @@ where
VisitControlFlow::Continue(node)
}

fn edges(&mut self, node: &ChunkContentGraphNode<I>) -> Self::EdgesFuture {
fn edges(&mut self, node: &ChunkContentGraphNode<Vc<I>>) -> Self::EdgesFuture {
let chunk_item = if let ChunkContentGraphNode::ChunkItem {
item: chunk_item, ..
} = node
Expand Down Expand Up @@ -553,7 +553,7 @@ where
}
}

fn span(&mut self, node: &ChunkContentGraphNode<I>) -> Span {
fn span(&mut self, node: &ChunkContentGraphNode<Vc<I>>) -> Span {
if let ChunkContentGraphNode::ChunkItem { ident, .. } = node {
info_span!("module", name = display(ident))
} else {
Expand Down
14 changes: 7 additions & 7 deletions crates/turbopack-core/src/ident.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ impl AssetIdent {
self.assets.push((key, asset));
}

pub async fn rename_as(&mut self, pattern: &str) -> Result<()> {
pub async fn rename_as_ref(&mut self, pattern: &str) -> Result<()> {
let root = self.path.root();
let path = self.path.await?;
self.path = root
.join(&pattern.replace("*", &path.path))
.join(pattern.replace("*", &path.path))
.resolve()
.await?;
Ok(())
Expand Down Expand Up @@ -109,8 +109,8 @@ impl AssetIdent {
#[turbo_tasks::function]
pub async fn rename_as(self: Vc<Self>, pattern: String) -> Result<Vc<Self>> {
let mut this = self.await?.clone_value();
this.rename_as(pattern).await?;
Ok(Vc::<Self>::new(Value::new(this)))
this.rename_as_ref(&pattern).await?;
Ok(Self::new(Value::new(this)))
}

#[turbo_tasks::function]
Expand All @@ -137,7 +137,7 @@ impl AssetIdent {
} else {
clean_separators(&this.path.to_string().await?)
};
let removed_extension = name.ends_with(expected_extension);
let removed_extension = name.ends_with(&expected_extension);
if removed_extension {
name.truncate(name.len() - expected_extension.len());
}
Expand All @@ -146,7 +146,7 @@ impl AssetIdent {
// Next.js).
let mut name = clean_additional_extensions(&name);

let default_modifier = match expected_extension {
let default_modifier = match expected_extension.as_str() {
".js" => Some("ecmascript"),
".css" => Some("css"),
_ => None,
Expand Down Expand Up @@ -241,7 +241,7 @@ impl AssetIdent {
if !removed_extension {
name += "._";
}
name += expected_extension;
name += &expected_extension;
Ok(Vc::cell(name))
}
}
Expand Down
2 changes: 1 addition & 1 deletion crates/turbopack-core/src/package_json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use turbo_tasks::{debug::ValueDebugFormat, trace::TraceRawVcs, ReadRef, Vc};
use turbo_tasks_fs::{FileContent, FileJsonContent, FileSystemPath};

use super::issue::Issue;
use crate::issue::IssueExt;

/// PackageJson wraps the parsed JSON content of a `package.json` file. The
/// wrapper is necessary so that we can reference the [FileJsonContent]'s inner
Expand Down Expand Up @@ -47,7 +48,6 @@ pub async fn read_package_json(path: Vc<FileSystemPath>) -> Result<Vc<OptionPack
path,
}
.cell()
.as_issue()
.emit();
Ok(OptionPackageJson(None).cell())
}
Expand Down
33 changes: 18 additions & 15 deletions crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,19 @@ use self::{
ResolveIntoPackage, ResolveModules, ResolveModulesOptions, ResolveOptions,
},
parse::Request,
pattern::QueryMap,
pattern::{Pattern, QueryMap},
remap::{ExportsField, ImportsField},
};
use crate::{
asset::{Asset, AssetOption, Assets},
issue::resolve::ResolvingIssue,
issue::{resolve::ResolvingIssue, IssueExt},
package_json::{read_package_json, PackageJsonIssue},
reference::AssetReference,
reference_type::ReferenceType,
resolve::{
pattern::{read_matches, PatternMatch},
plugin::ResolvePlugin,
},
source_asset::SourceAsset,
};

Expand All @@ -44,7 +48,6 @@ pub use alias_map::{
AliasMap, AliasMapIntoIter, AliasMapLookupIterator, AliasMatch, AliasPattern, AliasTemplate,
};
pub use remap::{ResolveAliasMap, SubpathValue};
use turbo_tasks::Vc;

use crate::issue::{IssueSeverity, OptionIssueSource};

Expand Down Expand Up @@ -404,12 +407,12 @@ async fn exports_field(package_json_path: Vc<FileSystemPath>) -> Result<Vc<Expor
match exports.try_into() {
Ok(exports) => Ok(ExportsFieldResult::Some(exports).cell()),
Err(err) => {
let issue: Vc<PackageJsonIssue> = PackageJsonIssue {
PackageJsonIssue {
path: package_json_path,
error_message: err.to_string(),
}
.into();
issue.as_issue().emit();
.cell()
.emit();
Ok(ExportsFieldResult::None.cell())
}
}
Expand Down Expand Up @@ -445,12 +448,12 @@ async fn imports_field(context: Vc<FileSystemPath>) -> Result<Vc<ImportsFieldRes
match imports.try_into() {
Ok(imports) => Ok(ImportsFieldResult::Some(imports, *package_json_path).cell()),
Err(err) => {
let issue: Vc<PackageJsonIssue> = PackageJsonIssue {
PackageJsonIssue {
path: *package_json_path,
error_message: err.to_string(),
}
.into();
issue.as_issue().emit();
.cell()
.emit();
Ok(ImportsFieldResult::None.cell())
}
}
Expand Down Expand Up @@ -916,13 +919,13 @@ async fn resolve_into_folder(
package_path: Vc<FileSystemPath>,
options: Vc<ResolveOptions>,
) -> Result<Vc<ResolveResult>> {
let package_json_path = package_path.join("package.json");
let package_json_path = package_path.join("package.json".to_string());
let options_value = options.await?;
for resolve_into_package in options_value.into_package.iter() {
match resolve_into_package {
ResolveIntoPackage::Default(req) => {
let str = "./".to_string()
+ &*normalize_path(req).ok_or_else(|| {
+ &*normalize_path(&req).ok_or_else(|| {
anyhow!(
"ResolveIntoPackage::Default can't be used with a request that \
escapes the current directory"
Expand Down Expand Up @@ -1058,7 +1061,7 @@ async fn resolve_module_request(
conditions,
unspecified_conditions,
} => {
let package_json_path = package_path.join("package.json");
let package_json_path = package_path.join("package.json".to_string());
if let ExportsFieldResult::Some(exports_field) =
&*exports_field(package_json_path).await?
{
Expand Down Expand Up @@ -1325,7 +1328,7 @@ async fn resolve_package_internal_with_imports_field(
};
// https://github.com/nodejs/node/blob/1b177932/lib/internal/modules/esm/resolve.js#L615-L619
if specifier == "#" || specifier.starts_with("#/") || specifier.ends_with('/') {
let issue: Vc<ResolvingIssue> = ResolvingIssue {
ResolvingIssue {
severity: IssueSeverity::Error.cell(),
context,
request_type: format!("package imports request: `{specifier}`"),
Expand All @@ -1334,8 +1337,8 @@ async fn resolve_package_internal_with_imports_field(
error_message: None,
source: OptionIssueSource::none(),
}
.into();
issue.as_issue().emit();
.cell()
.emit();
return Ok(ResolveResult::unresolveable().into());
}

Expand Down
4 changes: 2 additions & 2 deletions crates/turbopack-core/src/resolve/plugin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ impl ResolvePluginCondition {
#[turbo_tasks::value_trait]
pub trait ResolvePlugin {
/// A condition which determines if the hooks gets called.
fn after_resolve_condition(&self) -> Vc<ResolvePluginCondition>;
fn after_resolve_condition(self: Vc<Self>) -> Vc<ResolvePluginCondition>;

/// This hook gets called when a full filepath has been resolved and the
/// condition matches. If a value is returned it replaces the resolve
/// result.
fn after_resolve(
&self,
self: Vc<Self>,
fs_path: Vc<FileSystemPath>,
context: Vc<FileSystemPath>,
request: Vc<Request>,
Expand Down
6 changes: 0 additions & 6 deletions crates/turbopack-core/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ impl From<AssetContent> for Vc<Box<dyn VersionedContent>> {
}
}

impl From<Vc<AssetContent>> for Vc<Box<dyn VersionedContent>> {
fn from(asset_content: Vc<AssetContent>) -> Self {
Vc::upcast(VersionedAssetContent::new(asset_content))
}
}

/// Describes the current version of an object, and how to update them from an
/// earlier version.
#[turbo_tasks::value_trait]
Expand Down

0 comments on commit bda331c

Please sign in to comment.