Unverified Commit 3883a8d9 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: disable tree shaking for tracing (#85722)

We had tree shaking enabled for NFT.
That is incorrect however, as even unused imports (if you don't use any of the imported bindings, and the file is marked as `sideEffects: false`) are still needed at runtime, since Node.js will execute everything anyway.

- I made the `treeShakingMode: None` explicit now (though that was already the `Default::default` anyway)
- The real problem was that we still dropped side-effect free `ModulePart::Evaluation` references even when tree shaking was disabled.

Closes #85172
Fixes PACK-5756
parent 5df64654
export default function Root({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
import getValue from 'foo'
export default function Page() {
return <p>{getValue()}</p>
}
import value from './value.js'
import './side-effect.js'
export default function getValue() {
return value
}
{
"name": "foo",
"type": "module",
"version": "0.0.0",
"sideEffects": false
}
// some noop, should still resolve and not crash though
/**
* @type {import('next').NextConfig}
*/
const nextConfig = {
serverExternalPackages: ['foo'],
}
module.exports = nextConfig
import { nextTestSetup } from 'e2e-utils'
describe('standalone mode - tracing-side-effects-false', () => {
const dependencies = require('./package.json').dependencies
const { next, skipped } = nextTestSetup({
files: __dirname,
dependencies,
skipDeployment: true,
skipStart: true,
})
if (skipped) {
return
}
it('should trace sideeffect imports even when sideEffects is false', async () => {
let { exitCode } = await next.build()
expect(exitCode).toBe(0)
let trace = await next.readJSON('.next/server/app/page.js.nft.json')
expect(trace.files).toContainEqual(
expect.stringMatching(/node_modules\/foo\/index\.js$/)
)
expect(trace.files).toContainEqual(
expect.stringMatching(/node_modules\/foo\/package\.json$/)
)
expect(trace.files).toContainEqual(
expect.stringMatching(/node_modules\/foo\/side-effect\.js$/)
)
expect(trace.files).toContainEqual(
expect.stringMatching(/node_modules\/foo\/value\.js$/)
)
})
})
...@@ -86,7 +86,11 @@ async fn setup( ...@@ -86,7 +86,11 @@ async fn setup(
ResolvedVc::upcast(module_asset_context), ResolvedVc::upcast(module_asset_context),
EcmascriptInputTransforms::empty().to_resolved().await?, EcmascriptInputTransforms::empty().to_resolved().await?,
EcmascriptOptions { EcmascriptOptions {
tree_shaking_mode: Some(TreeShakingMode::ReexportsOnly), tree_shaking_mode: if analyze_mode == AnalyzeMode::Tracing {
None
} else {
Some(TreeShakingMode::ReexportsOnly)
},
analyze_mode, analyze_mode,
..Default::default() ..Default::default()
} }
......
...@@ -168,8 +168,8 @@ pub enum SpecifiedModuleType { ...@@ -168,8 +168,8 @@ pub enum SpecifiedModuleType {
)] )]
#[serde(rename_all = "kebab-case")] #[serde(rename_all = "kebab-case")]
pub enum TreeShakingMode { pub enum TreeShakingMode {
#[default]
ModuleFragments, ModuleFragments,
#[default]
ReexportsOnly, ReexportsOnly,
} }
......
...@@ -79,8 +79,10 @@ async fn node_file_trace_operation( ...@@ -79,8 +79,10 @@ async fn node_file_trace_operation(
let module_asset_context = ModuleAssetContext::new( let module_asset_context = ModuleAssetContext::new(
Default::default(), Default::default(),
// This config should be kept in sync with // This config should be kept in sync with
// turbopack/crates/turbopack/tests/node-file-trace.rs and // turbopack/crates/turbopack-tracing/tests/node-file-trace.rs and
// turbopack/crates/turbopack/src/lib.rs // turbopack/crates/turbopack-tracing/tests/unit.rs and
// turbopack/crates/turbopack/src/lib.rs and
// turbopack/crates/turbopack-nft/src/nft.rs
CompileTimeInfo::new(environment), CompileTimeInfo::new(environment),
ModuleOptionsContext { ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
...@@ -97,6 +99,9 @@ async fn node_file_trace_operation( ...@@ -97,6 +99,9 @@ async fn node_file_trace_operation(
// node-file-trace. // node-file-trace.
environment: None, environment: None,
analyze_mode: AnalyzeMode::Tracing, analyze_mode: AnalyzeMode::Tracing,
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
tree_shaking_mode: None,
..Default::default() ..Default::default()
} }
.cell(), .cell(),
......
...@@ -361,8 +361,11 @@ async fn node_file_trace_operation( ...@@ -361,8 +361,11 @@ async fn node_file_trace_operation(
Default::default(), Default::default(),
// TODO These test cases should move into the `node-file-trace` crate and use the same // TODO These test cases should move into the `node-file-trace` crate and use the same
// config. // config.
// It's easy to make a mistake here as this should match the config in the binary from // This config should be kept in sync with
// turbopack/crates/turbopack/src/lib.rs // turbopack/crates/turbopack-tracing/tests/node-file-trace.rs and
// turbopack/crates/turbopack-tracing/tests/unit.rs and
// turbopack/crates/turbopack/src/lib.rs and
// turbopack/crates/turbopack-nft/src/nft.rs
CompileTimeInfo::new(environment), CompileTimeInfo::new(environment),
ModuleOptionsContext { ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
...@@ -380,6 +383,9 @@ async fn node_file_trace_operation( ...@@ -380,6 +383,9 @@ async fn node_file_trace_operation(
// node-file-trace. // node-file-trace.
environment: None, environment: None,
analyze_mode: AnalyzeMode::Tracing, analyze_mode: AnalyzeMode::Tracing,
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
tree_shaking_mode: None,
..Default::default() ..Default::default()
} }
.cell(), .cell(),
......
...@@ -198,8 +198,11 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result< ...@@ -198,8 +198,11 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result<
Default::default(), Default::default(),
// TODO These test cases should move into the `node-file-trace` crate and use the same // TODO These test cases should move into the `node-file-trace` crate and use the same
// config. // config.
// It's easy to make a mistake here as this should match the config in the binary from // This config should be kept in sync with
// turbopack/crates/turbopack/src/lib.rs // turbopack/crates/turbopack-tracing/tests/node-file-trace.rs and
// turbopack/crates/turbopack-tracing/tests/unit.rs and
// turbopack/crates/turbopack/src/lib.rs and
// turbopack/crates/turbopack-nft/src/nft.rs
CompileTimeInfo::new(environment), CompileTimeInfo::new(environment),
ModuleOptionsContext { ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
...@@ -216,6 +219,9 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result< ...@@ -216,6 +219,9 @@ async fn node_file_trace_operation(package_root: RcStr, input: RcStr) -> Result<
// node-file-trace. // node-file-trace.
environment: None, environment: None,
analyze_mode: AnalyzeMode::Tracing, analyze_mode: AnalyzeMode::Tracing,
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
tree_shaking_mode: None,
..Default::default() ..Default::default()
} }
.cell(), .cell(),
......
...@@ -166,8 +166,12 @@ async fn apply_module_type( ...@@ -166,8 +166,12 @@ async fn apply_module_type(
if runtime_code { if runtime_code {
ResolvedVc::upcast(module) ResolvedVc::upcast(module)
} else { } else {
if matches!(&part, Some(ModulePart::Evaluation)) { let options_value = options.await?;
// Skip the evaluation part if the module is marked as side effect free. if options_value.tree_shaking_mode.is_some()
&& matches!(&part, Some(ModulePart::Evaluation))
{
// If we are tree shaking, skip the evaluation part if the module is marked as
// side effect free.
let side_effect_free_packages = module_asset_context let side_effect_free_packages = module_asset_context
.side_effect_free_packages() .side_effect_free_packages()
.resolve() .resolve()
...@@ -181,7 +185,6 @@ async fn apply_module_type( ...@@ -181,7 +185,6 @@ async fn apply_module_type(
} }
} }
let options_value = options.await?;
match options_value.tree_shaking_mode { match options_value.tree_shaking_mode {
Some(TreeShakingMode::ModuleFragments) => { Some(TreeShakingMode::ModuleFragments) => {
Vc::upcast(EcmascriptModulePartAsset::select_part( Vc::upcast(EcmascriptModulePartAsset::select_part(
...@@ -740,9 +743,11 @@ pub async fn externals_tracing_module_context( ...@@ -740,9 +743,11 @@ pub async fn externals_tracing_module_context(
Ok(ModuleAssetContext::new_without_replace_externals( Ok(ModuleAssetContext::new_without_replace_externals(
Default::default(), Default::default(),
compile_time_info, compile_time_info,
// Keep these options more or less in sync with // This config should be kept in sync with
// turbopack/crates/turbopack/tests/node-file-trace.rs to ensure that the NFT unit tests // turbopack/crates/turbopack-tracing/tests/node-file-trace.rs and
// are actually representative of what Turbopack does. // turbopack/crates/turbopack-tracing/tests/unit.rs and
// turbopack/crates/turbopack/src/lib.rs and
// turbopack/crates/turbopack-nft/src/nft.rs
ModuleOptionsContext { ModuleOptionsContext {
ecmascript: EcmascriptOptionsContext { ecmascript: EcmascriptOptionsContext {
enable_typescript_transform: Some( enable_typescript_transform: Some(
...@@ -762,6 +767,9 @@ pub async fn externals_tracing_module_context( ...@@ -762,6 +767,9 @@ pub async fn externals_tracing_module_context(
// node-file-trace. // node-file-trace.
environment: None, environment: None,
analyze_mode: AnalyzeMode::Tracing, analyze_mode: AnalyzeMode::Tracing,
// Disable tree shaking. Even side-effect-free imports need to be traced, as they will
// execute at runtime.
tree_shaking_mode: None,
..Default::default() ..Default::default()
} }
.cell(), .cell(),
......
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