Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Turbopack: more tracing #75351

Open
wants to merge 2 commits into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ mime = { workspace = true }
owo-colors = { workspace = true }
serde = { workspace = true }
tokio = { workspace = true, features = ["full"] }
tracing = { workspace = true }
tracing-subscriber = { workspace = true, features = ["env-filter", "json"] }
turbo-rcstr = { workspace = true }
turbo-tasks = { workspace = true }
Expand Down
171 changes: 98 additions & 73 deletions turbopack/crates/turbopack-cli/src/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use std::{
};

use anyhow::{bail, Context, Result};
use tracing::Instrument;
use turbo_rcstr::RcStr;
use turbo_tasks::{
apply_effects, ReadConsistency, ResolvedVc, TransientInstance, TryJoinIterExt, TurboTasks,
Expand Down Expand Up @@ -146,7 +147,9 @@ impl TurbopackBuildBuilder {
// Await the result to propagate any errors.
build_result_op.read_strongly_consistent().await?;

apply_effects(build_result_op).await?;
apply_effects(build_result_op)
.instrument(tracing::info_span!("apply effects"))
.await?;

let issue_reporter: Vc<Box<dyn IssueReporter>> =
Vc::upcast(ConsoleUi::new(TransientInstance::new(LogOptions {
Expand Down Expand Up @@ -267,28 +270,38 @@ async fn build_internal(

let origin = PlainResolveOrigin::new(asset_context, project_fs.root().join("_".into()));
let project_dir = &project_dir;
let entries = entry_requests
.into_iter()
.map(|request_vc| async move {
let ty = Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined));
let request = request_vc.await?;
origin
.resolve_asset(request_vc, origin.resolve_options(ty.clone()), ty)
.await?
.first_module()
.await?
.with_context(|| {
format!(
"Unable to resolve entry {} from directory {}.",
request.request().unwrap(),
project_dir
)
})
})
.try_join()
.await?;
let entries = async move {
entry_requests
.into_iter()
.map(|request_vc| async move {
let ty = Value::new(ReferenceType::Entry(EntryReferenceSubType::Undefined));
let request = request_vc.await?;
origin
.resolve_asset(request_vc, origin.resolve_options(ty.clone()), ty)
.await?
.first_module()
.await?
.with_context(|| {
format!(
"Unable to resolve entry {} from directory {}.",
request.request().unwrap(),
project_dir
)
})
})
.try_join()
.await
}
.instrument(tracing::info_span!("resolve entries"))
.await?;

let module_graph = ModuleGraph::from_modules(Vc::cell(entries.clone()));
let module_graph = async {
ModuleGraph::from_modules(Vc::cell(entries.clone()))
.resolve()
.await
}
.instrument(tracing::info_span!("prepare module graph"))
.await?;
let module_id_strategy = ResolvedVc::upcast(
get_global_module_id_strategy(module_graph)
.to_resolved()
Expand Down Expand Up @@ -372,16 +385,40 @@ async fn build_internal(

let entry_chunk_groups = entries
.into_iter()
.map(|entry_module| async move {
Ok(
if let Some(ecmascript) =
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(entry_module).await?
{
match target {
Target::Browser => {
chunking_context
.evaluated_chunk_group(
AssetIdent::from_path(
.map(async |entry_module| {
async move {
Ok(
if let Some(ecmascript) =
ResolvedVc::try_sidecast::<Box<dyn EvaluatableAsset>>(entry_module).await?
{
match target {
Target::Browser => {
chunking_context
.evaluated_chunk_group(
AssetIdent::from_path(
build_output_root
.join(
ecmascript
.ident()
.path()
.file_stem()
.await?
.as_deref()
.unwrap()
.into(),
)
.with_extension("entry.js".into()),
),
EvaluatableAssets::one(*ResolvedVc::upcast(ecmascript)),
module_graph,
Value::new(AvailabilityInfo::Root),
)
.await?
.assets
}
Target::Node => ResolvedVc::cell(vec![
chunking_context
.entry_chunk_group(
build_output_root
.join(
ecmascript
Expand All @@ -394,58 +431,46 @@ async fn build_internal(
.into(),
)
.with_extension("entry.js".into()),
),
EvaluatableAssets::one(*ResolvedVc::upcast(ecmascript)),
module_graph,
Value::new(AvailabilityInfo::Root),
)
.await?
.assets
*ResolvedVc::upcast(ecmascript),
EvaluatableAssets::one(*ResolvedVc::upcast(ecmascript)),
module_graph,
OutputAssets::empty(),
Value::new(AvailabilityInfo::Root),
)
.await?
.asset,
]),
}
Target::Node => ResolvedVc::cell(vec![
chunking_context
.entry_chunk_group(
build_output_root
.join(
ecmascript
.ident()
.path()
.file_stem()
.await?
.as_deref()
.unwrap()
.into(),
)
.with_extension("entry.js".into()),
*ResolvedVc::upcast(ecmascript),
EvaluatableAssets::one(*ResolvedVc::upcast(ecmascript)),
module_graph,
OutputAssets::empty(),
Value::new(AvailabilityInfo::Root),
)
.await?
.asset,
]),
}
} else {
bail!(
"Entry module is not chunkable, so it can't be used to bootstrap the \
application"
)
},
)
} else {
bail!(
"Entry module is not chunkable, so it can't be used to bootstrap the \
application"
)
},
)
}
.instrument(tracing::info_span!("chunk entry"))
.await
})
.try_join()
.await?;

let mut chunks: HashSet<ResolvedVc<Box<dyn OutputAsset>>> = HashSet::new();
for chunk_group in entry_chunk_groups {
chunks.extend(&*all_assets_from_entries(*chunk_group).await?);
chunks.extend(
&*async move { all_assets_from_entries(*chunk_group).await }
.instrument(tracing::info_span!("list chunks"))
.await?,
);
}

chunks
.iter()
.map(|c| c.content().write(c.ident().path()))
.map(async |c| {
async move { c.content().write(c.ident().path()).resolve().await }
.instrument(tracing::info_span!("build chunk content"))
.await
})
.try_join()
.await?;

Expand Down
1 change: 1 addition & 0 deletions turbopack/crates/turbopack-cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#![feature(min_specialization)]
#![feature(arbitrary_self_types)]
#![feature(arbitrary_self_types_pointers)]
#![feature(async_closure)]

pub mod arguments;
pub mod build;
Expand Down
25 changes: 21 additions & 4 deletions turbopack/crates/turbopack-ecmascript/src/minify.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{io::Write, sync::Arc};
use std::{borrow::Cow, io::Write, sync::Arc};

use anyhow::{bail, Context, Result};
use swc_core::{
Expand All @@ -19,7 +19,8 @@ use swc_core::{
transforms::base::fixer::paren_remover,
},
};
use turbo_tasks::{ResolvedVc, Vc};
use tracing::{field::Empty, Instrument};
use turbo_tasks::{ResolvedVc, ValueToString, Vc};
use turbo_tasks_fs::FileSystemPath;
use turbopack_core::{
code_builder::{Code, CodeBuilder},
Expand All @@ -34,6 +35,22 @@ pub async fn minify(
code: Vc<Code>,
source_maps: Vc<bool>,
) -> Result<Vc<Code>> {
let code_ref = code.await?;
let code_str = code_ref.source_code().to_str()?;
let span = tracing::info_span!("minify", path = %path.to_string().await?, input_len = code_str.len(), output_len = Empty);
let code = async move { minify_inner(path, code, code_str, source_maps).await }
.instrument(span.clone())
.await?;
span.record("output_len", code.source_code().len());
Ok(code.cell())
}

pub async fn minify_inner(
path: Vc<FileSystemPath>,
code: Vc<Code>,
code_str: Cow<'_, str>,
source_maps: Vc<bool>,
) -> Result<Code> {
let path = path.await?;
let source_maps = source_maps.await?.then(|| code.generate_source_map());
let code = code.await?;
Expand All @@ -43,7 +60,7 @@ pub async fn minify(
let compiler = Arc::new(Compiler::new(cm.clone()));
let fm = compiler.cm.new_source_file(
FileName::Custom(path.path.to_string()).into(),
code.source_code().to_str()?.into_owned(),
code_str.into_owned(),
);

let lexer = Lexer::new(
Expand Down Expand Up @@ -134,7 +151,7 @@ pub async fn minify(
} else {
builder.push_source(&src.into(), None);
}
Ok(builder.build().cell())
Ok(builder.build())
}

// From https://github.com/swc-project/swc/blob/11efd4e7c5e8081f8af141099d3459c3534c1e1d/crates/swc/src/lib.rs#L523-L560
Expand Down
Loading