Unverified Commit 6466487e authored by vercel-fleet-prod[bot]'s avatar vercel-fleet-prod[bot] Committed by GitHub

Prepare flat export bindings before identifier generation

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 5d19377e
...@@ -1206,10 +1206,10 @@ impl ChunkingContext for UseCacheContentHashChunkingContext { ...@@ -1206,10 +1206,10 @@ impl ChunkingContext for UseCacheContentHashChunkingContext {
} }
#[turbo_tasks::function] #[turbo_tasks::function]
fn is_module_merging_enabled(self: Vc<Self>) -> Result<Vc<bool>> { fn is_module_merging_enabled(self: Vc<Self>) -> Vc<bool> {
bail!( // Hash individual factories, independently of which modules the rest of the build
"is_module_merging_enabled should not be called in UseCacheContentHashChunkingContext" // places in a scope-hoisting group. Code generation must not request that analysis.
) Vc::cell(false)
} }
#[turbo_tasks::function] #[turbo_tasks::function]
......
...@@ -292,6 +292,27 @@ export function generateStaticParams() { ...@@ -292,6 +292,27 @@ export function generateStaticParams() {
expect(second).toEqual(first) expect(second).toEqual(first)
}) })
it('codeHash is independent of scope hoisting', async () => {
expect((await next.build()).exitCode).toBe(0)
const before = await getCodeHashes(next)
expect(before.some((entry) => typeof entry.codeHash === 'string')).toBe(
true
)
await next.patchFile(
'next.config.js',
(config) =>
config.replace(
'experimental: {',
'experimental: { turbopackScopeHoisting: false,'
),
async () => {
expect((await next.build()).exitCode).toBe(0)
expect(await getCodeHashes(next)).toEqual(before)
}
)
})
it("changes when the action's own code changes", async () => { it("changes when the action's own code changes", async () => {
await next.build() await next.build()
const before = await getCodeHashes(next, ['app/use-cache/page']) const before = await getCodeHashes(next, ['app/use-cache/page'])
......
...@@ -126,13 +126,15 @@ impl AsyncModule { ...@@ -126,13 +126,15 @@ impl AsyncModule {
ReferencedAsset::External(_, ExternalType::EcmaScriptModule) => { ReferencedAsset::External(_, ExternalType::EcmaScriptModule) => {
if self.import_externals { if self.import_externals {
referenced_asset referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
None, None,
ScopeHoistingContext::None, ScopeHoistingContext::None,
) )
.await? .await?
.get_ident(chunking_context)
.await?
.map(|i| i.into_module_namespace_ident().unwrap()) .map(|i| i.into_module_namespace_ident().unwrap())
.map(|(i, ctx)| (i, ctx.unwrap_or_default().into())) .map(|(i, ctx)| (i, ctx.unwrap_or_default().into()))
} else { } else {
...@@ -145,13 +147,15 @@ impl AsyncModule { ...@@ -145,13 +147,15 @@ impl AsyncModule {
.contains(&ResolvedVc::upcast(*placeable)) .contains(&ResolvedVc::upcast(*placeable))
{ {
referenced_asset referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
None, None,
ScopeHoistingContext::None, ScopeHoistingContext::None,
) )
.await? .await?
.get_ident(chunking_context)
.await?
.map(|i| i.into_module_namespace_ident().unwrap()) .map(|i| i.into_module_namespace_ident().unwrap())
.map(|(i, ctx)| (i, ctx.unwrap_or_default().into())) .map(|(i, ctx)| (i, ctx.unwrap_or_default().into()))
} else { } else {
......
...@@ -205,15 +205,62 @@ impl ReferencedAssetIdent { ...@@ -205,15 +205,62 @@ impl ReferencedAssetIdent {
} }
} }
impl ReferencedAsset { /// A binding prepared for code generation. Target selection and re-export traversal happen
/// before identifier formatting, while the original module is still available for local
/// binding resolution and export-name allocation.
pub enum PreparedBinding {
LocalBinding {
ident: RcStr,
ctxt: SyntaxContext,
liveness: Liveness,
},
Module {
import_source: ImportSource,
ctxt: Option<SyntaxContext>,
export: Option<RcStr>,
},
None,
}
impl PreparedBinding {
pub async fn get_ident( pub async fn get_ident(
self,
chunking_context: Vc<Box<dyn ChunkingContext>>,
) -> Result<Option<ReferencedAssetIdent>> {
Ok(match self {
Self::LocalBinding {
ident,
ctxt,
liveness,
} => Some(ReferencedAssetIdent::LocalBinding {
ident,
ctxt,
liveness,
}),
Self::Module {
import_source,
ctxt,
export,
} => Some(ReferencedAssetIdent::Module {
namespace_ident: import_source.get_namespace_ident(chunking_context).await?,
ctxt,
export,
import_source,
}),
Self::None => None,
})
}
}
impl ReferencedAsset {
pub async fn prepare_binding(
&self, &self,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis, module_merging: ModuleMergingAnalysis,
export: Option<RcStr>, export: Option<RcStr>,
scope_hoisting_context: ScopeHoistingContext<'_>, scope_hoisting_context: ScopeHoistingContext<'_>,
) -> Result<Option<ReferencedAssetIdent>> { ) -> Result<PreparedBinding> {
self.get_ident_inner( self.prepare_binding_inner(
chunking_context, chunking_context,
module_merging, module_merging,
export, export,
...@@ -223,14 +270,14 @@ impl ReferencedAsset { ...@@ -223,14 +270,14 @@ impl ReferencedAsset {
.await .await
} }
async fn get_ident_inner( async fn prepare_binding_inner(
&self, &self,
chunking_context: Vc<Box<dyn ChunkingContext>>, chunking_context: Vc<Box<dyn ChunkingContext>>,
module_merging: ModuleMergingAnalysis, module_merging: ModuleMergingAnalysis,
export: Option<RcStr>, export: Option<RcStr>,
scope_hoisting_context: ScopeHoistingContext<'_>, scope_hoisting_context: ScopeHoistingContext<'_>,
initial: Option<&ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>>, initial: Option<&ResolvedVc<Box<dyn EcmascriptChunkPlaceable>>>,
) -> Result<Option<ReferencedAssetIdent>> { ) -> Result<PreparedBinding> {
Ok(match self { Ok(match self {
ReferencedAsset::Some(asset) => { ReferencedAsset::Some(asset) => {
if let Some(ctxt) = scope_hoisting_context.get_module_syntax_context(*asset) if let Some(ctxt) = scope_hoisting_context.get_module_syntax_context(*asset)
...@@ -244,11 +291,11 @@ impl ReferencedAsset { ...@@ -244,11 +291,11 @@ impl ReferencedAsset {
// A local binding in a module that is merged in the same group. Use the // A local binding in a module that is merged in the same group. Use the
// export name as identifier, it will be replaced with the actual // export name as identifier, it will be replaced with the actual
// variable name during AST merging. // variable name during AST merging.
return Ok(Some(ReferencedAssetIdent::LocalBinding { return Ok(PreparedBinding::LocalBinding {
ident: export.clone(), ident: export.clone(),
ctxt, ctxt,
liveness: *liveness, liveness: *liveness,
})); });
} }
Some(b @ EsmExport::ImportedBinding(esm_ref, _, _)) Some(b @ EsmExport::ImportedBinding(esm_ref, _, _))
| Some(b @ EsmExport::ImportedNamespace(esm_ref)) => { | Some(b @ EsmExport::ImportedNamespace(esm_ref)) => {
...@@ -275,13 +322,13 @@ impl ReferencedAsset { ...@@ -275,13 +322,13 @@ impl ReferencedAsset {
} }
.resolved_cell() .resolved_cell()
.emit(); .emit();
return Ok(None); return Ok(PreparedBinding::None);
} }
// If the target module is still in the same group, we can // If the target module is still in the same group, we can
// refer it locally, otherwise it will be imported // refer it locally, otherwise it will be imported
return Ok( return Ok(
match Box::pin(referenced_asset.get_ident_inner( match Box::pin(referenced_asset.prepare_binding_inner(
chunking_context, chunking_context,
module_merging, module_merging,
imported, imported,
...@@ -290,20 +337,17 @@ impl ReferencedAsset { ...@@ -290,20 +337,17 @@ impl ReferencedAsset {
)) ))
.await? .await?
{ {
Some(ReferencedAssetIdent::Module { PreparedBinding::Module {
namespace_ident, // This import is inserted in the module containing the
// Overwrite the context. This import isn't // reexport, not the module using the import.
// inserted in the module that uses the import,
// but in the module containing the reexport
ctxt: None, ctxt: None,
export, export,
import_source, import_source,
}) => Some(ReferencedAssetIdent::Module { } => PreparedBinding::Module {
namespace_ident,
ctxt: Some(ctxt), ctxt: Some(ctxt),
export, export,
import_source, import_source,
}), },
ident => ident, ident => ident,
}, },
); );
...@@ -323,13 +367,10 @@ impl ReferencedAsset { ...@@ -323,13 +367,10 @@ impl ReferencedAsset {
*asset *asset
}; };
let import_source = ImportSource::Module { asset: target }; let import_source = ImportSource::Module { asset: target };
Some(ReferencedAssetIdent::Module { PreparedBinding::Module {
namespace_ident: import_source.get_namespace_ident(chunking_context).await?,
ctxt: None, ctxt: None,
// The target module may emit its exports under shortened keys. This is the // Allocate against the original module, not the shared factory target,
// only place a cross-module export access is materialized, and it resolves // using the same mapping as the producing module.
// the same map the producing module uses (see
// `EsmExports::code_generation`), so the two always agree.
export: match &export { export: match &export {
Some(export) => Some( Some(export) => Some(
generated_export_key(*asset, chunking_context, module_merging, export) generated_export_key(*asset, chunking_context, module_merging, export)
...@@ -338,19 +379,18 @@ impl ReferencedAsset { ...@@ -338,19 +379,18 @@ impl ReferencedAsset {
None => None, None => None,
}, },
import_source, import_source,
}) }
} }
ReferencedAsset::External(request, ty) => { ReferencedAsset::External(request, ty) => {
let import_source = ImportSource::External { let import_source = ImportSource::External {
request: request.clone(), request: request.clone(),
ty: *ty, ty: *ty,
}; };
Some(ReferencedAssetIdent::Module { PreparedBinding::Module {
namespace_ident: import_source.get_namespace_ident(chunking_context).await?,
ctxt: None, ctxt: None,
export, export,
import_source, import_source,
}) }
} }
ReferencedAsset::NonPlaceable(module) => { ReferencedAsset::NonPlaceable(module) => {
// The module exists but has no ECMAScript bindings, so there is // The module exists but has no ECMAScript bindings, so there is
...@@ -378,9 +418,11 @@ impl ReferencedAsset { ...@@ -378,9 +418,11 @@ impl ReferencedAsset {
} }
.resolved_cell() .resolved_cell()
.emit(); .emit();
None PreparedBinding::None
}
ReferencedAsset::None | ReferencedAsset::Empty | ReferencedAsset::Unresolvable => {
PreparedBinding::None
} }
ReferencedAsset::None | ReferencedAsset::Empty | ReferencedAsset::Unresolvable => None,
}) })
} }
...@@ -896,7 +938,7 @@ impl EsmAssetReference { ...@@ -896,7 +938,7 @@ impl EsmAssetReference {
} else { } else {
'import: { 'import: {
let ident = referenced_asset let ident = referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
this.export_name this.export_name
...@@ -905,6 +947,8 @@ impl EsmAssetReference { ...@@ -905,6 +947,8 @@ impl EsmAssetReference {
.cloned(), .cloned(),
scope_hoisting_context, scope_hoisting_context,
) )
.await?
.get_ident(chunking_context)
.await?; .await?;
// `referenced_asset` must not be used past this point: the ident // `referenced_asset` must not be used past this point: the ident
// carries everything about the import // carries everything about the import
......
...@@ -137,13 +137,15 @@ impl EsmBindings { ...@@ -137,13 +137,15 @@ impl EsmBindings {
imported_module => match exports_cache.entry(export.clone()) { imported_module => match exports_cache.entry(export.clone()) {
std::collections::hash_map::Entry::Vacant(entry) => { std::collections::hash_map::Entry::Vacant(entry) => {
let ident: ImportedIdent = imported_module let ident: ImportedIdent = imported_module
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
export.clone(), export.clone(),
scope_hoisting_context, scope_hoisting_context,
) )
.await? .await?
.get_ident(chunking_context)
.await?
.map_or(ImportedIdent::Unresolvable, ImportedIdent::Module); .map_or(ImportedIdent::Unresolvable, ImportedIdent::Module);
entry.insert(ident.clone()); entry.insert(ident.clone());
ident ident
......
...@@ -660,13 +660,15 @@ async fn compact_reference_target( ...@@ -660,13 +660,15 @@ async fn compact_reference_target(
import_source: ImportSource::Module { asset }, import_source: ImportSource::Module { asset },
.. ..
}) = referenced_asset }) = referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
None, None,
scope_hoisting_context, scope_hoisting_context,
) )
.await? .await?
.get_ident(chunking_context)
.await?
else { else {
return Ok(None); return Ok(None);
}; };
...@@ -788,13 +790,15 @@ async fn build_compact_reexports( ...@@ -788,13 +790,15 @@ async fn build_compact_reexports(
export: Some(imported_key), export: Some(imported_key),
import_source: ImportSource::Module { asset }, import_source: ImportSource::Module { asset },
}) = referenced_asset }) = referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
Some(imported_name.clone()), Some(imported_name.clone()),
scope_hoisting_context, scope_hoisting_context,
) )
.await? .await?
.get_ident(chunking_context)
.await?
else { else {
// Scope-hoisted into a local binding, an external, or a namespace re-export: none of // Scope-hoisted into a local binding, an external, or a namespace re-export: none of
// these are a property read off an imported namespace. // these are a property read off an imported namespace.
...@@ -1185,7 +1189,9 @@ impl EsmExports { ...@@ -1185,7 +1189,9 @@ impl EsmExports {
let referenced_asset = let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?; ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset referenced_asset
.get_ident(chunking_context, module_merging, Some(name.clone()), scope_hoisting_context) .prepare_binding(chunking_context, module_merging, Some(name.clone()), scope_hoisting_context)
.await?
.get_ident(chunking_context)
.await? .await?
.map(|ident| { .map(|ident| {
let expr = ident.as_expr_individual(DUMMY_SP); let expr = ident.as_expr_individual(DUMMY_SP);
...@@ -1250,13 +1256,15 @@ impl EsmExports { ...@@ -1250,13 +1256,15 @@ impl EsmExports {
let referenced_asset = let referenced_asset =
ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?; ReferencedAsset::from_resolve_result(esm_ref.resolve_reference()).await?;
referenced_asset referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
None, None,
scope_hoisting_context, scope_hoisting_context,
) )
.await? .await?
.get_ident(chunking_context)
.await?
.map(|ident| { .map(|ident| {
let imported = ident.as_expr(DUMMY_SP, false); let imported = ident.as_expr(DUMMY_SP, false);
if early_exports { if early_exports {
......
...@@ -163,12 +163,14 @@ impl ModuleHotReferenceCodeGen { ...@@ -163,12 +163,14 @@ impl ModuleHotReferenceCodeGen {
match &referenced_asset { match &referenced_asset {
ReferencedAsset::Some(asset) => { ReferencedAsset::Some(asset) => {
let ident = referenced_asset let ident = referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
None, None,
scope_hoisting_context, scope_hoisting_context,
) )
.await?
.get_ident(chunking_context)
.await?; .await?;
if let Some((namespace_ident, ctxt)) = if let Some((namespace_ident, ctxt)) =
ident.and_then(|i| i.into_module_namespace_ident()) ident.and_then(|i| i.into_module_namespace_ident())
......
...@@ -205,7 +205,7 @@ impl EcmascriptModulePartReference { ...@@ -205,7 +205,7 @@ impl EcmascriptModulePartReference {
// hoisting group (unless it's a namespace import) // hoisting group (unless it's a namespace import)
} else { } else {
let ident = referenced_asset let ident = referenced_asset
.get_ident( .prepare_binding(
chunking_context, chunking_context,
module_merging, module_merging,
match export_usage { match export_usage {
...@@ -218,6 +218,8 @@ impl EcmascriptModulePartReference { ...@@ -218,6 +218,8 @@ impl EcmascriptModulePartReference {
scope_hoisting_context, scope_hoisting_context,
) )
.await? .await?
.get_ident(chunking_context)
.await?
.context("part module reference should have an ident")?; .context("part module reference should have an ident")?;
match ident { match ident {
......
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