Unverified Commit 70149d7a authored by Luke Sandberg's avatar Luke Sandberg Committed by GitHub

[turbopack] standardize chunk entry names for webpack and postcss loaders (#94256)

### What?

Use the `chunk_path` api for computing entrypoint filepath names for our
node worker pool files. For postcss loaders ensure that the config
filepath is part of the module ident of the transformer.

### Why?

I was investigating a bug in reported in next 16.2.6
(https://vercel.slack.com/archives/C09GRCW5MQR/p1780088728756579) and
noticed that the chunk entry path construction logic was unusual. It
happens to be that https://github.com/vercel/next.js/pull/91338 fixed
the reported bug (just not released yet). This migrates all entry
construction to the standard form
parent 0cc8234b
...@@ -9,13 +9,12 @@ use serde::{Deserialize, Serialize, de::DeserializeOwned}; ...@@ -9,13 +9,12 @@ use serde::{Deserialize, Serialize, de::DeserializeOwned};
use serde_json::Value as JsonValue; use serde_json::Value as JsonValue;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, FxIndexMap, OperationVc, PrettyPrintError, ResolvedVc, TryJoinIterExt, Completion, FxIndexMap, OperationVc, PrettyPrintError, ResolvedVc, TryJoinIterExt, Vc,
ValueToString, Vc, duration_span, fxindexmap, parallel::available_parallelism, duration_span, fxindexmap, parallel::available_parallelism,
resolve_strongly_consistent_and_take_and_apply_effects, trace::TraceRawVcs, resolve_strongly_consistent_and_take_and_apply_effects, trace::TraceRawVcs,
}; };
use turbo_tasks_env::{EnvMap, ProcessEnv}; use turbo_tasks_env::{EnvMap, ProcessEnv};
use turbo_tasks_fs::{File, FileContent, FileSystemPath, to_sys_path}; use turbo_tasks_fs::{File, FileContent, FileSystemPath, to_sys_path};
use turbo_tasks_hash::{DeterministicHash, Xxh3Hash64Hasher};
use turbopack_core::{ use turbopack_core::{
asset::AssetContent, asset::AssetContent,
changed::content_changed, changed::content_changed,
...@@ -151,14 +150,15 @@ async fn emit_evaluate_pool_assets_operation( ...@@ -151,14 +150,15 @@ async fn emit_evaluate_pool_assets_operation(
main_entry_ident, main_entry_ident,
} = &*entries.await?; } = &*entries.await?;
let module_ident = main_entry_ident.to_string().await?; let entrypoint = chunking_context
let module_ident_hash = { .chunk_path(
let mut hasher = Xxh3Hash64Hasher::new(); None,
module_ident.deterministic_hash(&mut hasher); **main_entry_ident,
hasher.finish() Some(rcstr!("pool_entry")),
}; rcstr!(".js"),
let file_name = format!("{module_ident_hash:016x}.js"); )
let entrypoint = chunking_context.output_root().await?.join(&file_name)?; .owned()
.await?;
let bootstrap = chunking_context.root_entry_chunk_group_asset( let bootstrap = chunking_context.root_entry_chunk_group_asset(
entrypoint.clone(), entrypoint.clone(),
......
...@@ -5,6 +5,7 @@ use serde::Deserialize; ...@@ -5,6 +5,7 @@ use serde::Deserialize;
use turbo_rcstr::{RcStr, rcstr}; use turbo_rcstr::{RcStr, rcstr};
use turbo_tasks::{ use turbo_tasks::{
Completion, Completions, ResolvedVc, TryFlatJoinIterExt, Vc, fxindexmap, trace::TraceRawVcs, Completion, Completions, ResolvedVc, TryFlatJoinIterExt, Vc, fxindexmap, trace::TraceRawVcs,
turbofmt,
}; };
use turbo_tasks_fs::{ use turbo_tasks_fs::{
File, FileContent, FileSystemEntryType, FileSystemPath, json::parse_json_with_source_context, File, FileContent, FileSystemEntryType, FileSystemPath, json::parse_json_with_source_context,
...@@ -342,8 +343,7 @@ pub(crate) async fn config_loader_source( ...@@ -342,8 +343,7 @@ pub(crate) async fn config_loader_source(
project_path: FileSystemPath, project_path: FileSystemPath,
postcss_config_path: FileSystemPath, postcss_config_path: FileSystemPath,
) -> Result<Vc<Box<dyn Source>>> { ) -> Result<Vc<Box<dyn Source>>> {
let postcss_config_path_value = postcss_config_path.clone(); let postcss_config_path_filename = postcss_config_path.file_name();
let postcss_config_path_filename = postcss_config_path_value.file_name();
if postcss_config_path_filename == "package.json" { if postcss_config_path_filename == "package.json" {
return Ok(Vc::upcast(JsonSource::new( return Ok(Vc::upcast(JsonSource::new(
...@@ -353,9 +353,7 @@ pub(crate) async fn config_loader_source( ...@@ -353,9 +353,7 @@ pub(crate) async fn config_loader_source(
))); )));
} }
if postcss_config_path_value.path.ends_with(".json") if postcss_config_path.path.ends_with(".json") || postcss_config_path_filename == ".postcssrc" {
|| postcss_config_path_filename == ".postcssrc"
{
return Ok(Vc::upcast(JsonSource::new( return Ok(Vc::upcast(JsonSource::new(
postcss_config_path, postcss_config_path,
Vc::cell(None), Vc::cell(None),
...@@ -364,11 +362,11 @@ pub(crate) async fn config_loader_source( ...@@ -364,11 +362,11 @@ pub(crate) async fn config_loader_source(
} }
// We can only load js files with `import()`. // We can only load js files with `import()`.
if !postcss_config_path_value.path.ends_with(".js") { if !postcss_config_path.path.ends_with(".js") {
return Ok(Vc::upcast(FileSource::new(postcss_config_path))); return Ok(Vc::upcast(FileSource::new(postcss_config_path)));
} }
let Some(config_path) = project_path.get_relative_path_to(&postcss_config_path_value) else { let Some(config_path) = project_path.get_relative_path_to(&postcss_config_path) else {
bail!("Unable to get relative path to postcss config"); bail!("Unable to get relative path to postcss config");
}; };
...@@ -405,7 +403,7 @@ async fn postcss_executor( ...@@ -405,7 +403,7 @@ async fn postcss_executor(
) -> Result<Vc<ProcessResult>> { ) -> Result<Vc<ProcessResult>> {
let config_asset = asset_context let config_asset = asset_context
.process( .process(
config_loader_source(project_path, postcss_config_path), config_loader_source(project_path, postcss_config_path.clone()),
ReferenceType::Entry(EntryReferenceSubType::Undefined), ReferenceType::Entry(EntryReferenceSubType::Undefined),
) )
.module() .module()
...@@ -413,10 +411,11 @@ async fn postcss_executor( ...@@ -413,10 +411,11 @@ async fn postcss_executor(
.await?; .await?;
Ok(asset_context.process( Ok(asset_context.process(
Vc::upcast(FileSource::new( Vc::upcast(FileSource::new_with_query(
embed_file_path(rcstr!("transforms/postcss.ts")) embed_file_path(rcstr!("transforms/postcss.ts"))
.owned() .owned()
.await?, .await?,
turbofmt!("?config={postcss_config_path}").await?,
)), )),
ReferenceType::Internal(ResolvedVc::cell(fxindexmap! { ReferenceType::Internal(ResolvedVc::cell(fxindexmap! {
rcstr!("CONFIG") => config_asset rcstr!("CONFIG") => config_asset
......
...@@ -29,6 +29,7 @@ use turbopack_core::{ ...@@ -29,6 +29,7 @@ use turbopack_core::{
file_source::FileSource, file_source::FileSource,
ident::AssetIdent, ident::AssetIdent,
issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString}, issue::{Issue, IssueExt, IssueSeverity, IssueSource, IssueStage, StyledString},
module::Module,
module_graph::{ module_graph::{
ModuleGraph, SingleModuleGraph, ModuleGraph, SingleModuleGraph,
chunk_group_info::{ChunkGroup, ChunkGroupEntry}, chunk_group_info::{ChunkGroup, ChunkGroupEntry},
...@@ -762,8 +763,16 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -762,8 +763,16 @@ impl EvaluateContext for WebpackLoaderContext {
.await?; .await?;
// Generate a full Node.js bundle using the real runtime // Generate a full Node.js bundle using the real runtime
let output_root = self.chunking_context.output_root().owned().await?; let entry_path = self
let entry_path = output_root.join("importModule.js")?; .chunking_context
.chunk_path(
None,
module.ident(),
Some(rcstr!("importModule")),
rcstr!(".js"),
)
.owned()
.await?;
let bootstrap = self.chunking_context.root_entry_chunk_group_asset( let bootstrap = self.chunking_context.root_entry_chunk_group_asset(
entry_path.clone(), entry_path.clone(),
...@@ -780,6 +789,7 @@ impl EvaluateContext for WebpackLoaderContext { ...@@ -780,6 +789,7 @@ impl EvaluateContext for WebpackLoaderContext {
true, true,
) )
.await?; .await?;
let output_root = self.chunking_context.output_root().owned().await?;
let mut chunks = Vec::new(); let mut chunks = Vec::new();
for asset in all_assets { for asset in all_assets {
......
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