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

Fix flat export registration through preserved aliases

Co-authored-by: 's avatarNiklas Mischkulnig <4586894+mischnic@users.noreply.github.com>
parent 33e50fa7
...@@ -1369,11 +1369,15 @@ impl EcmascriptModuleContent { ...@@ -1369,11 +1369,15 @@ impl EcmascriptModuleContent {
} else { } else {
None None
}; };
// A deoptimized factory entry keeps its own namespace. Register the shared table // Any exposed deoptimized member can instantiate this factory through its alias.
// under an eligible member instead, without changing the factory's identity. // Always target the flat namespace explicitly in mixed groups, even when it is the
// original entry, so registration never writes to and seals an alias's namespace.
let flat_export_id = if let Some(group) = &flat_group let flat_export_id = if let Some(group) = &flat_group
&& group.entry != first_entry && (group.entry != first_entry
{ || modules.iter().any(|(module, exposure)| {
*exposure == MergeableModuleExposure::External
&& !group.names.contains_key(module)
})) {
Some(group.entry.chunk_item_id(*options.chunking_context).await?) Some(group.entry.chunk_item_id(*options.chunking_context).await?)
} else { } else {
None None
......
...@@ -73,13 +73,11 @@ pub async fn flat_export_group( ...@@ -73,13 +73,11 @@ pub async fn flat_export_group(
.get(&ResolvedVc::upcast(module)) .get(&ResolvedVc::upcast(module))
.await? .await?
.as_deref() .as_deref()
{ && let Some(flat_group) = *flat_group_exports(**group, chunking_context).await?
if let Some(flat_group) = *flat_group_exports(**group, chunking_context).await?
&& flat_group.await?.names.contains_key(&module) && flat_group.await?.names.contains_key(&module)
{ {
return Ok(Vc::cell(Some(flat_group))); return Ok(Vc::cell(Some(flat_group)));
} }
}
Ok(Vc::cell(None)) Ok(Vc::cell(None))
} }
......
globalThis.flatAliasEvaluations = (globalThis.flatAliasEvaluations || 0) + 1
export const someLongExportName = 'flat'
export const a = 'flat-short'
export let liveValue = 0
export function increment() {
liveValue++
}
import { someLongExportName, a, liveValue } from './flat'
import * as preserved from './preserved'
it('keeps the preserved namespace in the merged group distinct', async () => {
const lazy = await import('./lazy')
expect(lazy.read()).toEqual([someLongExportName, a, 0, preserved])
expect(Object.keys(preserved).sort()).toEqual([
'increment',
'liveValue',
'someLongExportName',
])
lazy.update()
expect(liveValue).toBe(1)
expect(preserved.liveValue).toBe(1)
expect(lazy.read()).toEqual([someLongExportName, a, 1, preserved])
})
it('registers flat exports when a preserved alias starts the factory', () => {
// Loaded at runtime from outside the bundle; `eval` or bundled requires here would stop
// this module's import sequence from being merged.
process.mainModule.require(process.cwd() + '/replay-alias-first.js')()
})
import { someLongExportName, a, liveValue, increment } from './flat'
import * as preserved from './preserved'
export const read = () => [someLongExportName, a, liveValue, preserved]
export function update() {
increment()
preserved.increment()
}
globalThis.preservedAliasEvaluations =
(globalThis.preservedAliasEvaluations || 0) + 1
export const someLongExportName = 'preserved'
export let liveValue = 0
export function increment() {
liveValue++
}
{
"mangleExportNames": true,
"mangleViaMaterializedNamespaceObject": false,
"crossModuleConstants": false,
"scopeHoisting": true
}
// Replays the merged flat/preserved factory from `output/` in a fresh runtime, so the preserved
// member's alias ID is the first to instantiate it. Kept outside `input/` so it is not bundled.
const assert = require('assert/strict')
const fs = require('fs')
const path = require('path')
const id = (name) =>
`[project]/turbopack/crates/turbopack-tests/tests/execution/turbopack/exports/mangle-hoisted-alias-first/input/${name}.js [test] (ecmascript)`
module.exports = function replayAliasFirst() {
const testDir = process.cwd()
const createRuntime = require(
path.resolve(
testDir,
'../../../../snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js'
)
)
const output = path.join(testDir, 'output')
const chunk = fs
.readdirSync(output)
.filter((file) => file.endsWith('.js'))
.map((file) => path.join(output, file))
.find((file) => {
const factories = require(file)
if (!Array.isArray(factories)) return false
const index = factories.indexOf(id('flat'))
return index !== -1 && factories[index + 1] === id('preserved')
})
assert(chunk, 'expected flat.js and preserved.js to share a merged factory')
const flatEvaluations = globalThis.flatAliasEvaluations
const preservedEvaluations = globalThis.preservedAliasEvaluations
const runtime = createRuntime('alias-first-test')
runtime.c(chunk)
const namespace = runtime.m(id('preserved')).exports
assert.deepEqual(Object.keys(namespace).sort(), [
'increment',
'liveValue',
'someLongExportName',
])
assert.equal(namespace.someLongExportName, 'preserved')
const flat = runtime.m(id('flat')).exports
assert.notEqual(flat, namespace)
assert(Object.values(flat).includes('flat'))
assert(Object.values(flat).includes('flat-short'))
const liveKey = Object.keys(flat).find(
(key) => flat[key] === 0 && Object.getOwnPropertyDescriptor(flat, key).get
)
assert(liveKey, 'expected a live getter in the shared flat table')
const increment = Object.values(flat).find(
(value) => typeof value === 'function'
)
increment()
namespace.increment()
assert.equal(flat[liveKey], 1)
assert.equal(namespace.liveValue, 1)
assert.equal(runtime.m(id('preserved')).exports, namespace)
assert.equal(runtime.m(id('flat')).exports, flat)
assert.equal(globalThis.flatAliasEvaluations, flatEvaluations + 1)
assert.equal(globalThis.preservedAliasEvaluations, preservedEvaluations + 1)
}
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