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