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

turbopack: track locally eager module loads

Co-authored-by: 's avatarLuke Sandberg <210140+lukesandberg@users.noreply.github.com>
parent e5188a20
...@@ -82,6 +82,8 @@ async fn compute_side_effect_free_module_info_single( ...@@ -82,6 +82,8 @@ async fn compute_side_effect_free_module_info_single(
// parent is a module that depends on it. // parent is a module that depends on it.
|child, parent, _s| { |child, parent, _s| {
Ok(if let Some((_, ref_data)) = child { Ok(if let Some((_, ref_data)) = child {
// if the parent imports this module in a 'deferred' mechanism our side effects
// don't flow to it.
if ref_data.binding_usage.evaluation_timing == ModuleEvaluationTiming::Deferred { if ref_data.binding_usage.evaluation_timing == ModuleEvaluationTiming::Deferred {
return Ok(GraphTraversalAction::Exclude); return Ok(GraphTraversalAction::Exclude);
} }
......
...@@ -149,15 +149,20 @@ impl BindingUsage { ...@@ -149,15 +149,20 @@ impl BindingUsage {
} }
} }
/// Defines whether following a module reference can execute its target during evaluation of the /// Defines whether following a module reference is reachable from the referencing module's own
/// referencing module. /// evaluation path.
///
/// A cyclic importer can invoke an exported function before the exporting module has completed,
/// but that invocation belongs to the importer's evaluation path. The eager static cycle already
/// represents that dependency for module-evaluation ordering.
#[turbo_tasks::value(shared)] #[turbo_tasks::value(shared)]
#[derive(Debug, Clone, Copy, Default, Hash, Serialize, Deserialize)] #[derive(Debug, Clone, Copy, Default, Hash, Serialize, Deserialize)]
pub enum ModuleEvaluationTiming { pub enum ModuleEvaluationTiming {
/// The target may execute during evaluation of the referencing module. /// The reference may be followed from the referencing module's own evaluation path.
#[default] #[default]
Evaluation, Evaluation,
/// The target can only execute after evaluation of the referencing module has completed. /// The reference is only reachable through a function-like body that the referencing module
/// does not invoke while evaluating.
Deferred, Deferred,
} }
......
...@@ -14,7 +14,10 @@ pub use crate::analyzer::graph::{ ...@@ -14,7 +14,10 @@ pub use crate::analyzer::graph::{
}; };
use crate::{ use crate::{
AnalyzeMode, SpecifiedModuleType, AnalyzeMode, SpecifiedModuleType,
analyzer::{Bump, JsValue, graph::visitor::Analyzer}, analyzer::{
Bump, JsValue,
graph::visitor::{Analyzer, evaluation_reachable_functions},
},
ast_path_trie::AstPathTrieBuilder, ast_path_trie::AstPathTrieBuilder,
chunk::CjsStaticExports, chunk::CjsStaticExports,
code_gen::CodeGen, code_gen::CodeGen,
...@@ -90,6 +93,7 @@ pub fn create_graph<'a>( ...@@ -90,6 +93,7 @@ pub fn create_graph<'a>(
code_gens: Default::default(), code_gens: Default::default(),
ast_paths: Default::default(), ast_paths: Default::default(),
supports_block_scoping, supports_block_scoping,
evaluation_reachable_functions: evaluation_reachable_functions(m),
}; };
// CommonJS export recognition runs for a CommonJS module that emits code when either CJS // CommonJS export recognition runs for a CommonJS module that emits code when either CJS
......
...@@ -758,7 +758,7 @@ ...@@ -758,7 +758,7 @@
span: 427..436, span: 427..436,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
...@@ -845,7 +845,7 @@ ...@@ -845,7 +845,7 @@
span: 489..505, span: 489..505,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
...@@ -948,7 +948,7 @@ ...@@ -948,7 +948,7 @@
span: 642..651, span: 642..651,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
...@@ -1015,7 +1015,7 @@ ...@@ -1015,7 +1015,7 @@
span: 654..670, span: 654..670,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
......
...@@ -453,7 +453,7 @@ ...@@ -453,7 +453,7 @@
span: 443..476, span: 443..476,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -698,7 +698,7 @@ ...@@ -698,7 +698,7 @@
span: 515..551, span: 515..551,
in_try: true, in_try: true,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
......
...@@ -352,7 +352,7 @@ ...@@ -352,7 +352,7 @@
span: 2725..2751, span: 2725..2751,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: Variable( obj: Variable(
...@@ -632,7 +632,7 @@ ...@@ -632,7 +632,7 @@
span: 2775..2800, span: 2775..2800,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Conditional { Conditional {
condition: Logical( condition: Logical(
...@@ -978,7 +978,7 @@ ...@@ -978,7 +978,7 @@
span: 2828..3484, span: 2828..3484,
in_try: false, in_try: false,
new: true, new: true,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
...@@ -1382,7 +1382,7 @@ ...@@ -1382,7 +1382,7 @@
span: 3526..3571, span: 3526..3571,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
...@@ -1519,7 +1519,7 @@ ...@@ -1519,7 +1519,7 @@
span: 3589..3606, span: 3589..3606,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
...@@ -2314,7 +2314,7 @@ ...@@ -2314,7 +2314,7 @@
span: 1025..1034, span: 1025..1034,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: Variable( obj: Variable(
...@@ -2464,7 +2464,7 @@ ...@@ -2464,7 +2464,7 @@
span: 1038..1053, span: 1038..1053,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
In { In {
left: Variable( left: Variable(
...@@ -2931,7 +2931,7 @@ ...@@ -2931,7 +2931,7 @@
span: 1325..1374, span: 1325..1374,
in_try: false, in_try: false,
new: true, new: true,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
...@@ -3308,7 +3308,7 @@ ...@@ -3308,7 +3308,7 @@
span: 1530..1563, span: 1530..1563,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -3553,7 +3553,7 @@ ...@@ -3553,7 +3553,7 @@
span: 1602..1638, span: 1602..1638,
in_try: true, in_try: true,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
FreeVar { FreeVar {
var: "downloadedBinPath", var: "downloadedBinPath",
...@@ -3705,7 +3705,7 @@ ...@@ -3705,7 +3705,7 @@
span: 1670..1701, span: 1670..1701,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -3941,7 +3941,7 @@ ...@@ -3941,7 +3941,7 @@
span: 1712..1734, span: 1712..1734,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Conditional { Conditional {
condition: Not( condition: Not(
...@@ -4262,7 +4262,7 @@ ...@@ -4262,7 +4262,7 @@
span: 1758..1778, span: 1758..1778,
in_try: true, in_try: true,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
FreeVar { FreeVar {
var: "Error", var: "Error",
...@@ -4466,7 +4466,7 @@ ...@@ -4466,7 +4466,7 @@
span: 1810..2124, span: 1810..2124,
in_try: false, in_try: false,
new: true, new: true,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
...@@ -4650,7 +4650,7 @@ ...@@ -4650,7 +4650,7 @@
span: 2196..2213, span: 2196..2213,
in_try: true, in_try: true,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Conditional { Conditional {
condition: Variable( condition: Variable(
...@@ -5020,7 +5020,7 @@ ...@@ -5020,7 +5020,7 @@
span: 2312..2338, span: 2312..2338,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
MemberCall { MemberCall {
obj: Variable( obj: Variable(
...@@ -5123,7 +5123,7 @@ ...@@ -5123,7 +5123,7 @@
span: 2299..2339, span: 2299..2339,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: Variable( obj: Variable(
...@@ -5416,7 +5416,7 @@ ...@@ -5416,7 +5416,7 @@
span: 2422..2444, span: 2422..2444,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
MemberCall { MemberCall {
obj: Variable( obj: Variable(
...@@ -5554,7 +5554,7 @@ ...@@ -5554,7 +5554,7 @@
span: 2367..2452, span: 2367..2452,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -5790,7 +5790,7 @@ ...@@ -5790,7 +5790,7 @@
span: 2463..2491, span: 2463..2491,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Conditional { Conditional {
condition: Not( condition: Not(
...@@ -6086,7 +6086,7 @@ ...@@ -6086,7 +6086,7 @@
span: 2501..2540, span: 2501..2540,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -6355,7 +6355,7 @@ ...@@ -6355,7 +6355,7 @@
span: 2548..2580, span: 2548..2580,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
], ],
range: Exact( range: Exact(
......
...@@ -417,7 +417,7 @@ ...@@ -417,7 +417,7 @@
span: 159..167, span: 159..167,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
...@@ -647,7 +647,7 @@ ...@@ -647,7 +647,7 @@
span: 271..281, span: 271..281,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
......
...@@ -229,7 +229,7 @@ ...@@ -229,7 +229,7 @@
span: 50..63, span: 50..63,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Member { Member {
obj: FreeVar( obj: FreeVar(
...@@ -623,7 +623,7 @@ ...@@ -623,7 +623,7 @@
span: 273..398, span: 273..398,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
......
...@@ -542,7 +542,7 @@ ...@@ -542,7 +542,7 @@
span: 34..42, span: 34..42,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
MemberCall { MemberCall {
obj: MemberCall( obj: MemberCall(
...@@ -618,7 +618,7 @@ ...@@ -618,7 +618,7 @@
span: 34..49, span: 34..49,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Unreachable { Unreachable {
start_ast_path: [ start_ast_path: [
......
...@@ -2527,7 +2527,7 @@ ...@@ -2527,7 +2527,7 @@
span: 1231..1286, span: 1231..1286,
in_try: false, in_try: false,
new: false, new: false,
evaluation_timing: Deferred, evaluation_timing: Evaluation,
}, },
Call { Call {
func: Variable( func: Variable(
......
import { entryValue } from './index.js'
export const childValue = 'child'
export function readEntry() {
return entryValue
}
const loadChild = () => require('./child.js')
export const childValue = loadChild().childValue
export const entryValue = 'entry'
it('keeps an evaluation-time cycle reached through a local function', () => {
expect(childValue).toBe('child')
expect(loadChild().readEntry()).toBe('entry')
const factory = __turbopack_modules__.get(
[...__turbopack_modules__.keys()].find((moduleId) =>
moduleId.endsWith(
'scope-hoisting/circuit-breaker-local-call-edge/input/index.js [test] (ecmascript)'
)
)
)
const source = factory.toString()
expect(source).toContain('()=>entryValue')
expect(source.indexOf('()=>entryValue')).toBeLessThan(
source.indexOf("const entryValue = 'entry'")
)
})
import './cyclic-y.js'
import { value } from './pure-dep.js'
export function load() {
return require('./cyclic-effect.cjs')
}
export const derived = value + 1
...@@ -2,6 +2,8 @@ import * as dynamicUnused from './dynamic-unused.js' ...@@ -2,6 +2,8 @@ import * as dynamicUnused from './dynamic-unused.js'
import * as requireUnused from './require-unused.js' import * as requireUnused from './require-unused.js'
import * as deferredContexts from './deferred-contexts.js' import * as deferredContexts from './deferred-contexts.js'
import * as evaluationContexts from './evaluation-contexts.js' import * as evaluationContexts from './evaluation-contexts.js'
import * as localCalled from './local-called.js'
import * as cyclicX from './cyclic-x.js'
import { loadDynamic } from './dynamic-used.js' import { loadDynamic } from './dynamic-used.js'
import { loadRequire } from './require-used.js' import { loadRequire } from './require-used.js'
...@@ -9,11 +11,15 @@ const unusedDynamic = dynamicUnused ...@@ -9,11 +11,15 @@ const unusedDynamic = dynamicUnused
const unusedRequire = requireUnused const unusedRequire = requireUnused
const unusedDeferredContexts = deferredContexts const unusedDeferredContexts = deferredContexts
const unusedEvaluationContexts = evaluationContexts const unusedEvaluationContexts = evaluationContexts
const unusedLocalCalled = localCalled
const unusedCyclicX = cyclicX
it('separates evaluation-time and deferred module edges', async () => { it('separates evaluation-time and deferred module edges', async () => {
expect(globalThis.__lateDynamicEffect).toBeUndefined() expect(globalThis.__lateDynamicEffect).toBeUndefined()
expect(globalThis.__lateRequireEffect).toBeUndefined() expect(globalThis.__lateRequireEffect).toBeUndefined()
expect(globalThis.__evaluationContextEffect).toBe(true) expect(globalThis.__evaluationContextEffect).toBe(true)
expect(globalThis.__localCalledEffect).toBe(true)
expect(globalThis.__cyclicLoaderEffect).toBe(true)
const factory = __turbopack_modules__.get( const factory = __turbopack_modules__.get(
[...__turbopack_modules__.keys()].find((moduleId) => [...__turbopack_modules__.keys()].find((moduleId) =>
...@@ -31,6 +37,8 @@ it('separates evaluation-time and deferred module edges', async () => { ...@@ -31,6 +37,8 @@ it('separates evaluation-time and deferred module edges', async () => {
expect(source).not.toContain(`${inputPath}/require-unused.js`) expect(source).not.toContain(`${inputPath}/require-unused.js`)
expect(source).not.toContain(`${inputPath}/deferred-contexts.js`) expect(source).not.toContain(`${inputPath}/deferred-contexts.js`)
expect(source).toContain(`${inputPath}/evaluation-contexts.js`) expect(source).toContain(`${inputPath}/evaluation-contexts.js`)
expect(source).toContain(`${inputPath}/local-called.js`)
expect(source).toContain(`${inputPath}/cyclic-x.js`)
const dynamicModule = await loadDynamic() const dynamicModule = await loadDynamic()
expect(dynamicModule.value).toBe('dynamic') expect(dynamicModule.value).toBe('dynamic')
......
import { value } from './pure-dep.js'
const load = () => require('./local-called-effect.cjs')
load()
export const derived = value + 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