Unverified Commit f9a12ba5 authored by Donny/강동윤's avatar Donny/강동윤 Committed by GitHub

fix(turbopack): Fix duplicate modules when tree shaking: true (#78203)

### What?

Use the `ModulePart::export` fragment instead of resolving it as an internal fragment. Additionally, this PR removes the concept of `ModulePart::InternalEvaluation` because it's not required anymore.

### Why?

Treating them as internal fragments causes a duplicate module identifier error.

### How?



Closes PACK-4367

---------
Co-authored-by: 's avatarBenjamin Woodruff <benjamin.woodruff@vercel.com>
parent 5133844c
......@@ -300,18 +300,14 @@ impl AssetIdent {
5_u8.deterministic_hash(&mut hasher);
id.deterministic_hash(&mut hasher);
}
ModulePart::InternalEvaluation(id) => {
6_u8.deterministic_hash(&mut hasher);
id.deterministic_hash(&mut hasher);
}
ModulePart::Locals => {
7_u8.deterministic_hash(&mut hasher);
6_u8.deterministic_hash(&mut hasher);
}
ModulePart::Exports => {
8_u8.deterministic_hash(&mut hasher);
7_u8.deterministic_hash(&mut hasher);
}
ModulePart::Facade => {
9_u8.deterministic_hash(&mut hasher);
8_u8.deterministic_hash(&mut hasher);
}
}
......
......@@ -3165,8 +3165,6 @@ pub enum ModulePart {
RenamedNamespace { export: RcStr },
/// A pointer to a specific part.
Internal(u32),
/// A pointer to a specific part, but with evaluation.
InternalEvaluation(u32),
/// The local declarations of a module.
Locals,
/// The whole exports of a module.
......@@ -3200,10 +3198,6 @@ impl ModulePart {
ModulePart::Internal(id)
}
pub fn internal_evaluation(id: u32) -> Self {
ModulePart::InternalEvaluation(id)
}
pub fn locals() -> Self {
ModulePart::Locals
}
......@@ -3230,7 +3224,6 @@ impl Display for ModulePart {
write!(f, "export * as {}", export)
}
ModulePart::Internal(id) => write!(f, "internal part {}", id),
ModulePart::InternalEvaluation(id) => write!(f, "internal part {}", id),
ModulePart::Locals => f.write_str("locals"),
ModulePart::Exports => f.write_str("exports"),
ModulePart::Facade => f.write_str("facade"),
......
......@@ -765,7 +765,7 @@ pub(crate) async fn analyse_ecmascript_module_internal(
ImportedSymbol::Symbol(name) => Some(ModulePart::export((&**name).into())),
ImportedSymbol::PartEvaluation(part_id) => {
should_add_evaluation = true;
Some(ModulePart::internal_evaluation(*part_id))
Some(ModulePart::internal(*part_id))
}
ImportedSymbol::Part(part_id) => Some(ModulePart::internal(*part_id)),
ImportedSymbol::Exports => Some(ModulePart::exports()),
......
......@@ -83,9 +83,7 @@ impl ModuleReference for EcmascriptModulePartReference {
| ModulePart::RenamedNamespace { .. } => Vc::upcast(
EcmascriptModuleFacadeModule::new(*self.module, part.clone()),
),
ModulePart::Export(..)
| ModulePart::Internal(..)
| ModulePart::InternalEvaluation(..) => {
ModulePart::Export(..) | ModulePart::Internal(..) => {
bail!(
"Unexpected ModulePart \"{}\" for EcmascriptModulePartReference",
part
......
......@@ -93,33 +93,31 @@ impl EcmascriptModulePartAsset {
/// of a pointer to the full module and the [ModulePart] pointing the part
/// of the module.
#[turbo_tasks::function]
pub async fn new(
fn new_raw(module: ResolvedVc<EcmascriptModuleAsset>, part: ModulePart) -> Vc<Self> {
Self {
full_module: module,
part,
}
.cell()
}
#[turbo_tasks::function]
pub async fn new_with_resolved_part(
module: ResolvedVc<EcmascriptModuleAsset>,
part: ModulePart,
) -> Result<Vc<Self>> {
if matches!(
part,
ModulePart::Internal(..)
| ModulePart::InternalEvaluation(..)
| ModulePart::Facade
| ModulePart::Exports
ModulePart::Internal(..) | ModulePart::Facade | ModulePart::Exports
) {
return Ok(EcmascriptModulePartAsset {
full_module: module,
part,
}
.cell());
return Ok(Self::new_raw(*module, part));
}
// This is a workaround to avoid creating duplicate assets for internal parts.
let split_result = split_module(*module).await?;
let part_id = get_part_id(&split_result, &part).await?;
Ok(EcmascriptModulePartAsset {
full_module: module,
part: ModulePart::internal(part_id),
}
.cell())
Ok(Self::new_raw(*module, ModulePart::internal(part_id)))
}
#[turbo_tasks::function]
......@@ -135,18 +133,22 @@ impl EcmascriptModulePartAsset {
ModulePart::Evaluation => {
// We resolve the module evaluation here to prevent duplicate assets.
let idx = *entrypoints.get(&Key::ModuleEvaluation).unwrap();
return Ok(Vc::upcast(EcmascriptModulePartAsset::new(
module,
ModulePart::InternalEvaluation(idx),
)));
return Ok(Vc::upcast(
EcmascriptModulePartAsset::new_with_resolved_part(
module,
ModulePart::internal(idx),
),
));
}
ModulePart::Export(export) => {
if entrypoints.contains_key(&Key::Export(export.clone())) {
return Ok(Vc::upcast(EcmascriptModulePartAsset::new(
module,
ModulePart::Export(export),
)));
return Ok(Vc::upcast(
EcmascriptModulePartAsset::new_with_resolved_part(
module,
ModulePart::Export(export),
),
));
}
let side_effect_free_packages = module.asset_context().side_effect_free_packages();
let source_module = Vc::upcast(module);
......@@ -201,10 +203,9 @@ impl EcmascriptModulePartAsset {
_ => (),
}
Ok(Vc::upcast(EcmascriptModulePartAsset::new(
module,
part.clone(),
)))
Ok(Vc::upcast(
EcmascriptModulePartAsset::new_with_resolved_part(module, part.clone()),
))
}
#[turbo_tasks::function]
......@@ -328,9 +329,9 @@ impl Module for EcmascriptModulePartAsset {
// This is an internal part that is not for evaluation, so we don't need to
// force-add it.
PartId::Internal(.., false) => return None,
PartId::Internal(part_id, true) => {
ModulePart::internal_evaluation(*part_id)
}
// Because of this we still need `PartId::Internal` to have `is_for_eval`
// flag.
PartId::Internal(part_id, true) => ModulePart::internal(*part_id),
PartId::Export(name) => ModulePart::export(name.clone()),
_ => unreachable!(
"PartId other than Internal and Export should not be used here"
......@@ -424,7 +425,8 @@ async fn only_effects(
module: Vc<Box<dyn EcmascriptChunkPlaceable>>,
) -> Result<Vc<Box<dyn EcmascriptChunkPlaceable>>> {
if let Some(module) = Vc::try_resolve_downcast_type::<EcmascriptModuleAsset>(module).await? {
let module = EcmascriptModulePartAsset::new(module, ModulePart::evaluation());
let module =
EcmascriptModulePartAsset::new_with_resolved_part(module, ModulePart::evaluation());
return Ok(Vc::upcast(module));
}
......
......@@ -384,9 +384,7 @@ async fn get_part_id(result: &SplitResult, part: &ModulePart) -> Result<u32> {
ModulePart::Evaluation => Key::ModuleEvaluation,
ModulePart::Export(export) => Key::Export(export.clone()),
ModulePart::Exports => Key::Exports,
ModulePart::Internal(part_id) | ModulePart::InternalEvaluation(part_id) => {
return Ok(*part_id)
}
ModulePart::Internal(part_id) => return Ok(*part_id),
ModulePart::Locals
| ModulePart::Facade
| ModulePart::RenamedExport { .. }
......
......@@ -37,9 +37,10 @@ impl ValueToString for EcmascriptModulePartReference {
impl ModuleReference for EcmascriptModulePartReference {
#[turbo_tasks::function]
async fn resolve_reference(&self) -> Result<Vc<ModuleResolveResult>> {
let module = EcmascriptModulePartAsset::new(*self.module, self.part.clone())
.to_resolved()
.await?;
let module =
EcmascriptModulePartAsset::new_with_resolved_part(*self.module, self.part.clone())
.to_resolved()
.await?;
Ok(*ModuleResolveResult::module(ResolvedVc::upcast(module)))
}
......
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4c35f_tests_snapshot_basic-tree-shake_export-named_input_index_343390a0.js",
"output/4c35f_tests_snapshot_basic-tree-shake_export-named_input_index_9180a415.js",
{},
{"otherChunks":["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_86188939._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/index.js [test] (ecmascript)"]}
{"otherChunks":["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_bf8be362._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_86188939._.js", {
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_bf8be362._.js", {
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/lib.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
......@@ -101,21 +101,6 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbo
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/module.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
var { g: global, __dirname } = __turbopack_context__;
{
__turbopack_context__.s({
"fakeCat": (()=>__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$named$2f$input$2f$module$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__1$3e$__["a"])
});
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$named$2f$input$2f$module$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__1$3e$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/module.js [test] (ecmascript) <internal part 1>");
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$named$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__2$3e$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/lib.js [test] (ecmascript) <internal part 2>");
;
;
;
;
}}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/module.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
var { g: global, __dirname } = __turbopack_context__;
{
__turbopack_context__.s({
......@@ -140,4 +125,4 @@ console.log(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$
}}),
}]);
//# sourceMappingURL=b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_86188939._.js.map
\ No newline at end of file
//# sourceMappingURL=b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-named_input_bf8be362._.js.map
\ No newline at end of file
......@@ -10,6 +10,5 @@
{"offset": {"line": 75, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 87, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 105, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 120, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 135, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/index.js"],"sourcesContent":["import { fakeCat } from \"./module\";\n\n\nconsole.log(fakeCat)\n"],"names":[],"mappings":";AAAA;;AAGA,QAAQ,GAAG,CAAC,mPAAA,CAAA,UAAO"}}]
{"offset": {"line": 120, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-named/input/index.js"],"sourcesContent":["import { fakeCat } from \"./module\";\n\n\nconsole.log(fakeCat)\n"],"names":[],"mappings":";AAAA;;AAGA,QAAQ,GAAG,CAAC,mPAAA,CAAA,UAAO"}}]
}
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4c35f_tests_snapshot_basic-tree-shake_export-namespace_input_index_3c992eb1.js",
"output/4c35f_tests_snapshot_basic-tree-shake_export-namespace_input_index_7c39b437.js",
{},
{"otherChunks":["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e9b22068._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js [test] (ecmascript)"]}
{"otherChunks":["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_0aae49e0._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e9b22068._.js", {
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push(["output/b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_0aae49e0._.js", {
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
......@@ -200,21 +200,6 @@ var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbo
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/module.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
var { g: global, __dirname } = __turbopack_context__;
{
__turbopack_context__.s({
"lib": (()=>__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$module$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__1$3e$__["a"])
});
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$module$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__1$3e$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/module.js [test] (ecmascript) <internal part 1>");
var __TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$2f$turbopack$2d$tests$2f$tests$2f$snapshot$2f$basic$2d$tree$2d$shake$2f$export$2d$namespace$2f$input$2f$lib$2e$js__$5b$test$5d$__$28$ecmascript$29$__$3c$internal__part__2$3e$__ = __turbopack_context__.i("[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/lib.js [test] (ecmascript) <internal part 2>");
;
;
;
;
}}),
"[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/module.js [test] (ecmascript) <internal part 0>": ((__turbopack_context__) => {
"use strict";
var { g: global, __dirname } = __turbopack_context__;
{
__turbopack_context__.s({
......@@ -239,4 +224,4 @@ console.log(__TURBOPACK__imported__module__$5b$project$5d2f$turbopack$2f$crates$
}}),
}]);
//# sourceMappingURL=b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_e9b22068._.js.map
\ No newline at end of file
//# sourceMappingURL=b1abf_turbopack-tests_tests_snapshot_basic-tree-shake_export-namespace_input_0aae49e0._.js.map
\ No newline at end of file
......@@ -14,6 +14,5 @@
{"offset": {"line": 169, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 189, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 204, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 219, "column": 0}, "map": {"version":3,"sources":[],"names":[],"mappings":""}},
{"offset": {"line": 234, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js"],"sourcesContent":["import {lib} from './module'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";AAAA;;AAEA,QAAQ,GAAG,CAAC,uPAAA,CAAA,MAAG,CAAC,GAAG"}}]
{"offset": {"line": 219, "column": 0}, "map": {"version":3,"sources":["turbopack:///[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/export-namespace/input/index.js"],"sourcesContent":["import {lib} from './module'\n\nconsole.log(lib.cat)\n"],"names":[],"mappings":";AAAA;;AAEA,QAAQ,GAAG,CAAC,uPAAA,CAAA,MAAG,CAAC,GAAG"}}]
}
\ No newline at end of file
(globalThis.TURBOPACK = globalThis.TURBOPACK || []).push([
"output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_acd812ea.js",
"output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_ffcdd55c.js",
{},
{"otherChunks":["output/4c35f_tests_snapshot_basic-tree-shake_tree-shake-test-1_input_index_09a16221.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/basic-tree-shake/tree-shake-test-1/input/index.js [test] (ecmascript)"]}
]);
......
......@@ -139,10 +139,7 @@ async fn apply_module_type(
ResolvedVc::upcast(builder.build().to_resolved().await?)
} else {
let module = builder.build().resolve().await?;
if matches!(
&part,
Some(ModulePart::Evaluation | ModulePart::InternalEvaluation(..))
) {
if matches!(&part, Some(ModulePart::Evaluation)) {
// Skip the evaluation part if the module is marked as side effect free.
let side_effect_free_packages = module_asset_context
.side_effect_free_packages()
......
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