Unverified Commit e4cae370 authored by Matt Mastracci's avatar Matt Mastracci Committed by GitHub

Turbopack: turbofmt/turbobail macros (#90092)

# What
Add turbofmt! and turbobail! proc macros for async string formatting
with Vc<T> and ResolvedVc<T> values.

A new, agent-friendly `FORMATTING.md` file is added to help agents
figure out how these macros work.

# Why

Formatting strings that include turbo-tasks values currently requires
manually .await?ing each Vc, then calling format!, then converting to
RcStr via .into(). By using macros, we can reduce a lot of boilerplate
and potentially offer some opportunities for optimizations by reducing
intermediate, throwaway tasks.

# How

We make use of the "auto-deref" pattern in Rust to choose the "best"
formatting option: ValueToString or Display.

- Zero-alloc FormatIter: A shared iterator parser over format strings
(RawString, EscapedBrace, VarRef, VarRefFormat) that both macros and the
derive use, replacing duplicated manual parsing.
- Borrow-based capture: turbofmt! uses async { .. } (not async move), so
non-Copy types like FileSystemPath can be used in multiple turbofmt!
calls without cloning.
  - Debug on StringifyType: Enables {:?} format specs in turbofmt!.
- Converted ~30 call sites across next-core, turbo-tasks-fs,
turbopack-core, turbopack-ecmascript, and others.

# What's Left

- Constants cannot be formatted with turbofmt! yet because of arcane
Rust hygiene rules (similar issue to
https://github.com/rust-lang/rust/issues/131446)
- rust-analyzer doesn't show these format strings as "real" format
strings yet because of limitations of the analyzer, but this should be
possible to make work.
- Automatic derive for ValueToStringRef to avoid intermediate tasks in a
few places where formatting is trivial
 -  turbowrite!/turbowriteln! could clean up a bit more code
parent 70a2e8d9
use anyhow::Result; use anyhow::Result;
use next_core::next_manifests::NextFontManifest; use next_core::next_manifests::NextFontManifest;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc, turbofmt};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -77,8 +77,7 @@ impl Asset for FontManifest { ...@@ -77,8 +77,7 @@ impl Asset for FontManifest {
let next_font_manifest = if !has_fonts { let next_font_manifest = if !has_fonts {
Default::default() Default::default()
} else if *app_dir { } else if *app_dir {
let dir_str = dir.value_to_string().await?; let page_path = turbofmt!("{dir}{original_name}").await?;
let page_path = format!("{dir_str}{original_name}").into();
NextFontManifest { NextFontManifest {
app: [(page_path, font_paths)].into_iter().collect(), app: [(page_path, font_paths)].into_iter().collect(),
......
...@@ -7,6 +7,7 @@ use turbo_rcstr::{RcStr, rcstr}; ...@@ -7,6 +7,7 @@ use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc, FxIndexMap, ReadRef, ResolvedVc, TryFlatJoinIterExt, TryJoinIterExt, ValueToString, Vc,
graph::{AdjacencyMap, GraphTraversal, Visit}, graph::{AdjacencyMap, GraphTraversal, Visit},
turbobail, turbofmt,
}; };
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DirectoryEntry, File, FileContent, FileSystem, FileSystemPath, DirectoryEntry, File, FileContent, FileSystem, FileSystemPath,
...@@ -286,11 +287,10 @@ impl Asset for NftJsonAsset { ...@@ -286,11 +287,10 @@ impl Asset for NftJsonAsset {
&*current_path.get_type().await?, &*current_path.get_type().await?,
FileSystemEntryType::Symlink FileSystemEntryType::Symlink
) { ) {
bail!( turbobail!(
"Encountered file inside of symlink in NFT list: {} is a symlink, \ "Encountered file inside of symlink in NFT list: {current_path} \
but {} was created inside of it", is a symlink, but {referenced_chunk_path} was created inside of \
current_path.value_to_string().await?, it"
referenced_chunk_path.value_to_string().await?
); );
} }
...@@ -307,12 +307,14 @@ impl Asset for NftJsonAsset { ...@@ -307,12 +307,14 @@ impl Asset for NftJsonAsset {
) { ) {
Ok(specifier) => specifier, Ok(specifier) => specifier,
Err(err) => { Err(err) => {
return Err(err.context(format!( return Err(err.context(
"NftJsonAsset: cannot handle filepath '{chunk_path}' for \ turbofmt!(
{referenced_chunk:?} it is not under the output_root: \ "NftJsonAsset: cannot handle filepath '{referenced_chunk_path}' \
'{output_root_ref}' or the project_root: '{project_root_ref}'", for {referenced_chunk:?} it is not under the output_root: \
chunk_path = referenced_chunk_path.value_to_string().await? '{output_root_ref}' or the project_root: '{project_root_ref}'",
))); )
.await?,
));
} }
}; };
......
use anyhow::{Result, bail}; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use next_core::emit_assets; use next_core::emit_assets;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, NonLocalValue, OperationValue, OperationVc, ResolvedVc, State, TryFlatJoinIterExt, FxIndexSet, NonLocalValue, OperationValue, OperationVc, ResolvedVc, State, TryFlatJoinIterExt,
TryJoinIterExt, ValueDefault, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, TryJoinIterExt, ValueDefault, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, turbobail,
}; };
use turbo_tasks_fs::{FileContent, FileSystemPath}; use turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
...@@ -209,8 +209,7 @@ impl VersionedContentMap { ...@@ -209,8 +209,7 @@ impl VersionedContentMap {
generate_source_map.generate_source_map() generate_source_map.generate_source_map()
}) })
} else { } else {
let path = path.value_to_string().await?; turbobail!("no source map for path {path}");
bail!("no source map for path {}", path);
} }
} }
......
...@@ -8,7 +8,7 @@ use tracing::Instrument; ...@@ -8,7 +8,7 @@ use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueDefault, Vc, FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueDefault, Vc,
debug::ValueDebugFormat, fxindexmap, trace::TraceRawVcs, debug::ValueDebugFormat, fxindexmap, trace::TraceRawVcs, turbobail,
}; };
use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath}; use turbo_tasks_fs::{DirectoryContent, DirectoryEntry, FileSystemEntryType, FileSystemPath};
use turbopack_core::issue::{ use turbopack_core::issue::{
...@@ -99,10 +99,7 @@ pub async fn get_metadata_route_name(meta: MetadataItem) -> Result<Vc<RcStr>> { ...@@ -99,10 +99,7 @@ pub async fn get_metadata_route_name(meta: MetadataItem) -> Result<Vc<RcStr>> {
MetadataItem::Static { path } => Vc::cell(path.file_name().into()), MetadataItem::Static { path } => Vc::cell(path.file_name().into()),
MetadataItem::Dynamic { path } => { MetadataItem::Dynamic { path } => {
let Some(stem) = path.file_stem() else { let Some(stem) = path.file_stem() else {
bail!( turbobail!("unable to resolve file stem for metadata item at {path}");
"unable to resolve file stem for metadata item at {}",
path.value_to_string().await?
);
}; };
match stem { match stem {
......
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result};
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{FxIndexMap, ResolvedVc, ValueToString, Vc}; use turbo_tasks::{FxIndexMap, ResolvedVc, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
...@@ -35,11 +35,7 @@ pub async fn bootstrap( ...@@ -35,11 +35,7 @@ pub async fn bootstrap(
) -> Result<Vc<Box<dyn EvaluatableAsset>>> { ) -> Result<Vc<Box<dyn EvaluatableAsset>>> {
let path = asset.ident().path().await?; let path = asset.ident().path().await?;
let Some(path) = base_path.get_path_to(&path) else { let Some(path) = base_path.get_path_to(&path) else {
bail!( turbobail!("asset {} is not in base path {base_path}", asset.ident())
"asset {} is not in base path {}",
asset.ident().to_string().await?,
base_path.value_to_string().await?
);
}; };
let path = if let Some((name, ext)) = path.rsplit_once('.') { let path = if let Some((name, ext)) = path.rsplit_once('.') {
if !ext.contains('/') { name } else { path } if !ext.contains('/') { name } else { path }
......
...@@ -2,11 +2,11 @@ ...@@ -2,11 +2,11 @@
//! //!
//! See `next/src/build/webpack/loaders/next-metadata-route-loader` //! See `next/src/build/webpack/loaders/next-metadata-route-loader`
use anyhow::{Ok, Result, bail}; use anyhow::{Ok, Result};
use base64::{display::Base64Display, engine::general_purpose::STANDARD}; use base64::{display::Base64Display, engine::general_purpose::STANDARD};
use indoc::formatdoc; use indoc::formatdoc;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::Vc; use turbo_tasks::{Vc, turbobail, turbofmt};
use turbo_tasks_fs::{self, File, FileContent, FileSystemPath}; use turbo_tasks_fs::{self, File, FileContent, FileSystemPath};
use turbopack::ModuleAssetContext; use turbopack::ModuleAssetContext;
use turbopack_core::{ use turbopack_core::{
...@@ -123,10 +123,7 @@ async fn get_base64_file_content(path: FileSystemPath) -> Result<String> { ...@@ -123,10 +123,7 @@ async fn get_base64_file_content(path: FileSystemPath) -> Result<String> {
Base64Display::new(&content, &STANDARD).to_string() Base64Display::new(&content, &STANDARD).to_string()
} }
FileContent::NotFound => { FileContent::NotFound => {
bail!( turbobail!("metadata file not found: {path}")
"metadata file not found: {}",
&path.value_to_string().await?
);
} }
}) })
} }
...@@ -570,16 +567,16 @@ impl Issue for StaticMetadataFileSizeIssue { ...@@ -570,16 +567,16 @@ impl Issue for StaticMetadataFileSizeIssue {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn description(&self) -> Result<Vc<OptionStyledString>> { async fn description(&self) -> Result<Vc<OptionStyledString>> {
let current_size = (self.file_size as f32) / 1024.0 / 1024.0;
Ok(Vc::cell(Some( Ok(Vc::cell(Some(
StyledString::Text( StyledString::Text(
format!( turbofmt!(
"File size for {} image \"{}\" exceeds {}MB. (Current: {:.1}MB)", "File size for {} image \"{}\" exceeds {}MB. (Current: {current_size:.1}MB)",
self.img_name, self.img_name,
self.path.value_to_string().await?, self.path,
self.file_size_limit_mb, self.file_size_limit_mb,
(self.file_size as f32) / 1024.0 / 1024.0
) )
.into(), .await?,
) )
.resolved_cell(), .resolved_cell(),
))) )))
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets}, chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
...@@ -50,9 +50,9 @@ impl RuntimeEntry { ...@@ -50,9 +50,9 @@ impl RuntimeEntry {
if let Some(entry) = ResolvedVc::try_downcast::<Box<dyn EvaluatableAsset>>(module) { if let Some(entry) = ResolvedVc::try_downcast::<Box<dyn EvaluatableAsset>>(module) {
runtime_entries.push(entry); runtime_entries.push(entry);
} else { } else {
bail!( turbobail!(
"runtime reference resolved to an asset ({}) that cannot be evaluated", "runtime reference resolved to an asset ({}) that cannot be evaluated",
module.ident().to_string().await? module.ident()
); );
} }
} }
......
...@@ -68,7 +68,7 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -68,7 +68,7 @@ pub async fn get_swc_ecma_transform_rule_impl(
enable_mdx_rs: bool, enable_mdx_rs: bool,
) -> Result<Option<ModuleRule>> { ) -> Result<Option<ModuleRule>> {
use anyhow::bail; use anyhow::bail;
use turbo_tasks::{TryFlatJoinIterExt, ValueToString}; use turbo_tasks::TryFlatJoinIterExt;
use turbo_tasks_fs::FileContent; use turbo_tasks_fs::FileContent;
use turbopack_core::{ use turbopack_core::{
asset::Asset, asset::Asset,
...@@ -135,11 +135,9 @@ pub async fn get_swc_ecma_transform_rule_impl( ...@@ -135,11 +135,9 @@ pub async fn get_swc_ecma_transform_rule_impl(
}; };
let Some(plugin_source) = &*plugin_module.source().await? else { let Some(plugin_source) = &*plugin_module.source().await? else {
use anyhow::bail; turbo_tasks::turbobail!(
bail!(
"Expected source for plugin module: {}", "Expected source for plugin module: {}",
plugin_module.ident().to_string().await? plugin_module.ident()
); );
}; };
......
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};
use anyhow::{Result, anyhow, bail}; use anyhow::{Result, bail};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use next_taskless::{expand_next_js_template, expand_next_js_template_no_imports}; use next_taskless::{expand_next_js_template, expand_next_js_template_no_imports};
use serde::{Deserialize, de::DeserializeOwned}; use serde::{Deserialize, de::DeserializeOwned};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, NonLocalValue, TaskInput, Vc, fxindexset, trace::TraceRawVcs}; use turbo_tasks::{
FxIndexMap, NonLocalValue, TaskInput, Vc, fxindexset, trace::TraceRawVcs, turbobail,
};
use turbo_tasks_fs::{File, FileContent, FileJsonContent, FileSystem, FileSystemPath, rope::Rope}; use turbo_tasks_fs::{File, FileContent, FileJsonContent, FileSystem, FileSystemPath, rope::Rope};
use turbopack::module_options::RuleCondition; use turbopack::module_options::RuleCondition;
use turbopack_core::{ use turbopack_core::{
...@@ -221,11 +223,7 @@ pub async fn pathname_for_path( ...@@ -221,11 +223,7 @@ pub async fn pathname_for_path(
let path = if let Some(path) = server_root.get_path_to(&server_path_value) { let path = if let Some(path) = server_root.get_path_to(&server_path_value) {
path path
} else { } else {
bail!( turbobail!("server_path ({server_path}) is not in server_root ({server_root})");
"server_path ({}) is not in server_root ({})",
server_path.value_to_string().await?,
server_root.value_to_string().await?
)
}; };
let path = match (path_ty, path) { let path = match (path_ty, path) {
// "/" is special-cased to "/index" for data routes. // "/" is special-cased to "/index" for data routes.
...@@ -482,11 +480,8 @@ pub async fn load_next_js_json_file<T: DeserializeOwned>( ...@@ -482,11 +480,8 @@ pub async fn load_next_js_json_file<T: DeserializeOwned>(
let content = &*file_path.read().await?; let content = &*file_path.read().await?;
match content.parse_json_ref() { match content.parse_json_ref() {
FileJsonContent::Unparsable(e) => Err(anyhow!("File is not valid JSON: {}", e)), FileJsonContent::Unparsable(e) => bail!("File is not valid JSON: {e}"),
FileJsonContent::NotFound => Err(anyhow!( FileJsonContent::NotFound => turbobail!("File not found: {file_path:?}",),
"File not found: {:?}",
file_path.value_to_string().await?
)),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?), FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
} }
} }
...@@ -502,11 +497,8 @@ pub async fn load_next_js_jsonc_file<T: DeserializeOwned>( ...@@ -502,11 +497,8 @@ pub async fn load_next_js_jsonc_file<T: DeserializeOwned>(
let content = &*file_path.read().await?; let content = &*file_path.read().await?;
match content.parse_json_with_comments_ref() { match content.parse_json_with_comments_ref() {
FileJsonContent::Unparsable(e) => Err(anyhow!("File is not valid JSON: {}", e)), FileJsonContent::Unparsable(e) => turbobail!("File is not valid JSON: {e}"),
FileJsonContent::NotFound => Err(anyhow!( FileJsonContent::NotFound => turbobail!("File not found: {file_path}",),
"File not found: {:?}",
file_path.value_to_string().await?
)),
FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?), FileJsonContent::Content(value) => Ok(serde_json::from_value(value)?),
} }
} }
......
...@@ -182,6 +182,12 @@ impl Borrow<str> for RcStr { ...@@ -182,6 +182,12 @@ impl Borrow<str> for RcStr {
} }
} }
impl AsRef<str> for RcStr {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl From<BytesStr> for RcStr { impl From<BytesStr> for RcStr {
fn from(s: BytesStr) -> Self { fn from(s: BytesStr) -> Self {
let bytes: Vec<u8> = s.into_bytes().into(); let bytes: Vec<u8> = s.into_bytes().into();
......
...@@ -4,8 +4,8 @@ ...@@ -4,8 +4,8 @@
use std::fmt; use std::fmt;
use turbo_rcstr::RcStr; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ReadRef, ResolvedVc, ValueToString, Vc};
use turbo_tasks_testing::{Registration, register, run_once}; use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!(); static REGISTRATION: Registration = register!();
...@@ -293,3 +293,95 @@ async fn test_mixed_enum() { ...@@ -293,3 +293,95 @@ async fn test_mixed_enum() {
.await .await
.unwrap() .unwrap()
} }
/// A Display type for torture-testing enum field resolution.
#[turbo_tasks::value(shared)]
struct DisplayVal(u32);
impl fmt::Display for DisplayVal {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "dv:{}", self.0)
}
}
/// Torture test: one enum variant with Display, RcStr, Vc, and ResolvedVc fields,
/// exercising all ValueToStringify dispatch levels in a single match arm.
#[turbo_tasks::value(shared)]
#[derive(ValueToString)]
enum TortureEnum {
/// Auto-field format with Display, RcStr, and ResolvedVc in one variant.
#[value_to_string("d={display_val} r={rc_str} rv1={resolved_named} rv2={resolved_const}")]
AllFieldTypes {
display_val: DisplayVal,
rc_str: RcStr,
resolved_named: ResolvedVc<NamedFields>,
resolved_const: ResolvedVc<ConstantString>,
},
/// FormatExprs form with a ResolvedVc positional arg.
#[value_to_string("expr({})", _0)]
ExprVc(ResolvedVc<NamedFields>),
/// DirectExpr form delegating to a ResolvedVc field.
#[value_to_string(_0)]
DelegateVc(ResolvedVc<ConstantString>),
/// ReadRef field.
#[value_to_string("read={0}")]
WithReadRef(ReadRef<NamedFields>),
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_torture_enum() {
run_once(&REGISTRATION, || async {
let named_resolved = NamedFields {
name: "x".into(),
count: 1,
}
.resolved_cell();
let named_resolved2 = NamedFields {
name: "y".into(),
count: 2,
}
.resolved_cell();
let const_resolved = ConstantString.resolved_cell();
// AllFieldTypes: Display + RcStr + ResolvedVc + ResolvedVc
let v1 = ResolvedVc::upcast(
(TortureEnum::AllFieldTypes {
display_val: DisplayVal(42),
rc_str: rcstr!("hello"),
resolved_named: named_resolved,
resolved_const: const_resolved,
})
.resolved_cell(),
);
assert_eq!(
&*to_string_operation(v1).read_strongly_consistent().await?,
"d=dv:42 r=hello rv1=item x (count: 1) rv2=constant-value"
);
// ExprVc: FormatExprs with ResolvedVc positional arg
let v2 = ResolvedVc::upcast(TortureEnum::ExprVc(named_resolved2).resolved_cell());
assert_eq!(
&*to_string_operation(v2).read_strongly_consistent().await?,
"expr(item y (count: 2))"
);
// DelegateVc: DirectExpr delegating to ResolvedVc
let v3 = ResolvedVc::upcast(TortureEnum::DelegateVc(const_resolved).resolved_cell());
assert_eq!(
&*to_string_operation(v3).read_strongly_consistent().await?,
"constant-value"
);
// WithReadRef: ReadRef field
let named_read: ReadRef<NamedFields> = named_resolved.await?;
let v4 = ResolvedVc::upcast(TortureEnum::WithReadRef(named_read).resolved_cell());
assert_eq!(
&*to_string_operation(v4).read_strongly_consistent().await?,
"read=item x (count: 1)"
);
anyhow::Ok(())
})
.await
.unwrap()
}
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![allow(clippy::needless_return)] // tokio macro-generated code doesn't respect this
use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail, turbofmt};
use turbo_tasks_testing::{Registration, register, run_once};
static REGISTRATION: Registration = register!();
#[turbo_tasks::value(shared)]
#[derive(ValueToString)]
#[value_to_string("item {name} (count: {count})")]
struct FmtTest {
name: RcStr,
count: u32,
}
#[turbo_tasks::function(operation)]
async fn turbofmt_operation(value: ResolvedVc<FmtTest>) -> anyhow::Result<Vc<RcStr>> {
let s: RcStr = turbofmt!("prefix {} vc {}", 42u32, value).await?;
Ok(Vc::cell(s))
}
#[turbo_tasks::function(operation)]
async fn turbobail_operation(value: ResolvedVc<FmtTest>) -> anyhow::Result<Vc<RcStr>> {
turbobail!("error: {} with {}", 42u32, value)
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_turbofmt() {
run_once(&REGISTRATION, || async {
let v = FmtTest {
name: "foo".into(),
count: 7,
}
.resolved_cell();
assert_eq!(
&*turbofmt_operation(v).read_strongly_consistent().await?,
"prefix 42 vc item foo (count: 7)"
);
anyhow::Ok(())
})
.await
.unwrap()
}
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn test_turbobail() {
run_once(&REGISTRATION, || async {
let v = FmtTest {
name: "bar".into(),
count: 3,
}
.resolved_cell();
let result = turbobail_operation(v).read_strongly_consistent().await;
let err = result.unwrap_err();
let root = err.root_cause().to_string();
assert_eq!(root, "error: 42 with item bar (count: 3)");
anyhow::Ok(())
})
.await
.unwrap()
}
use std::{env, sync::MutexGuard}; use std::{env, sync::MutexGuard};
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, ResolvedVc, Vc, turbofmt};
use turbo_tasks_fs::{FileContent, FileSystemPath}; use turbo_tasks_fs::{FileContent, FileSystemPath};
use crate::{GLOBAL_ENV_LOCK, ProcessEnv, TransientEnvMap, sorted_env_vars}; use crate::{GLOBAL_ENV_LOCK, ProcessEnv, TransientEnvMap, sorted_env_vars};
...@@ -62,10 +62,8 @@ impl DotenvProcessEnv { ...@@ -62,10 +62,8 @@ impl DotenvProcessEnv {
} }
if let Err(e) = res { if let Err(e) = res {
return Err(e).context(anyhow!( return Err(e)
"unable to read {} for env vars", .context(turbofmt!("unable to read {} for env vars", self.path).await?);
self.path.value_to_string().await?
));
} }
Ok(Vc::cell(vars)) Ok(Vc::cell(vars))
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail};
use crate::{FileContent, FileMeta, FileSystem, FileSystemPath, LinkContent, RawDirectoryContent}; use crate::{FileContent, FileMeta, FileSystem, FileSystemPath, LinkContent, RawDirectoryContent};
...@@ -54,12 +54,9 @@ impl AttachedFileSystem { ...@@ -54,12 +54,9 @@ impl AttachedFileSystem {
let self_fs: ResolvedVc<Box<dyn FileSystem>> = ResolvedVc::upcast(self); let self_fs: ResolvedVc<Box<dyn FileSystem>> = ResolvedVc::upcast(self);
if path.fs != self_fs { if path.fs != self_fs {
let self_fs_str = self_fs.to_string().await?; turbobail!(
let path_fs_str = path.fs.to_string().await?; "path fs does not match (expected {self_fs}, got {})",
bail!( path.fs
"path fs does not match (expected {}, got {})",
self_fs_str,
path_fs_str
) )
} }
......
...@@ -36,7 +36,7 @@ use std::{ ...@@ -36,7 +36,7 @@ use std::{
borrow::Cow, borrow::Cow,
cmp::{Ordering, min}, cmp::{Ordering, min},
env, env,
fmt::{self, Debug, Display, Formatter}, fmt::{self, Debug, Formatter},
fs::FileType, fs::FileType,
future::Future, future::Future,
io::{self, BufRead, BufReader, ErrorKind, Read, Write as _}, io::{self, BufRead, BufReader, ErrorKind, Read, Write as _},
...@@ -64,8 +64,9 @@ use tracing::Instrument; ...@@ -64,8 +64,9 @@ use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
ApplyEffectsContext, Completion, InvalidationReason, Invalidator, NonLocalValue, ReadRef, ApplyEffectsContext, Completion, InvalidationReason, Invalidator, NonLocalValue, ReadRef,
ResolvedVc, TaskInput, TurboTasksApi, ValueToString, Vc, debug::ValueDebugFormat, effect, ResolvedVc, TaskInput, TurboTasksApi, ValueToString, ValueToStringRef, Vc,
mark_session_dependent, parallel, trace::TraceRawVcs, turbo_tasks_weak, debug::ValueDebugFormat, effect, mark_session_dependent, parallel, trace::TraceRawVcs,
turbo_tasks_weak, turbobail, turbofmt,
}; };
use turbo_tasks_hash::{ use turbo_tasks_hash::{
DeterministicHash, DeterministicHasher, HashAlgorithm, deterministic_hash, hash_xxh3_hash64, DeterministicHash, DeterministicHasher, HashAlgorithm, deterministic_hash, hash_xxh3_hash64,
...@@ -976,10 +977,7 @@ impl FileSystem for DiskFileSystem { ...@@ -976,10 +977,7 @@ impl FileSystem for DiskFileSystem {
// Check if path is denied - if so, return an error // Check if path is denied - if so, return an error
if self.inner.is_path_denied(&fs_path) { if self.inner.is_path_denied(&fs_path) {
bail!( turbobail!("Cannot write to denied path: {fs_path}");
"Cannot write to denied path: {}",
fs_path.value_to_string().await?
);
} }
let full_path = self.to_sys_path(&fs_path); let full_path = self.to_sys_path(&fs_path);
...@@ -1109,10 +1107,7 @@ impl FileSystem for DiskFileSystem { ...@@ -1109,10 +1107,7 @@ impl FileSystem for DiskFileSystem {
// Check if path is denied - if so, return an error // Check if path is denied - if so, return an error
if self.inner.is_path_denied(&fs_path) { if self.inner.is_path_denied(&fs_path) {
bail!( turbobail!("Cannot write link to denied path: {fs_path}");
"Cannot write link to denied path: {}",
fs_path.value_to_string().await?
);
} }
let content = target.await?; let content = target.await?;
...@@ -1322,10 +1317,7 @@ impl FileSystem for DiskFileSystem { ...@@ -1322,10 +1317,7 @@ impl FileSystem for DiskFileSystem {
// Check if path is denied - if so, return an error (metadata shouldn't be readable) // Check if path is denied - if so, return an error (metadata shouldn't be readable)
if self.inner.is_path_denied(&fs_path) { if self.inner.is_path_denied(&fs_path) {
bail!( turbobail!("Cannot read metadata from denied path: {fs_path}");
"Cannot read metadata from denied path: {}",
fs_path.value_to_string().await?
);
} }
self.inner.register_read_invalidator(&full_path)?; self.inner.register_read_invalidator(&full_path)?;
...@@ -1372,8 +1364,7 @@ fn remove_symbolic_link_dir_helper(path: &Path) -> io::Result<()> { ...@@ -1372,8 +1364,7 @@ fn remove_symbolic_link_dir_helper(path: &Path) -> io::Result<()> {
} }
} }
#[derive(Debug, Clone, Hash, TaskInput, ValueToString)] #[derive(Debug, Clone, Hash, TaskInput)]
#[value_to_string("[{fs}]/{path}")]
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
pub struct FileSystemPath { pub struct FileSystemPath {
pub fs: ResolvedVc<Box<dyn FileSystem>>, pub fs: ResolvedVc<Box<dyn FileSystem>>,
...@@ -1383,15 +1374,22 @@ pub struct FileSystemPath { ...@@ -1383,15 +1374,22 @@ pub struct FileSystemPath {
impl FileSystemPath { impl FileSystemPath {
/// Mimics `ValueToString::to_string`. /// Mimics `ValueToString::to_string`.
pub fn value_to_string(&self) -> Vc<RcStr> { pub fn value_to_string(&self) -> Vc<RcStr> {
value_to_string(self.clone()) <FileSystemPath as ValueToString>::to_string(self.clone().cell())
} }
} }
#[turbo_tasks::function] impl ValueToStringRef for FileSystemPath {
async fn value_to_string(path: FileSystemPath) -> Result<Vc<RcStr>> { async fn to_string_ref(&self) -> Result<RcStr> {
Ok(Vc::cell( turbofmt!("[{}]/{}", self.fs, self.path).await
format!("[{}]/{}", path.fs.to_string().await?, path.path).into(), }
)) }
#[turbo_tasks::value_impl]
impl ValueToString for FileSystemPath {
#[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell(self.to_string_ref().await?))
}
} }
impl FileSystemPath { impl FileSystemPath {
...@@ -1694,9 +1692,9 @@ impl FileSystemPath { ...@@ -1694,9 +1692,9 @@ impl FileSystemPath {
} }
} }
impl Display for FileSystemPath { impl std::fmt::Display for FileSystemPath {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.path) f.write_str(&self.path)
} }
} }
...@@ -1716,7 +1714,7 @@ pub async fn rebase( ...@@ -1716,7 +1714,7 @@ pub async fn rebase(
} else { } else {
let base_path = [&old_base.path, "/"].concat(); let base_path = [&old_base.path, "/"].concat();
if !fs_path.path.starts_with(&base_path) { if !fs_path.path.starts_with(&base_path) {
bail!( turbobail!(
"rebasing {fs_path} from {old_base} onto {new_base} doesn't work because it's not \ "rebasing {fs_path} from {old_base} onto {new_base} doesn't work because it's not \
part of the source path", part of the source path",
); );
...@@ -1793,7 +1791,7 @@ impl FileSystemPath { ...@@ -1793,7 +1791,7 @@ impl FileSystemPath {
let result = &(*self.realpath_with_links().await?); let result = &(*self.realpath_with_links().await?);
match &result.path_result { match &result.path_result {
Ok(path) => Ok(path.clone()), Ok(path) => Ok(path.clone()),
Err(error) => Err(anyhow::anyhow!(error.as_error_message(self, result))), Err(error) => Err(anyhow::anyhow!(error.as_error_message(self, result).await?)),
} }
} }
...@@ -1859,22 +1857,33 @@ pub enum RealPathResultError { ...@@ -1859,22 +1857,33 @@ pub enum RealPathResultError {
impl RealPathResultError { impl RealPathResultError {
/// Formats the error message /// Formats the error message
pub fn as_error_message(&self, orig: &FileSystemPath, result: &RealPathResult) -> String { pub async fn as_error_message(
match self { &self,
RealPathResultError::TooManySymlinks => format!( orig: &FileSystemPath,
"Symlink {orig} leads to too many other symlinks ({len} links)", result: &RealPathResult,
len = result.symlinks.len() ) -> Result<RcStr> {
), Ok(match self {
RealPathResultError::TooManySymlinks => {
let len = result.symlinks.len();
turbofmt!("Symlink {orig} leads to too many other symlinks ({len} links)").await?
}
RealPathResultError::CycleDetected => { RealPathResultError::CycleDetected => {
format!("Symlink {orig} is in a symlink loop: {:?}", result.symlinks) // symlinks is Vec<FileSystemPath> — format with Debug since
// turbofmt can't resolve a whole Vec asynchronously.
let symlinks_dbg = format!(
"{:?}",
result.symlinks.iter().map(|s| &s.path).collect::<Vec<_>>()
);
turbofmt!("Symlink {orig} is in a symlink loop: {symlinks_dbg}").await?
} }
RealPathResultError::Invalid => { RealPathResultError::Invalid => {
format!("Symlink {orig} is invalid, it points out of the filesystem root") turbofmt!("Symlink {orig} is invalid, it points out of the filesystem root").await?
} }
RealPathResultError::NotFound => { RealPathResultError::NotFound => {
format!("Symlink {orig} is invalid, it points at a file that doesn't exist") turbofmt!("Symlink {orig} is invalid, it points at a file that doesn't exist")
.await?
} }
} })
} }
} }
...@@ -2491,7 +2500,7 @@ impl DirectoryEntry { ...@@ -2491,7 +2500,7 @@ impl DirectoryEntry {
Ok(path) => path, Ok(path) => path,
Err(error) => { Err(error) => {
return Ok(DirectoryEntry::Error( return Ok(DirectoryEntry::Error(
error.as_error_message(symlink, result).into(), error.as_error_message(symlink, result).await?,
)); ));
} }
}; };
...@@ -2500,10 +2509,11 @@ impl DirectoryEntry { ...@@ -2500,10 +2509,11 @@ impl DirectoryEntry {
FileSystemEntryType::File => DirectoryEntry::File(real_path.clone()), FileSystemEntryType::File => DirectoryEntry::File(real_path.clone()),
// Happens if the link is to a non-existent file // Happens if the link is to a non-existent file
FileSystemEntryType::NotFound => DirectoryEntry::Error( FileSystemEntryType::NotFound => DirectoryEntry::Error(
format!("Symlink {symlink} points at {real_path} which does not exist").into(), turbofmt!("Symlink {symlink} points at {real_path} which does not exist")
.await?,
), ),
// This is caused by eventual consistency // This is caused by eventual consistency
FileSystemEntryType::Symlink => bail!( FileSystemEntryType::Symlink => turbobail!(
"Symlink {symlink} points at a symlink but realpath_with_links returned a path" "Symlink {symlink} points at a symlink but realpath_with_links returned a path"
), ),
_ => self, _ => self,
......
...@@ -2,7 +2,7 @@ use anyhow::{Result, bail}; ...@@ -2,7 +2,7 @@ use anyhow::{Result, bail};
use futures::try_join; use futures::try_join;
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{Completion, ResolvedVc, TryJoinIterExt, Vc}; use turbo_tasks::{Completion, ResolvedVc, TryJoinIterExt, Vc, turbobail};
use crate::{ use crate::{
DirectoryContent, DirectoryEntry, FileSystem, FileSystemPath, LinkContent, LinkType, glob::Glob, DirectoryContent, DirectoryEntry, FileSystem, FileSystemPath, LinkContent, LinkType, glob::Glob,
...@@ -122,10 +122,7 @@ async fn resolve_symlink_safely(entry: DirectoryEntry) -> Result<DirectoryEntry> ...@@ -122,10 +122,7 @@ async fn resolve_symlink_safely(entry: DirectoryEntry) -> Result<DirectoryEntry>
// match. // match.
let source_path = entry.path().unwrap(); let source_path = entry.path().unwrap();
if source_path.is_inside_or_equal(&resolved_entry.clone().path().unwrap()) { if source_path.is_inside_or_equal(&resolved_entry.clone().path().unwrap()) {
bail!( bail!("'{source_path}' is a symlink causes that causes an infinite loop!",)
"'{}' is a symlink causes that causes an infinite loop!",
source_path.path,
)
} }
} }
Ok(resolved_entry) Ok(resolved_entry)
...@@ -197,12 +194,10 @@ async fn track_glob_internal( ...@@ -197,12 +194,10 @@ async fn track_glob_internal(
reads.push(fs.read(path.clone())) reads.push(fs.read(path.clone()))
} }
} }
DirectoryEntry::Symlink(symlink_path) => bail!( DirectoryEntry::Symlink(symlink_path) => turbobail!(
"resolve_symlink_safely() should have resolved all symlinks or returned \ "resolve_symlink_safely() should have resolved all symlinks or returned \
an error, but found unresolved symlink at path: '{}'. Found path: '{}'. \ an error, but found unresolved symlink at path: '{entry_path}'. Found \
Please report this as a bug.", path: '{symlink_path}'. Please report this as a bug.",
entry_path,
symlink_path
), ),
DirectoryEntry::Other(path) => { DirectoryEntry::Other(path) => {
if glob_value.matches(&entry_path) { if glob_value.matches(&entry_path) {
......
...@@ -17,6 +17,7 @@ mod expand; ...@@ -17,6 +17,7 @@ mod expand;
mod ident; mod ident;
mod primitive_input; mod primitive_input;
mod self_filter; mod self_filter;
mod turbofmt_macro;
mod value_trait_arguments; mod value_trait_arguments;
use proc_macro::TokenStream; use proc_macro::TokenStream;
...@@ -57,6 +58,8 @@ pub fn derive_task_input(input: TokenStream) -> TokenStream { ...@@ -57,6 +58,8 @@ pub fn derive_task_input(input: TokenStream) -> TokenStream {
derive::derive_task_input(input) derive::derive_task_input(input)
} }
/// Derive macro for `ValueToString`. Also generates `ValueToStringify for &T`.
#[doc = include_str!("../../turbo-tasks/FORMATTING.md")]
#[proc_macro_derive(ValueToString, attributes(value_to_string))] #[proc_macro_derive(ValueToString, attributes(value_to_string))]
pub fn derive_value_to_string(input: TokenStream) -> TokenStream { pub fn derive_value_to_string(input: TokenStream) -> TokenStream {
derive::value_to_string_macro::derive_value_to_string(input) derive::value_to_string_macro::derive_value_to_string(input)
...@@ -120,3 +123,25 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream { ...@@ -120,3 +123,25 @@ pub fn value_impl(args: TokenStream, input: TokenStream) -> TokenStream {
pub fn primitive(input: TokenStream) -> TokenStream { pub fn primitive(input: TokenStream) -> TokenStream {
primitive_macro::primitive(input) primitive_macro::primitive(input)
} }
/// Async format macro. Returns `impl Future<Output = Result<RcStr>>`.
///
/// ```ignore
/// let s: RcStr = turbofmt!("asset {} in path {}", asset.ident(), base_path).await?;
/// ```
#[doc = include_str!("../../turbo-tasks/FORMATTING.md")]
#[proc_macro]
pub fn turbofmt(input: TokenStream) -> TokenStream {
turbofmt_macro::turbofmt(input)
}
/// Async bail macro. Resolves arguments then calls `anyhow::bail!()`.
///
/// ```ignore
/// turbobail!("asset {} is not in path {}", asset.ident(), base_path);
/// ```
#[doc = include_str!("../../turbo-tasks/FORMATTING.md")]
#[proc_macro]
pub fn turbobail(input: TokenStream) -> TokenStream {
turbofmt_macro::turbobail(input)
}
This diff is collapsed.
# Async Formatting
Standard Rust formatting (`format!`, `Display`) is synchronous. In turbo-tasks,
many values live behind `Vc<T>` or `ResolvedVc<T>`, which require `.await` to
read. These macros handle that automatically.
## `turbofmt!` — async `format!`
Returns `Result<RcStr>` after resolving all arguments asynchronously.
```rust
// Positional arguments
let msg = turbofmt!("asset {} in path {}", asset.ident(), base_path).await?;
// Captured variables
let name = some_vc;
let msg = turbofmt!("hello {name}").await?;
```
Arguments can be `Vc<T>`, `ResolvedVc<T>`, `ReadRef<T>`, or any type
implementing `Display` — they are all resolved automatically.
Arguments with format specifiers (e.g., `{x:?}`) use standard `Debug`/`Display`
formatting directly and are **not** resolved asynchronously.
## `turbobail!` — async `bail!`
Same as `turbofmt!`, but calls `anyhow::bail!()` with the formatted message.
Has implicit `.await` and return flow.
```rust
turbobail!("asset {} is not in path {}", asset.ident(), base_path);
```
## `#[derive(ValueToString)]`
Generates both `ValueToStringRef` (returning `RcStr`) and `ValueToString`
(returning `Vc<RcStr>`) impls. Fields are resolved asynchronously, so `Vc<T>`
and `ResolvedVc<T>` fields work directly in format strings.
**No attribute** — delegates to `Display::to_string()`:
```rust
#[derive(ValueToString)]
struct Foo { ... } // requires Display impl
```
**Format string with field references** — fields are resolved async:
```rust
#[derive(ValueToString)]
#[value_to_string("[{fs}]/{path}")]
struct FileSystemPath { fs: ResolvedVc<...>, path: RcStr }
```
**Format string with explicit expressions:**
```rust
#[derive(ValueToString)]
#[value_to_string("{}", self.name())]
struct Bar { ... }
```
**Direct expression:**
```rust
#[derive(ValueToString)]
#[value_to_string(self.inner)]
struct Wrapper { inner: RcStr }
```
**Enums** — variants without an attribute default to their name:
```rust
#[derive(ValueToString)]
enum Kind {
Foo, // → "Foo"
#[value_to_string("custom")]
Bar, // → "custom"
#[value_to_string("value is {0}")]
Baz(ResolvedVc<RcStr>), // → "value is ..."
}
```
## `ValueToString` trait
The underlying async trait. You rarely need to implement this manually —
prefer `#[derive(ValueToString)]` instead.
```rust
#[turbo_tasks::value_trait]
pub trait ValueToString {
fn to_string(self: Vc<Self>) -> Vc<RcStr>;
}
```
## Resolution Priority
When a value is used in `turbofmt!` or `turbobail!`, it is resolved using the
first matching rule (highest priority first):
| Priority | Type | Resolution |
| -------- | ------------------------- | ------------------------------------------ |
| 1 | `ValueToStringRef` impl | Awaits `ValueToStringRef::to_string_ref()` |
| 2 | `Vc<T>` / `ResolvedVc<T>` | Awaits `ValueToString::to_string()` |
| 3 | `Display` impl | Synchronous `Display::to_string()` |
`#[derive(ValueToString)]` generates both `ValueToStringRef` and
`ValueToString` impls, so derived types resolve at priority 1 when used as
owned values and at priority 2 when used behind `Vc`/`ResolvedVc`.
A type may implement `Display` for a short synchronous representation while
`ValueToStringRef` provides a richer async format, but this is strongly discouraged
and causes confusion. In general, only the sync _XOR_ async formatting functions should be implemented.
use std::{ use std::future::Future;
fmt::{self, Display},
future::Future,
};
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::Vc; use turbo_tasks::Vc;
pub use turbo_tasks_macros::ValueToString; pub use turbo_tasks_macros::ValueToString;
use crate::{self as turbo_tasks, ReadRef, vc::ResolvedVc}; use crate::{self as turbo_tasks};
/// Converts a value to a string, like [`Display`], but returning `Vc<RcStr>`. /// Async counterpart to `Display`, returning `Vc<RcStr>`.
///
/// Use `#[derive(ValueToString)]` to generate an implementation.
#[doc = include_str!("../FORMATTING.md")]
#[turbo_tasks::value_trait] #[turbo_tasks::value_trait]
pub trait ValueToString { pub trait ValueToString {
#[turbo_tasks::function] #[turbo_tasks::function]
fn to_string(self: Vc<Self>) -> Vc<RcStr>; fn to_string(self: Vc<Self>) -> Vc<RcStr>;
} }
/// A helper trait used by the `#[derive(ValueToString)]` macro. /// Implements an async counterpart to `Display`, returning `RcStr`. This may
/// /// act as an optimization.
/// Provides async string conversion with a blanket implementation for `Display` pub trait ValueToStringRef {
/// types. `Vc<T>` and `ResolvedVc<T>` have specialized implementations that fn to_string_ref(&self) -> impl Future<Output = Result<RcStr>> + Send;
/// await the inner value's `ValueToString` implementation.
///
/// Note that these methods are inlined and these function calls are only used for
/// effecient macro codegen.
#[doc(hidden)]
pub trait ValueToStringify {
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send;
} }
/// Used only for macro codegen. /// Ref-following: `&T` delegates to `T`'s `ValueToStringRef`.
#[doc(hidden)] impl<T: ValueToStringRef + Sync> ValueToStringRef for &T {
pub enum StringifyType { fn to_string_ref(&self) -> impl Future<Output = Result<RcStr>> + Send {
RcStr(ReadRef<RcStr>), (**self).to_string_ref()
String(String), }
} }
impl AsRef<str> for StringifyType { /// Identity implementation: `RcStr` just returns itself.
fn as_ref(&self) -> &str { #[turbo_tasks::value_impl]
match self { impl ValueToString for RcStr {
StringifyType::RcStr(s) => s.as_str(), #[turbo_tasks::function]
StringifyType::String(s) => s.as_str(), fn to_string(self: Vc<Self>) -> Vc<RcStr> {
} self
} }
} }
impl Display for StringifyType { /// Identity implementation: `RcStr` just returns itself.
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { impl ValueToStringRef for RcStr {
f.write_str(self.as_ref()) async fn to_string_ref(&self) -> Result<RcStr> {
Ok(self.clone())
} }
} }
impl From<StringifyType> for RcStr { /// Deref-following: `ReadRef<T>` delegates to the deref target's `ValueToStringRef`.
fn from(s: StringifyType) -> Self { impl<T> ValueToStringRef for crate::ReadRef<T>
match s { where
StringifyType::RcStr(r) => (*r).clone(), T: crate::VcValueType,
StringifyType::String(s) => RcStr::from(s), <T::Read as crate::VcRead<T>>::Target: ValueToStringRef,
} {
fn to_string_ref(&self) -> impl Future<Output = Result<RcStr>> + Send {
(**self).to_string_ref()
} }
} }
/// Blanket implementation for all `Display` types. /// Part of the auto-deref specialization system.
impl<T: Display + Send + Sync> ValueToStringify for T { #[doc(hidden)]
#[inline(always)] #[macro_export]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send { macro_rules! __turbo_stringify {
let s = self.to_string(); ($name:ident, $i:expr) => {
async move { Ok(StringifyType::String(s)) } // Ugh: https://sabrinajewson.org/blog/truly-hygienic-let
} // This "let mut" makes errors more obvious in this case
let mut $name: $crate::display::macro_helpers::StringifyType = {
use $crate::display::macro_helpers::ValueToStringify as _;
let tmp = $crate::display::macro_helpers::ValueToStringifyWrap($i);
(&&&tmp).to_stringify().await?
};
};
} }
/// Implementation for `Vc<T>` that awaits the turbo-tasks `ValueToString` result. /// Runtime helpers for the `turbofmt!`/`turbobail!` macros. Not part of the
impl<T: Send> ValueToStringify for Vc<T> /// public API.
where #[doc(hidden)]
T: ValueToString, pub mod macro_helpers {
{ use std::{
#[inline(always)] fmt::{self, Display},
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send { future::Future,
let vc = *self; };
async move {
let s = vc.to_string().await?; use anyhow::Result;
Ok(StringifyType::RcStr(s)) use turbo_rcstr::RcStr;
use super::{ValueToString, ValueToStringRef};
use crate::vc::ResolvedVc;
pub struct ValueToStringifyWrap<T>(pub T);
pub trait ValueToStringify<const LEVEL: u8> {
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send;
}
pub enum StringifyType {
RcStr(RcStr),
String(String),
}
impl AsRef<str> for StringifyType {
fn as_ref(&self) -> &str {
match self {
StringifyType::RcStr(s) => s.as_str(),
StringifyType::String(s) => s.as_str(),
}
} }
} }
}
/// Implementation for `ResolvedVc<T>` that delegates to the `Vc<T>` implementation. impl fmt::Debug for StringifyType {
impl<T: Send> ValueToStringify for ResolvedVc<T> fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
where fmt::Debug::fmt(self.as_ref(), f)
T: ValueToString, }
{ }
#[inline(always)]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send { impl Display for StringifyType {
let vc = *self; fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
async move { f.write_str(self.as_ref())
let s = vc.to_string().await?; }
Ok(StringifyType::RcStr(s)) }
impl From<StringifyType> for RcStr {
fn from(s: StringifyType) -> Self {
match s {
StringifyType::RcStr(r) => r,
StringifyType::String(s) => RcStr::from(s),
}
}
}
/// Blanket impl: uses synchronous `Display::to_string()` for owned values.
impl<T: Display + Send + Sync> ValueToStringify<1> for &ValueToStringifyWrap<&T> {
#[inline(always)]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send {
let s = (self.0).to_string();
async move { Ok(StringifyType::String(s)) }
}
}
impl<T: Send> ValueToStringify<2> for &&ValueToStringifyWrap<&crate::Vc<T>>
where
T: ValueToString,
{
#[inline(always)]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send {
let vc = self.0;
async move {
let s = vc.to_string().await?;
Ok(StringifyType::RcStr((*s).clone()))
}
}
}
impl<T: Send> ValueToStringify<2> for &&ValueToStringifyWrap<&ResolvedVc<T>>
where
T: ValueToString,
{
#[inline(always)]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> + Send {
let vc = self.0;
async move {
let s = vc.to_string().await?;
Ok(StringifyType::RcStr((*s).clone()))
}
}
}
impl<T: Send> ValueToStringify<2> for &&&ValueToStringifyWrap<&T>
where
T: ValueToStringRef,
{
#[inline(always)]
fn to_stringify(&self) -> impl Future<Output = Result<StringifyType>> {
let s = self.0.to_string_ref();
async move { Ok(StringifyType::RcStr(s.await?)) }
} }
} }
} }
...@@ -46,6 +46,7 @@ mod capture_future; ...@@ -46,6 +46,7 @@ mod capture_future;
mod collectibles; mod collectibles;
mod completion; mod completion;
pub mod debug; pub mod debug;
#[doc = include_str!("../FORMATTING.md")]
pub mod display; pub mod display;
pub mod duration_span; pub mod duration_span;
mod effect; mod effect;
...@@ -97,13 +98,13 @@ pub use anyhow::{Error, Result}; ...@@ -97,13 +98,13 @@ pub use anyhow::{Error, Result};
use auto_hash_map::AutoSet; use auto_hash_map::AutoSet;
use rustc_hash::FxHasher; use rustc_hash::FxHasher;
pub use shrink_to_fit::ShrinkToFit; pub use shrink_to_fit::ShrinkToFit;
pub use turbo_tasks_macros::{TaskInput, function, value_impl}; pub use turbo_tasks_macros::{TaskInput, function, turbobail, turbofmt, value_impl};
pub use crate::{ pub use crate::{
capture_future::TurboTasksPanic, capture_future::TurboTasksPanic,
collectibles::CollectiblesSource, collectibles::CollectiblesSource,
completion::{Completion, Completions}, completion::{Completion, Completions},
display::ValueToString, display::{ValueToString, ValueToStringRef},
effect::{ApplyEffectsContext, Effects, apply_effects, effect, get_effects}, effect::{ApplyEffectsContext, Effects, apply_effects, effect, get_effects},
error::PrettyPrintError, error::PrettyPrintError,
id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId}, id::{ExecutionId, LocalTaskId, TRANSIENT_TASK_BIT, TaskId, TraitTypeId, ValueTypeId},
......
use std::io::Write; use std::io::Write;
use anyhow::{Result, bail}; use anyhow::Result;
use either::Either; use either::Either;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, Vc}; use turbo_tasks::{ResolvedVc, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent}; use turbo_tasks_fs::{File, FileContent};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
...@@ -84,7 +84,7 @@ impl EcmascriptBrowserChunkContent { ...@@ -84,7 +84,7 @@ impl EcmascriptBrowserChunkContent {
let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
path path
} else { } else {
bail!("chunk path {chunk_path} is not in output root {output_root}"); turbobail!("chunk path {chunk_path} is not in output root {output_root}");
}; };
Either::Left(StringifyJs(chunk_server_path)) Either::Left(StringifyJs(chunk_server_path))
} }
......
use std::io::Write; use std::io::Write;
use anyhow::{Result, bail}; use anyhow::Result;
use either::Either; use either::Either;
use indoc::writedoc; use indoc::writedoc;
use serde::Serialize; use serde::Serialize;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -101,7 +101,7 @@ impl EcmascriptBrowserEvaluateChunk { ...@@ -101,7 +101,7 @@ impl EcmascriptBrowserEvaluateChunk {
let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_server_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
path path
} else { } else {
bail!("chunk path {chunk_path} is not in output root {output_root}"); turbobail!("chunk path {chunk_path} is not in output root {output_root}");
}; };
Either::Left(StringifyJs(chunk_server_path)) Either::Left(StringifyJs(chunk_server_path))
} }
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, Vc}; use turbo_tasks::{FxIndexMap, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex}; use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex};
use turbopack_core::{chunk::ModuleId, version::Version}; use turbopack_core::{chunk::ModuleId, version::Version};
...@@ -27,7 +27,7 @@ impl EcmascriptBrowserChunkVersion { ...@@ -27,7 +27,7 @@ impl EcmascriptBrowserChunkVersion {
let chunk_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
path path
} else { } else {
bail!("chunk path {chunk_path} is not in client root {output_root}"); turbobail!("chunk path {chunk_path} is not in client root {output_root}");
}; };
let entries = EcmascriptBrowserChunkContentEntries::new(content).await?; let entries = EcmascriptBrowserChunkContentEntries::new(content).await?;
let mut entries_hashes = let mut entries_hashes =
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets}, chunk::{EvaluatableAsset, EvaluatableAssetExt, EvaluatableAssets},
...@@ -50,9 +50,9 @@ impl RuntimeEntry { ...@@ -50,9 +50,9 @@ impl RuntimeEntry {
if let Some(entry) = ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module) { if let Some(entry) = ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(module) {
runtime_entries.push(entry); runtime_entries.push(entry);
} else { } else {
bail!( turbobail!(
"runtime reference resolved to an asset ({}) that cannot be evaluated", "runtime reference resolved to an asset ({}) that cannot be evaluated",
module.ident().to_string().await? module.ident()
); );
} }
} }
......
...@@ -2,7 +2,7 @@ use anyhow::Result; ...@@ -2,7 +2,7 @@ use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, Vc, FxIndexSet, NonLocalValue, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, Vc,
trace::TraceRawVcs, trace::TraceRawVcs, turbofmt,
}; };
use turbo_tasks_hash::Xxh3Hash64Hasher; use turbo_tasks_hash::Xxh3Hash64Hasher;
...@@ -30,9 +30,9 @@ impl AvailableModuleItem { ...@@ -30,9 +30,9 @@ impl AvailableModuleItem {
AvailableModuleItem::Batch(batch) => { AvailableModuleItem::Batch(batch) => {
IdentStrings::Multiple(batch.ident_strings().await?) IdentStrings::Multiple(batch.ident_strings().await?)
} }
AvailableModuleItem::AsyncLoader(module) => IdentStrings::Single( AvailableModuleItem::AsyncLoader(module) => {
format!("async loader {}", module.ident().to_string().await?).into(), IdentStrings::Single(turbofmt!("async loader {}", module.ident()).await?)
), }
}) })
} }
} }
......
use anyhow::{Result, bail}; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use rustc_hash::FxHashMap; use rustc_hash::FxHashMap;
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, ResolvedVc, ValueToString, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, NonLocalValue, ResolvedVc, ValueToString, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
turbobail,
}; };
use turbo_tasks_hash::hash_xxh3_hash64; use turbo_tasks_hash::hash_xxh3_hash64;
...@@ -61,7 +62,7 @@ impl ModuleIdStrategy { ...@@ -61,7 +62,7 @@ impl ModuleIdStrategy {
)); ));
} }
bail!("ModuleId not found for ident: {}", ident.to_string().await?); turbobail!("ModuleId not found for ident: {}", ident);
} }
ModuleIdFallback::Ident => Ok(ModuleId::String(ident.to_string().owned().await?)), ModuleIdFallback::Ident => Ok(ModuleId::String(ident.to_string().owned().await?)),
} }
......
use anyhow::{Result, bail}; use anyhow::Result;
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{NonLocalValue, ResolvedVc, TaskInput, Upcast, Vc, trace::TraceRawVcs}; use turbo_tasks::{
NonLocalValue, ResolvedVc, TaskInput, Upcast, Vc, trace::TraceRawVcs, turbobail,
};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::DeterministicHash; use turbo_tasks_hash::DeterministicHash;
...@@ -488,10 +490,7 @@ pub trait ChunkingContext { ...@@ -488,10 +490,7 @@ pub trait ChunkingContext {
/// Returns the worker entrypoint for this chunking context. /// Returns the worker entrypoint for this chunking context.
#[turbo_tasks::function] #[turbo_tasks::function]
async fn worker_entrypoint(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> { async fn worker_entrypoint(self: Vc<Self>) -> Result<Vc<Box<dyn OutputAsset>>> {
bail!( turbobail!("Worker entrypoint is not supported by {}", self.name());
"Worker entrypoint is not supported by {name}",
name = self.name().await?
);
} }
} }
pub trait ChunkingContextExt { pub trait ChunkingContextExt {
...@@ -716,11 +715,10 @@ async fn relative_path_from_chunk_root_to_project_root( ...@@ -716,11 +715,10 @@ async fn relative_path_from_chunk_root_to_project_root(
let output_root = chunking_context.output_root().await?; let output_root = chunking_context.output_root().await?;
let chunk_to_output_root = chunk_root_path.get_relative_path_to(&output_root); let chunk_to_output_root = chunk_root_path.get_relative_path_to(&output_root);
let Some(chunk_to_output_root) = chunk_to_output_root else { let Some(chunk_to_output_root) = chunk_to_output_root else {
bail!( turbobail!(
"expected chunk_root_path: {chunk_root_path} to be inside of output_root: \ "expected chunk_root_path: {} to be inside of output_root: {}",
{output_root}", chunking_context.chunk_root_path(),
chunk_root_path = chunk_root_path.value_to_string().await?, chunking_context.output_root()
output_root = output_root.value_to_string().await?
); );
}; };
let output_root_to_chunk_root_path = chunking_context.output_root_to_root_path().await?; let output_root_to_chunk_root_path = chunking_context.output_root_to_root_path().await?;
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_tasks::{ResolvedVc, Upcast, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, Upcast, Vc, turbobail};
use super::ChunkableModule; use super::ChunkableModule;
use crate::{ use crate::{
...@@ -46,10 +46,7 @@ async fn to_evaluatable( ...@@ -46,10 +46,7 @@ async fn to_evaluatable(
let Some(entry) = let Some(entry) =
ResolvedVc::try_downcast::<Box<dyn EvaluatableAsset>>(module.to_resolved().await?) ResolvedVc::try_downcast::<Box<dyn EvaluatableAsset>>(module.to_resolved().await?)
else { else {
bail!( turbobail!("{} is not a valid evaluated entry", module.ident());
"{} is not a valid evaluated entry",
module.ident().to_string().await?
)
}; };
Ok(*entry) Ok(*entry)
} }
......
...@@ -6,7 +6,7 @@ use once_cell::sync::Lazy; ...@@ -6,7 +6,7 @@ use once_cell::sync::Lazy;
use regex::Regex; use regex::Regex;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, ReadRef, ResolvedVc, TaskInput, ValueToString, Vc, trace::TraceRawVcs, NonLocalValue, ReadRef, ResolvedVc, TaskInput, ValueToString, Vc, trace::TraceRawVcs, turbofmt,
}; };
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher, encode_hex, hash_xxh3_hash64}; use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher, encode_hex, hash_xxh3_hash64};
...@@ -376,12 +376,11 @@ impl AssetIdent { ...@@ -376,12 +376,11 @@ impl AssetIdent {
impl ValueToString for AssetIdent { impl ValueToString for AssetIdent {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> { async fn to_string(&self) -> Result<Vc<RcStr>> {
let mut s = self.path.value_to_string().owned().await?.into_owned(); // The query string/fragment is either empty or non-empty starting with
// `?` so we can just concat
// The query string is either empty or non-empty starting with `?` so we can just concat let mut s = turbofmt!("{}{}{}", self.path, self.query, self.fragment)
s.push_str(&self.query); .await?
// ditto for fragment .into_owned();
s.push_str(&self.fragment);
if !self.assets.is_empty() { if !self.assets.is_empty() {
s.push_str(" {"); s.push_str(" {");
......
...@@ -2,7 +2,7 @@ use std::fmt::Write; ...@@ -2,7 +2,7 @@ use std::fmt::Write;
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{PrettyPrintError, ReadRef, ResolvedVc, ValueToString, Vc}; use turbo_tasks::{PrettyPrintError, ReadRef, ResolvedVc, ValueToString, ValueToStringRef, Vc};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use super::{Issue, IssueSource, IssueStage, OptionStyledString, StyledString}; use super::{Issue, IssueSource, IssueStage, OptionStyledString, StyledString};
...@@ -101,7 +101,7 @@ impl Issue for ResolvingIssue { ...@@ -101,7 +101,7 @@ impl Issue for ResolvingIssue {
writeln!( writeln!(
detail, detail,
"Path where resolving has started: {context}", "Path where resolving has started: {context}",
context = self.file_path.value_to_string().await? context = self.file_path.to_string_ref().await?
)?; )?;
writeln!( writeln!(
detail, detail,
......
...@@ -13,7 +13,7 @@ use tracing::Instrument; ...@@ -13,7 +13,7 @@ use tracing::Instrument;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString,
Vc, debug::ValueDebugFormat, trace::TraceRawVcs, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, turbofmt,
}; };
use crate::{ use crate::{
...@@ -228,18 +228,15 @@ impl ChunkGroup { ...@@ -228,18 +228,15 @@ impl ChunkGroup {
.try_join() .try_join()
.await? .await?
), ),
ChunkGroup::Async(entry) => { ChunkGroup::Async(entry) => turbofmt!("ChunkGroup::Async({:?})", entry.ident())
format!("ChunkGroup::Async({:?})", entry.ident().to_string().await?) .await?
} .to_string(),
ChunkGroup::Isolated(entry) => { ChunkGroup::Isolated(entry) => turbofmt!("ChunkGroup::Isolated({:?})", entry.ident())
format!( .await?
"ChunkGroup::Isolated({:?})", .to_string(),
entry.ident().to_string().await? ChunkGroup::Shared(entry) => turbofmt!("ChunkGroup::Shared({:?})", entry.ident())
) .await?
} .to_string(),
ChunkGroup::Shared(entry) => {
format!("ChunkGroup::Shared({:?})", entry.ident().to_string().await?)
}
ChunkGroup::IsolatedMerged { ChunkGroup::IsolatedMerged {
parent, parent,
merge_tag, merge_tag,
...@@ -315,11 +312,11 @@ impl ChunkGroupKey { ...@@ -315,11 +312,11 @@ impl ChunkGroupKey {
.await? .await?
), ),
ChunkGroupKey::Async(module) => { ChunkGroupKey::Async(module) => {
format!("Async({:?})", module.ident().to_string().await?) turbofmt!("Async({:?})", module.ident()).await?.to_string()
}
ChunkGroupKey::Isolated(module) => {
format!("Isolated({:?})", module.ident().to_string().await?)
} }
ChunkGroupKey::Isolated(module) => turbofmt!("Isolated({:?})", module.ident())
.await?
.to_string(),
ChunkGroupKey::IsolatedMerged { parent, merge_tag } => { ChunkGroupKey::IsolatedMerged { parent, merge_tag } => {
format!( format!(
"IsolatedMerged {{ parent: {}, merge_tag: {:?} }}", "IsolatedMerged {{ parent: {}, merge_tag: {:?} }}",
...@@ -328,7 +325,7 @@ impl ChunkGroupKey { ...@@ -328,7 +325,7 @@ impl ChunkGroupKey {
) )
} }
ChunkGroupKey::Shared(module) => { ChunkGroupKey::Shared(module) => {
format!("Shared({:?})", module.ident().to_string().await?) turbofmt!("Shared({:?})", module.ident()).await?.to_string()
} }
ChunkGroupKey::SharedMerged { parent, merge_tag } => { ChunkGroupKey::SharedMerged { parent, merge_tag } => {
format!( format!(
......
...@@ -4,7 +4,7 @@ use std::{ ...@@ -4,7 +4,7 @@ use std::{
mem::take, mem::take,
}; };
use anyhow::{Context, Result, bail}; use anyhow::{Context, Result};
use bincode::{Decode, Encode}; use bincode::{Decode, Encode};
use either::Either; use either::Either;
use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex}; use petgraph::graph::{DiGraph, EdgeIndex, NodeIndex};
...@@ -14,7 +14,7 @@ use tracing::Instrument; ...@@ -14,7 +14,7 @@ use tracing::Instrument;
use turbo_prehash::BuildHasherExt; use turbo_prehash::BuildHasherExt;
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, FxIndexMap, FxIndexSet, NonLocalValue, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString,
Vc, trace::TraceRawVcs, Vc, trace::TraceRawVcs, turbobail,
}; };
use crate::{ use crate::{
...@@ -79,15 +79,19 @@ pub struct ModuleBatchesGraph { ...@@ -79,15 +79,19 @@ pub struct ModuleBatchesGraph {
impl ModuleBatchesGraph { impl ModuleBatchesGraph {
pub async fn get_entry_index(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<NodeIndex> { pub async fn get_entry_index(&self, entry: ResolvedVc<Box<dyn Module>>) -> Result<NodeIndex> {
let Some(entry) = self.entries.get(&entry) else { let Some(entry) = self.entries.get(&entry) else {
bail!( let possible_entries = format!(
"Entry {} is not in graph (possible entries: {:#?})", "{:#?}",
entry.ident().to_string().await?,
self.entries self.entries
.keys() .keys()
.map(|e| e.ident().to_string()) .map(|e| e.ident().to_string())
.try_join() .try_join()
.await? .await?
); );
turbobail!(
"Entry {} is not in graph (possible entries: {})",
entry.ident(),
possible_entries
);
}; };
Ok(*entry) Ok(*entry)
} }
......
...@@ -127,7 +127,7 @@ async fn dir_references(package_dir: FileSystemPath) -> Result<Vc<ModuleReferenc ...@@ -127,7 +127,7 @@ async fn dir_references(package_dir: FileSystemPath) -> Result<Vc<ModuleReferenc
Ok(path) => { Ok(path) => {
results.insert(path.clone()); results.insert(path.clone());
} }
Err(e) => bail!(e.as_error_message(file, &realpath)), Err(e) => bail!(e.as_error_message(file, &realpath).await?),
} }
} }
PatternMatch::Directory(..) => {} PatternMatch::Directory(..) => {}
......
use std::hash::Hash; use std::hash::Hash;
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use crate::{ use crate::{
...@@ -78,10 +78,7 @@ impl Asset for RebasedAsset { ...@@ -78,10 +78,7 @@ impl Asset for RebasedAsset {
if let Some(source) = *self.module.source().await? { if let Some(source) = *self.module.source().await? {
Ok(source.content()) Ok(source.content())
} else { } else {
bail!( turbobail!("Module {} has no source", self.module.ident());
"Module {} has no source",
self.module.ident().to_string().await?
)
} }
} }
} }
...@@ -1169,7 +1169,7 @@ async fn realpath( ...@@ -1169,7 +1169,7 @@ async fn realpath(
} }
match &result.path_result { match &result.path_result {
Ok(path) => Ok(path.clone()), Ok(path) => Ok(path.clone()),
Err(e) => bail!(e.as_error_message(fs_path, &result)), Err(e) => bail!(e.as_error_message(fs_path, &result).await?),
} }
} }
...@@ -1375,7 +1375,7 @@ async fn find_package( ...@@ -1375,7 +1375,7 @@ async fn find_package(
if let Some(name) = basepath.get_path_to(package_dir) { if let Some(name) = basepath.get_path_to(package_dir) {
Ok(name.into()) Ok(name.into())
} else { } else {
bail!("Package directory {package_dir} is not inside the lookup path {basepath}"); bail!("Package directory {package_dir} is not inside the lookup path {basepath}",);
} }
} }
...@@ -1531,7 +1531,7 @@ pub async fn resolve_raw( ...@@ -1531,7 +1531,7 @@ pub async fn resolve_raw(
let result = &*path.realpath_with_links().await?; let result = &*path.realpath_with_links().await?;
let path = match &result.path_result { let path = match &result.path_result {
Ok(path) => path, Ok(path) => path,
Err(e) => bail!(e.as_error_message(path, result)), Err(e) => bail!(e.as_error_message(path, result).await?),
}; };
let request_key = RequestKey::new(request); let request_key = RequestKey::new(request);
let source = ResolvedVc::upcast(FileSource::new(path.clone()).to_resolved().await?); let source = ResolvedVc::upcast(FileSource::new(path.clone()).to_resolved().await?);
...@@ -3121,7 +3121,7 @@ async fn resolved( ...@@ -3121,7 +3121,7 @@ async fn resolved(
let result = &*fs_path.realpath_with_links().await?; let result = &*fs_path.realpath_with_links().await?;
let path = match &result.path_result { let path = match &result.path_result {
Ok(path) => path, Ok(path) => path,
Err(e) => bail!(e.as_error_message(&fs_path, result)), Err(e) => bail!(e.as_error_message(&fs_path, result).await?),
}; };
let path_ref = path.clone(); let path_ref = path.clone();
......
...@@ -5,7 +5,7 @@ use bincode::{Decode, Encode}; ...@@ -5,7 +5,7 @@ use bincode::{Decode, Encode};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexSet, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc, FxIndexSet, NonLocalValue, ResolvedVc, TryJoinIterExt, ValueToString, Vc,
debug::ValueDebugFormat, trace::TraceRawVcs, debug::ValueDebugFormat, trace::TraceRawVcs, turbofmt,
}; };
use turbo_tasks_fs::{FileSystemPath, glob::Glob}; use turbo_tasks_fs::{FileSystemPath, glob::Glob};
...@@ -510,16 +510,11 @@ impl ValueToString for ImportMapResult { ...@@ -510,16 +510,11 @@ impl ValueToString for ImportMapResult {
ImportMapResult::AliasExternal { .. } => Ok(Vc::cell(rcstr!("TODO external"))), ImportMapResult::AliasExternal { .. } => Ok(Vc::cell(rcstr!("TODO external"))),
ImportMapResult::Alias(request, context) => { ImportMapResult::Alias(request, context) => {
let s = if let Some(path) = context { let s = if let Some(path) = context {
let path = path.value_to_string().await?; turbofmt!("aliased to {} inside of {path}", *request).await?
format!(
"aliased to {} inside of {}",
request.to_string().await?,
path
)
} else { } else {
format!("aliased to {}", request.to_string().await?) turbofmt!("aliased to {}", *request).await?
}; };
Ok(Vc::cell(s.into())) Ok(Vc::cell(s))
} }
ImportMapResult::Alternatives(alternatives) => { ImportMapResult::Alternatives(alternatives) => {
// The .cell() calls happen synchronously during iteration, before try_join() // The .cell() calls happen synchronously during iteration, before try_join()
......
...@@ -3,7 +3,7 @@ use std::sync::LazyLock; ...@@ -3,7 +3,7 @@ use std::sync::LazyLock;
use anyhow::{Ok, Result}; use anyhow::{Ok, Result};
use regex::Regex; use regex::Regex;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc, turbofmt};
use super::pattern::Pattern; use super::pattern::Pattern;
...@@ -652,7 +652,7 @@ impl Request { ...@@ -652,7 +652,7 @@ impl Request {
encoding, encoding,
data, data,
} => { } => {
let data = ResolvedVc::cell(format!("{}{}", data.await?, suffix).into()); let data = ResolvedVc::cell(turbofmt!("{}{suffix}", *data).await?);
Self::DataUri { Self::DataUri {
media_type: media_type.clone(), media_type: media_type.clone(),
encoding: encoding.clone(), encoding: encoding.clone(),
......
...@@ -7,7 +7,7 @@ use regex::Regex; ...@@ -7,7 +7,7 @@ use regex::Regex;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use serde_json::value::RawValue; use serde_json::value::RawValue;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ResolvedVc, ValueToString}; use turbo_tasks::{ResolvedVc, turbofmt};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DiskFileSystem, FileContent, FileSystemPath, rope::Rope, util::uri_from_path_buf, DiskFileSystem, FileContent, FileSystemPath, rope::Rope, util::uri_from_path_buf,
}; };
...@@ -86,7 +86,7 @@ pub async fn resolve_source_map_sources( ...@@ -86,7 +86,7 @@ pub async fn resolve_source_map_sources(
origin: &FileSystemPath, origin: &FileSystemPath,
) -> Result<Option<Rope>> { ) -> Result<Option<Rope>> {
let fs_vc = origin.fs().to_resolved().await?; let fs_vc = origin.fs().to_resolved().await?;
let fs_str = &*format!("[{}]", fs_vc.to_string().await?); let fs_str = &*turbofmt!("[{fs_vc}]").await?;
let disk_fs = if let Some(fs_vc) = ResolvedVc::try_downcast_type::<DiskFileSystem>(fs_vc) { let disk_fs = if let Some(fs_vc) = ResolvedVc::try_downcast_type::<DiskFileSystem>(fs_vc) {
Some((fs_vc, fs_vc.await?)) Some((fs_vc, fs_vc.await?))
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_tasks::{ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TryJoinIterExt, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use crate::{ use crate::{
...@@ -58,10 +58,7 @@ impl Asset for TracedAsset { ...@@ -58,10 +58,7 @@ impl Asset for TracedAsset {
if let Some(source) = *self.module.source().await? { if let Some(source) = *self.module.source().await? {
Ok(source.content()) Ok(source.content())
} else { } else {
bail!( turbobail!("Module {} has no source", self.module.ident());
"Module {} has no source",
self.module.ident().to_string().await?,
)
} }
} }
} }
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{IntoTraitRef, ResolvedVc, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{IntoTraitRef, ResolvedVc, TryJoinIterExt, Vc, turbofmt};
use turbo_tasks_fs::{FileContent, FileSystemPath}; use turbo_tasks_fs::{FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MinifyType}, chunk::{ChunkItem, ChunkType, ChunkableModule, ChunkingContext, MinifyType},
...@@ -344,11 +344,10 @@ impl CssChunkItem for CssModuleChunkItem { ...@@ -344,11 +344,10 @@ impl CssChunkItem for CssModuleChunkItem {
.cell()) .cell())
} else { } else {
Ok(CssChunkItemContent { Ok(CssChunkItemContent {
inner_code: format!( inner_code: turbofmt!("/* unparsable {} */", self.module.ident())
"/* unparsable {} */", .await?
self.module.ident().to_string().await? .to_string()
) .into(),
.into(),
imports: vec![], imports: vec![],
import_context: None, import_context: None,
source_map: FileContent::NotFound.resolved_cell(), source_map: FileContent::NotFound.resolved_cell(),
......
use std::{fmt::Write, sync::Arc}; use std::{fmt::Write, sync::Arc};
use anyhow::{Context, Result}; use anyhow::{Context, Result};
use indoc::formatdoc;
use lightningcss::css_modules::CssModuleReference; use lightningcss::css_modules::CssModuleReference;
use swc_core::common::{BytePos, FileName, LineCol, SourceMap}; use swc_core::common::{BytePos, FileName, LineCol, SourceMap};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{FxIndexMap, IntoTraitRef, ResolvedVc, ValueToString, Vc}; use turbo_tasks::{FxIndexMap, IntoTraitRef, ResolvedVc, Vc, turbofmt};
use turbo_tasks_fs::{FileSystemPath, rope::Rope}; use turbo_tasks_fs::{FileSystemPath, rope::Rope};
use turbopack_core::{ use turbopack_core::{
chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt}, chunk::{AsyncModuleInfo, ChunkableModule, ChunkingContext, ModuleChunkItemIdExt},
...@@ -288,15 +287,18 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset { ...@@ -288,15 +287,18 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset {
let Some(resolved_module) = &*resolved_module else { let Some(resolved_module) = &*resolved_module else {
CssModuleComposesIssue { CssModuleComposesIssue {
severity: IssueSeverity::Error, severity: IssueSeverity::Error,
// TODO(PACK-4879): this should include detailed location information // TODO(PACK-4879): this should include detailed location
// information
source: IssueSource::from_source_only(self.await?.source), source: IssueSource::from_source_only(self.await?.source),
message: formatdoc! { message: turbofmt!(
r#" "Module {} referenced in `composes: ... from ...;` can't be \
Module {from} referenced in `composes: ... from {from};` can't be resolved. resolved.\n",
"#, from.await?.request
from = &*from.await?.request.to_string().await? )
}.into(), .await?,
}.resolved_cell().emit(); }
.resolved_cell()
.emit();
continue; continue;
}; };
...@@ -305,15 +307,18 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset { ...@@ -305,15 +307,18 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset {
else { else {
CssModuleComposesIssue { CssModuleComposesIssue {
severity: IssueSeverity::Error, severity: IssueSeverity::Error,
// TODO(PACK-4879): this should include detailed location information // TODO(PACK-4879): this should include detailed location
// information
source: IssueSource::from_source_only(self.await?.source), source: IssueSource::from_source_only(self.await?.source),
message: formatdoc! { message: turbofmt!(
r#" "Module {} referenced in `composes: ... from ...;` is not a \
Module {from} referenced in `composes: ... from {from};` is not a CSS module. CSS module.\n",
"#, from.await?.request
from = &*from.await?.request.to_string().await? )
}.into(), .await?,
}.resolved_cell().emit(); }
.resolved_cell()
.emit();
continue; continue;
}; };
...@@ -353,7 +358,7 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset { ...@@ -353,7 +358,7 @@ impl EcmascriptChunkPlaceable for ModuleCssAsset {
// displayed in dev tools. // displayed in dev tools.
source_map: if source_map { source_map: if source_map {
Some(generate_minimal_source_map( Some(generate_minimal_source_map(
self.ident().to_string().await?.to_string(), turbofmt!("{}", self.ident()).await?.to_string(),
code, code,
)?) )?)
} else { } else {
......
...@@ -78,7 +78,7 @@ use tracing::{Instrument, Level, instrument}; ...@@ -78,7 +78,7 @@ use tracing::{Instrument, Level, instrument};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxDashMap, FxIndexMap, IntoTraitRef, NonLocalValue, ReadRef, ResolvedVc, TaskInput, FxDashMap, FxIndexMap, IntoTraitRef, NonLocalValue, ReadRef, ResolvedVc, TaskInput,
TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, turbofmt,
}; };
use turbo_tasks_fs::{FileJsonContent, FileSystemPath, glob::Glob, rope::Rope}; use turbo_tasks_fs::{FileJsonContent, FileSystemPath, glob::Glob, rope::Rope};
use turbopack_core::{ use turbopack_core::{
...@@ -1559,10 +1559,9 @@ async fn merge_modules( ...@@ -1559,10 +1559,9 @@ async fn merge_modules(
let (merged_ast, inserted) = match result { let (merged_ast, inserted) = match result {
Ok(v) => v, Ok(v) => v,
Err((content_idx, err)) => { Err((content_idx, err)) => {
return Err(err.context(format!( return Err(
"Processing {}", err.context(turbofmt!("Processing {}", contents[content_idx].0.ident()).await?)
contents[content_idx].0.ident().to_string().await? );
)));
} }
}; };
...@@ -1803,7 +1802,7 @@ async fn process_parse_result( ...@@ -1803,7 +1802,7 @@ async fn process_parse_result(
Some(Comment { Some(Comment {
kind: CommentKind::Line, kind: CommentKind::Line,
span: DUMMY_SP, span: DUMMY_SP,
text: format!(" MERGED MODULE: {}", ident.to_string().await?).into(), text: (&*turbofmt!(" MERGED MODULE: {}", ident).await?).into(),
}) })
} else { } else {
None None
...@@ -1918,12 +1917,15 @@ async fn process_parse_result( ...@@ -1918,12 +1917,15 @@ async fn process_parse_result(
Ok(match parse_result { Ok(match parse_result {
ParseResult::Ok { .. } => unreachable!(), ParseResult::Ok { .. } => unreachable!(),
ParseResult::Unparsable { messages } => { ParseResult::Unparsable { messages } => {
let path = ident.path().to_string().await?;
let error_messages = messages let error_messages = messages
.as_ref() .as_ref()
.and_then(|m| m.first().map(|f| format!("\n{f}"))) .and_then(|m| m.first().map(|f| format!("\n{f}")))
.unwrap_or("".into()); .unwrap_or("".into());
let msg = format!("Could not parse module '{path}'\n{error_messages}"); let msg = &*turbofmt!(
"Could not parse module '{}'\n{error_messages}",
ident.path()
)
.await?;
let body = vec![ let body = vec![
quote!( quote!(
"const e = new Error($msg);" as Stmt, "const e = new Error($msg);" as Stmt,
...@@ -1949,8 +1951,9 @@ async fn process_parse_result( ...@@ -1949,8 +1951,9 @@ async fn process_parse_result(
} }
} }
ParseResult::NotFound => { ParseResult::NotFound => {
let path = ident.path().to_string().await?; let msg =
let msg = format!("Could not parse module '{path}', file not found"); &*turbofmt!("Could not parse module '{}', file not found", ident.path())
.await?;
let body = vec![ let body = vec![
quote!( quote!(
"const e = new Error($msg);" as Stmt, "const e = new Error($msg);" as Stmt,
......
use std::{future::Future, sync::Arc}; use std::{future::Future, sync::Arc};
use anyhow::{Context, Result, anyhow}; use anyhow::{Context, Result};
use bytes_str::BytesStr; use bytes_str::BytesStr;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use swc_core::{ use swc_core::{
...@@ -31,7 +31,7 @@ use swc_core::{ ...@@ -31,7 +31,7 @@ use swc_core::{
}; };
use tracing::{Instrument, instrument}; use tracing::{Instrument, instrument};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{PrettyPrintError, ResolvedVc, ValueToString, Vc, util::WrapFuture}; use turbo_tasks::{PrettyPrintError, ResolvedVc, ValueToString, Vc, turbofmt, util::WrapFuture};
use turbo_tasks_fs::{FileContent, FileSystemPath, rope::Rope}; use turbo_tasks_fs::{FileContent, FileSystemPath, rope::Rope};
use turbo_tasks_hash::hash_xxh3_hash64; use turbo_tasks_hash::hash_xxh3_hash64;
use turbopack_core::{ use turbopack_core::{
...@@ -284,10 +284,7 @@ pub async fn parse( ...@@ -284,10 +284,7 @@ pub async fn parse(
.await .await
{ {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(error) => Err(error.context(format!( Err(error) => Err(error.context(turbofmt!("failed to parse {}", source.ident()).await?)),
"failed to parse {}",
source.ident().to_string().await?
))),
} }
} }
...@@ -347,10 +344,13 @@ async fn parse_internal( ...@@ -347,10 +344,13 @@ async fn parse_internal(
{ {
Ok(result) => result, Ok(result) => result,
Err(e) => { Err(e) => {
return Err(e).context(anyhow!( return Err(e).context(
"Transforming and/or parsing of {} failed", turbofmt!(
source.ident().to_string().await? "Transforming and/or parsing of {} failed",
)); source.ident()
)
.await?,
);
} }
} }
} }
......
...@@ -10,7 +10,7 @@ use swc_core::{ ...@@ -10,7 +10,7 @@ use swc_core::{
quote, quote,
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
chunk::{ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleChunkItemIdExt}, chunk::{ChunkingContext, ChunkingType, ChunkingTypeOption, ModuleChunkItemIdExt},
...@@ -669,11 +669,10 @@ impl EsmAssetReference { ...@@ -669,11 +669,10 @@ impl EsmAssetReference {
.supports_esm_externals() .supports_esm_externals()
.await? .await?
{ {
bail!( turbobail!(
"the chunking context ({}) does not support \ "the chunking context ({}) does not support \
external modules (esm request: {})", external modules (esm request: {request})",
chunking_context.name().await?, chunking_context.name()
request
); );
} }
let (sym, ctxt) = let (sym, ctxt) =
...@@ -720,11 +719,10 @@ impl EsmAssetReference { ...@@ -720,11 +719,10 @@ impl EsmAssetReference {
.supports_commonjs_externals() .supports_commonjs_externals()
.await? .await?
{ {
bail!( turbobail!(
"the chunking context ({}) does not support \ "the chunking context ({}) does not support \
external modules (request: {})", external modules (request: {request})",
chunking_context.name().await?, chunking_context.name()
request
); );
} }
let (sym, ctxt) = let (sym, ctxt) =
......
...@@ -12,8 +12,7 @@ use swc_core::{ ...@@ -12,8 +12,7 @@ use swc_core::{
}; };
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, ValueToString, Vc, FxIndexMap, NonLocalValue, ResolvedVc, TryFlatJoinIterExt, Vc, trace::TraceRawVcs, turbofmt,
trace::TraceRawVcs,
}; };
use turbopack_core::{ use turbopack_core::{
chunk::{ChunkingContext, ModuleChunkItemIdExt}, chunk::{ChunkingContext, ModuleChunkItemIdExt},
...@@ -416,27 +415,27 @@ pub async fn expand_star_exports( ...@@ -416,27 +415,27 @@ pub async fn expand_star_exports(
EcmascriptExports::None | EcmascriptExports::EmptyCommonJs => { EcmascriptExports::None | EcmascriptExports::EmptyCommonJs => {
emit_star_exports_issue( emit_star_exports_issue(
asset.ident(), asset.ident(),
format!( turbofmt!(
"export * used with module {} which has no exports\nTypescript only: Did \ "export * used with module {} which has no exports\nTypescript only: Did \
you want to export only types with `export type * from \"...\"`?\nNote: \ you want to export only types with `export type * from \"...\"`?\nNote: \
Using `export type` is more efficient than `export *` as it won't emit \ Using `export type` is more efficient than `export *` as it won't emit \
any runtime code.", any runtime code.",
asset.ident().to_string().await? asset.ident()
) )
.into(), .await?,
) )
.await? .await?
} }
EcmascriptExports::Value => { EcmascriptExports::Value => {
emit_star_exports_issue( emit_star_exports_issue(
asset.ident(), asset.ident(),
format!( turbofmt!(
"export * used with module {} which only has a default export (default \ "export * used with module {} which only has a default export (default \
export is not exported with export *)\nDid you want to use `export {{ \ export is not exported with export *)\nDid you want to use `export {{ \
default }} from \"...\";` instead?", default }} from \"...\";` instead?",
asset.ident().to_string().await? asset.ident()
) )
.into(), .await?,
) )
.await? .await?
} }
...@@ -444,14 +443,14 @@ pub async fn expand_star_exports( ...@@ -444,14 +443,14 @@ pub async fn expand_star_exports(
dynamic_exporting_modules.push(asset); dynamic_exporting_modules.push(asset);
emit_star_exports_issue( emit_star_exports_issue(
asset.ident(), asset.ident(),
format!( turbofmt!(
"export * used with module {} which is a CommonJS module with exports \ "export * used with module {} which is a CommonJS module with exports \
only available at runtime\nList all export names manually (`export {{ a, \ only available at runtime\nList all export names manually (`export {{ a, \
b, c }} from \"...\") or rewrite the module to ESM, to avoid the \ b, c }} from \"...\") or rewrite the module to ESM, to avoid the \
additional runtime code.`", additional runtime code.`",
asset.ident().to_string().await? asset.ident()
) )
.into(), .await?,
) )
.await?; .await?;
} }
......
...@@ -63,7 +63,7 @@ use tracing::Instrument; ...@@ -63,7 +63,7 @@ use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
FxIndexMap, FxIndexSet, NonLocalValue, PrettyPrintError, ReadRef, ResolvedVc, TaskInput, FxIndexMap, FxIndexSet, NonLocalValue, PrettyPrintError, ReadRef, ResolvedVc, TaskInput,
TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, TryJoinIterExt, Upcast, ValueToString, Vc, trace::TraceRawVcs, turbofmt,
}; };
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
...@@ -533,10 +533,8 @@ pub async fn analyze_ecmascript_module( ...@@ -533,10 +533,8 @@ pub async fn analyze_ecmascript_module(
match result { match result {
Ok(result) => Ok(result), Ok(result) => Ok(result),
Err(err) => Err(err.context(format!( Err(err) => Err(err
"failed to analyze ecmascript module '{}'", .context(turbofmt!("failed to analyze ecmascript module '{}'", module.ident()).await?)),
module.ident().to_string().await?
))),
} }
} }
......
...@@ -164,7 +164,7 @@ async fn resolve_reference_from_dir( ...@@ -164,7 +164,7 @@ async fn resolve_reference_from_dir(
} }
let path: FileSystemPath = match &realpath.path_result { let path: FileSystemPath = match &realpath.path_result {
Ok(path) => path.clone(), Ok(path) => path.clone(),
Err(e) => bail!(e.as_error_message(file, &realpath)), Err(e) => bail!(e.as_error_message(file, &realpath).await?),
}; };
results.push(( results.push((
RequestKey::new(matched_path.clone()), RequestKey::new(matched_path.clone()),
......
use anyhow::Result; use anyhow::Result;
use swc_core::{ecma::ast::Expr, quote}; use swc_core::{ecma::ast::Expr, quote};
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, Vc, turbofmt};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
self, self,
...@@ -92,13 +92,13 @@ impl Issue for TooManyMatchesWarning { ...@@ -92,13 +92,13 @@ impl Issue for TooManyMatchesWarning {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn title(&self) -> Result<Vc<StyledString>> { async fn title(&self) -> Result<Vc<StyledString>> {
Ok(StyledString::Text( Ok(StyledString::Text(
format!( turbofmt!(
"The file pattern {pattern} matches {num_matches} files in {context_dir}", "The file pattern {} matches {} files in {}",
pattern = self.pattern.to_string().await?, self.pattern,
context_dir = self.context_dir.value_to_string().await?, self.num_matches,
num_matches = self.num_matches self.context_dir
) )
.into(), .await?,
) )
.cell()) .cell())
} }
......
...@@ -8,6 +8,7 @@ use swc_core::{ ...@@ -8,6 +8,7 @@ use swc_core::{
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
NonLocalValue, ResolvedVc, ValueToString, Vc, debug::ValueDebugFormat, trace::TraceRawVcs, NonLocalValue, ResolvedVc, ValueToString, Vc, debug::ValueDebugFormat, trace::TraceRawVcs,
turbofmt,
}; };
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{ use turbopack_core::{
...@@ -177,8 +178,6 @@ impl ModuleReference for WorkerAssetReference { ...@@ -177,8 +178,6 @@ impl ModuleReference for WorkerAssetReference {
for (request_key, resolve_item) in result_ref.primary.iter() { for (request_key, resolve_item) in result_ref.primary.iter() {
match resolve_item { match resolve_item {
ModuleResolveResultItem::Module(module) => { ModuleResolveResultItem::Module(module) => {
let module_ident = module.ident().to_string().await?;
let Some(chunkable) = let Some(chunkable) =
ResolvedVc::try_downcast::<Box<dyn ChunkableModule>>(*module) ResolvedVc::try_downcast::<Box<dyn ChunkableModule>>(*module)
else { else {
...@@ -187,13 +186,13 @@ impl ModuleReference for WorkerAssetReference { ...@@ -187,13 +186,13 @@ impl ModuleReference for WorkerAssetReference {
title: StyledString::Text(rcstr!("non-chunkable module")) title: StyledString::Text(rcstr!("non-chunkable module"))
.resolved_cell(), .resolved_cell(),
message: StyledString::Text( message: StyledString::Text(
format!( turbofmt!(
"Worker entry point module '{}' is not chunkable and cannot \ "Worker entry point module '{}' is not chunkable and cannot \
be used as a worker module. This may happen if the module \ be used as a worker module. This may happen if the module \
type doesn't support bundling.", type doesn't support bundling.",
module_ident module.ident()
) )
.into(), .await?,
) )
.resolved_cell(), .resolved_cell(),
path: self.origin.origin_path().owned().await?, path: self.origin.origin_path().owned().await?,
...@@ -215,14 +214,14 @@ impl ModuleReference for WorkerAssetReference { ...@@ -215,14 +214,14 @@ impl ModuleReference for WorkerAssetReference {
title: StyledString::Text(rcstr!("non-evaluatable module")) title: StyledString::Text(rcstr!("non-evaluatable module"))
.resolved_cell(), .resolved_cell(),
message: StyledString::Text( message: StyledString::Text(
format!( turbofmt!(
"Worker thread entry point module '{}' must be evaluatable to \ "Worker thread entry point module '{}' must be evaluatable to \
serve as an entry point. This module cannot be used as a \ serve as an entry point. This module cannot be used as a \
Node.js worker_threads Worker entry point because it doesn't \ Node.js worker_threads Worker entry point because it doesn't \
support direct evaluation.", support direct evaluation.",
module_ident module.ident()
) )
.into(), .await?,
) )
.resolved_cell(), .resolved_cell(),
path: self.origin.origin_path().owned().await?, path: self.origin.origin_path().owned().await?,
...@@ -285,21 +284,16 @@ impl WorkerAssetReference { ...@@ -285,21 +284,16 @@ impl WorkerAssetReference {
impl ValueToString for WorkerAssetReference { impl ValueToString for WorkerAssetReference {
#[turbo_tasks::function] #[turbo_tasks::function]
async fn to_string(&self) -> Result<Vc<RcStr>> { async fn to_string(&self) -> Result<Vc<RcStr>> {
Ok(Vc::cell( let worker_type = match self.worker_type {
format!( WorkerType::WebWorker => "WebWorker",
"new {}({})", WorkerType::SharedWebWorker => "SharedWorker",
match self.worker_type { WorkerType::NodeWorkerThread => "NodeWorkerThread",
WorkerType::WebWorker => "WebWorker", };
WorkerType::SharedWebWorker => "SharedWorker", let request = match &self.request {
WorkerType::NodeWorkerThread => "NodeWorkerThread", WorkerRequest::Url(request) => request.to_string(),
}, WorkerRequest::Pattern { path, .. } => path.to_string(),
match &self.request { };
WorkerRequest::Url(request) => request.to_string().await?, Ok(Vc::cell(turbofmt!("new {worker_type}({request})").await?))
WorkerRequest::Pattern { path, .. } => path.to_string().await?,
}
)
.into(),
))
} }
} }
......
...@@ -13,7 +13,7 @@ use swc_core::{ ...@@ -13,7 +13,7 @@ use swc_core::{
}, },
}; };
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexSet, ResolvedVc, ValueToString, Vc}; use turbo_tasks::{FxIndexSet, ResolvedVc, ValueToString, Vc, turbobail};
use turbopack_core::{ident::AssetIdent, resolve::ModulePart, source::Source}; use turbopack_core::{ident::AssetIdent, resolve::ModulePart, source::Source};
use self::graph::{DepGraph, ItemData, ItemId, ItemIdGroupKind, Mode, SplitModuleResult}; use self::graph::{DepGraph, ItemData, ItemId, ItemIdGroupKind, Mode, SplitModuleResult};
...@@ -716,11 +716,11 @@ pub(crate) async fn part_of_module( ...@@ -716,11 +716,11 @@ pub(crate) async fn part_of_module(
let part_id = get_part_id(&split_data, &part).await?; let part_id = get_part_id(&split_data, &part).await?;
if part_id as usize >= modules.len() { if part_id as usize >= modules.len() {
bail!( turbobail!(
"part_id is out of range: {part_id} >= {}; asset = {}; entrypoints = \ "part_id is out of range: {part_id} >= {}; asset = {}; entrypoints = \
{entrypoints:?}: part_deps = {deps:?}", {entrypoints:?}: part_deps = {deps:?}",
asset_ident.to_string().await?,
modules.len(), modules.len(),
*asset_ident
); );
} }
......
...@@ -2,7 +2,7 @@ use std::{collections::HashSet, env::current_dir, path::PathBuf}; ...@@ -2,7 +2,7 @@ use std::{collections::HashSet, env::current_dir, path::PathBuf};
use anyhow::Result; use anyhow::Result;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ResolvedVc, TransientInstance, TryJoinIterExt, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, TransientInstance, TryJoinIterExt, Vc, turbofmt};
use turbo_tasks_fs::{DiskFileSystem, FileSystem}; use turbo_tasks_fs::{DiskFileSystem, FileSystem};
use turbopack::{ use turbopack::{
ModuleAssetContext, ModuleAssetContext,
...@@ -155,17 +155,18 @@ async fn to_graph(asset: ResolvedVc<Box<dyn OutputAsset>>, max_depth: usize) -> ...@@ -155,17 +155,18 @@ async fn to_graph(asset: ResolvedVc<Box<dyn OutputAsset>>, max_depth: usize) ->
for _ in 0..depth { for _ in 0..depth {
indent.push_str(" "); indent.push_str(" ");
} }
let path = asset.path();
if visited.insert(asset) { if visited.insert(asset) {
if depth < max_depth { if depth < max_depth {
for &asset in references.iter().rev() { for &asset in references.iter().rev() {
queue.push((depth + 1, asset)); queue.push((depth + 1, asset));
} }
} }
result.push(format!("{}{}", indent, asset.path().to_string().await?).into()); result.push(turbofmt!("{indent}{path}").await?);
} else if references.is_empty() { } else if references.is_empty() {
result.push(format!("{}{} *", indent, asset.path().to_string().await?).into()); result.push(turbofmt!("{indent}{path} *").await?);
} else { } else {
result.push(format!("{}{} *...", indent, asset.path().to_string().await?).into()); result.push(turbofmt!("{indent}{path} *...").await?);
} }
} }
result.push("".into()); result.push("".into());
......
...@@ -11,8 +11,8 @@ use serde_with::serde_as; ...@@ -11,8 +11,8 @@ use serde_with::serde_as;
use tracing::Instrument; use tracing::Instrument;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, OperationVc, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, ValueToString, Vc, Completion, OperationVc, ReadRef, ResolvedVc, TaskInput, TryJoinIterExt, Vc,
trace::TraceRawVcs, trace::TraceRawVcs, turbobail,
}; };
use turbo_tasks_env::ProcessEnv; use turbo_tasks_env::ProcessEnv;
use turbo_tasks_fs::{ use turbo_tasks_fs::{
...@@ -262,10 +262,10 @@ impl WebpackLoadersProcessedAsset { ...@@ -262,10 +262,10 @@ impl WebpackLoadersProcessedAsset {
let resource_fs_path = self.source.ident().path().await?; let resource_fs_path = self.source.ident().path().await?;
let Some(resource_path) = project_path.get_relative_path_to(&resource_fs_path) else { let Some(resource_path) = project_path.get_relative_path_to(&resource_fs_path) else {
bail!(format!( turbobail!(
"Resource path \"{}\" need to be on project filesystem \"{}\"", "Resource path \"{resource_fs_path}\" needs to be on project filesystem \
resource_fs_path, project_path \"{project_path}\"",
)); );
}; };
let config_value = evaluate_webpack_loader(WebpackLoaderContext { let config_value = evaluate_webpack_loader(WebpackLoaderContext {
entries, entries,
...@@ -610,18 +610,13 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -610,18 +610,13 @@ impl EvaluateContext for WebpackLoaderContext {
{ {
Ok(ResponseMessage::Resolve { path }) Ok(ResponseMessage::Resolve { path })
} else { } else {
bail!( turbobail!(
"Resolving {} in {} ends up on a different filesystem", "Resolving {request} in {lookup_path} ends up on a different \
request.to_string().await?, filesystem"
lookup_path.value_to_string().await?
); );
} }
} else { } else {
bail!( turbobail!("Unable to resolve {request} in {lookup_path}");
"Unable to resolve {} in {}",
request.to_string().await?,
lookup_path.value_to_string().await?
);
} }
} }
RequestMessage::TrackFileRead { file } => { RequestMessage::TrackFileRead { file } => {
......
use std::io::Write; use std::io::Write;
use anyhow::{Result, bail}; use anyhow::Result;
use indoc::writedoc; use indoc::writedoc;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -75,7 +75,7 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -75,7 +75,7 @@ impl EcmascriptBuildNodeEntryChunk {
if let Some(path) = chunk_directory.get_relative_path_to(&runtime_path) { if let Some(path) = chunk_directory.get_relative_path_to(&runtime_path) {
path path
} else { } else {
bail!( turbobail!(
"cannot find a relative path from the chunk ({chunk_path}) to the runtime \ "cannot find a relative path from the chunk ({chunk_path}) to the runtime \
chunk ({runtime_path})", chunk ({runtime_path})",
); );
...@@ -83,7 +83,7 @@ impl EcmascriptBuildNodeEntryChunk { ...@@ -83,7 +83,7 @@ impl EcmascriptBuildNodeEntryChunk {
let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_public_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
path path
} else { } else {
bail!("chunk path ({chunk_path}) is not in output root ({output_root})"); turbobail!("chunk path ({chunk_path}) is not in output root ({output_root})");
}; };
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
......
use std::io::Write; use std::io::Write;
use anyhow::{Result, bail}; use anyhow::Result;
use indoc::writedoc; use indoc::writedoc;
use turbo_rcstr::rcstr; use turbo_rcstr::rcstr;
use turbo_tasks::{ResolvedVc, ValueToString, Vc}; use turbo_tasks::{ResolvedVc, ValueToString, Vc, turbobail};
use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath}; use turbo_tasks_fs::{File, FileContent, FileSystem, FileSystemPath};
use turbopack_core::{ use turbopack_core::{
asset::{Asset, AssetContent}, asset::{Asset, AssetContent},
...@@ -48,7 +48,7 @@ impl EcmascriptBuildNodeRuntimeChunk { ...@@ -48,7 +48,7 @@ impl EcmascriptBuildNodeRuntimeChunk {
let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) { let runtime_public_path = if let Some(path) = output_root.get_path_to(&runtime_path) {
path path
} else { } else {
bail!("runtime path {runtime_path} is not in output root {output_root}"); turbobail!("runtime path {runtime_path} is not in output root {output_root}");
}; };
let mut code = CodeBuilder::default(); let mut code = CodeBuilder::default();
......
use anyhow::{Result, bail}; use anyhow::Result;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{FxIndexMap, ReadRef, Vc}; use turbo_tasks::{FxIndexMap, ReadRef, Vc, turbobail};
use turbo_tasks_fs::FileSystemPath; use turbo_tasks_fs::FileSystemPath;
use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex}; use turbo_tasks_hash::{Xxh3Hash64Hasher, encode_hex};
use turbopack_core::{ use turbopack_core::{
...@@ -31,7 +31,7 @@ impl EcmascriptBuildNodeChunkVersion { ...@@ -31,7 +31,7 @@ impl EcmascriptBuildNodeChunkVersion {
let chunk_path = if let Some(path) = output_root.get_path_to(&chunk_path) { let chunk_path = if let Some(path) = output_root.get_path_to(&chunk_path) {
path path
} else { } else {
bail!("chunk path {chunk_path} is not in client root {output_root}"); turbobail!("chunk path {chunk_path} is not in client root {output_root}");
}; };
let chunk_items = content.await?.chunk_item_code_and_ids().await?; let chunk_items = content.await?.chunk_item_code_and_ids().await?;
......
...@@ -184,7 +184,9 @@ async fn resolve_node_pre_gyp_files( ...@@ -184,7 +184,9 @@ async fn resolve_node_pre_gyp_files(
format!("deps/lib/{key}").into(), format!("deps/lib/{key}").into(),
Vc::upcast(FileSource::new(match &realpath_with_links.path_result { Vc::upcast(FileSource::new(match &realpath_with_links.path_result {
Ok(path) => path.clone(), Ok(path) => path.clone(),
Err(e) => bail!(e.as_error_message(dylib, &realpath_with_links)), Err(e) => {
bail!(e.as_error_message(dylib, &realpath_with_links).await?)
}
})), })),
); );
} }
......
...@@ -6,7 +6,7 @@ use regex::Regex; ...@@ -6,7 +6,7 @@ use regex::Regex;
use rustc_hash::{FxHashMap, FxHashSet}; use rustc_hash::{FxHashMap, FxHashSet};
use similar::TextDiff; use similar::TextDiff;
use turbo_rcstr::RcStr; use turbo_rcstr::RcStr;
use turbo_tasks::{ReadRef, TryJoinIterExt, Vc}; use turbo_tasks::{ReadRef, TryJoinIterExt, Vc, turbofmt};
use turbo_tasks_fs::{ use turbo_tasks_fs::{
DirectoryContent, DirectoryEntry, File, FileContent, FileSystemEntryType, FileSystemPath, DirectoryContent, DirectoryEntry, File, FileContent, FileSystemEntryType, FileSystemPath,
}; };
...@@ -156,14 +156,14 @@ pub async fn diff(path: FileSystemPath, actual: Vc<AssetContent>) -> Result<()> ...@@ -156,14 +156,14 @@ pub async fn diff(path: FileSystemPath, actual: Vc<AssetContent>) -> Result<()>
async fn get_contents(file: Vc<AssetContent>, path: FileSystemPath) -> Result<Option<String>> { async fn get_contents(file: Vc<AssetContent>, path: FileSystemPath) -> Result<Option<String>> {
Ok( Ok(
match &*file.await.context(format!( match &*file
"Unable to read AssetContent of {}", .await
path.value_to_string().await? .context(turbofmt!("Unable to read AssetContent of {path}").await?)?
))? { {
AssetContent::File(file) => match &*file.await.context(format!( AssetContent::File(file) => match &*file
"Unable to read FileContent of {}", .await
path.value_to_string().await? .context(turbofmt!("Unable to read FileContent of {path}").await?)?
))? { {
FileContent::NotFound => None, FileContent::NotFound => None,
FileContent::Content(expected) => { FileContent::Content(expected) => {
let rope = expected.content(); let rope = expected.content();
......
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