Skip to content

Commit 0af3970

Browse files
authored
[turbopack] Fix a bug where our global rewrite could introduce a TDZ issue (#82659)
Change our `global` rewrite to use a non-shadowable property on `__turbopack_context__` The previous rewrite to `globalThis` would break if there was a local declaration of `let globalThis` which could either trigger a TDZ error or simply evaluate to the wrong value. Closes PACK-5256 Fixes #82632
1 parent 50524de commit 0af3970

File tree

9 files changed

+68
-67
lines changed

9 files changed

+68
-67
lines changed

turbopack/crates/turbopack-ecmascript-runtime/js/src/shared/runtime-types.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,4 +137,5 @@ interface TurbopackBaseContext<M> {
137137
x: ExternalRequire
138138
y: ExternalImport
139139
z: CommonJsRequire
140+
g: typeof globalThis
140141
}

turbopack/crates/turbopack-ecmascript-runtime/js/src/shared/runtime-utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -686,6 +686,9 @@ function requireStub(_moduleId: ModuleId): never {
686686
}
687687
contextPrototype.z = requireStub
688688

689+
// Make `globalThis` available to the module in a way that cannot be shadowed by a local variable.
690+
contextPrototype.g = globalThis
691+
689692
type ContextConstructor<M> = {
690693
new (module: Module, exports: Exports): TurbopackBaseContext<M>
691694
}

turbopack/crates/turbopack-ecmascript/src/references/mod.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ use crate::{
159159
util::InlineSourceMap,
160160
},
161161
runtime_functions::{
162-
TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXPORTS,
162+
TURBOPACK_EXPORT_NAMESPACE, TURBOPACK_EXPORT_VALUE, TURBOPACK_EXPORTS, TURBOPACK_GLOBAL,
163163
TURBOPACK_REQUIRE_REAL, TURBOPACK_REQUIRE_STUB, TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS,
164164
},
165165
tree_shake::{find_turbopack_part_id_in_asserts, part_of_module, split},
@@ -1506,7 +1506,8 @@ async fn compile_time_info_for_module_type(
15061506
InputRelativeConstant::FileName,
15071507
));
15081508

1509-
// Compiletime rewrite the nodejs `global` to `globalThis`
1509+
// Compiletime rewrite the nodejs `global` to `__turbopack_context_.g` which is a shortcut for
1510+
// `globalThis` that cannot be shadowed by a local variable.
15101511
let global = rcstr!("global");
15111512
free_var_references
15121513
.entry(vec![
@@ -1516,7 +1517,7 @@ async fn compile_time_info_for_module_type(
15161517
.or_insert(rcstr!("object").into());
15171518
free_var_references
15181519
.entry(vec![DefinableNameSegment::Name(global)])
1519-
.or_insert(FreeVarReference::Ident(rcstr!("globalThis")));
1520+
.or_insert(TURBOPACK_GLOBAL.into());
15201521

15211522
free_var_references.extend(TURBOPACK_RUNTIME_FUNCTION_SHORTCUTS.into_iter().map(
15221523
|(name, shortcut)| {

turbopack/crates/turbopack-ecmascript/src/runtime_functions.rs

Lines changed: 39 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -53,60 +53,45 @@ impl<'l> From<&'l TurbopackRuntimeFunctionShortcut> for &'l str {
5353
}
5454
}
5555

56-
pub const TURBOPACK_EXPORTS: &TurbopackRuntimeFunctionShortcut =
57-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.e", "e");
58-
pub const TURBOPACK_MODULE: &TurbopackRuntimeFunctionShortcut =
59-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.m", "m");
60-
pub const TURBOPACK_REQUIRE: &TurbopackRuntimeFunctionShortcut =
61-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.r", "r");
62-
pub const TURBOPACK_ASYNC_LOADER: &TurbopackRuntimeFunctionShortcut =
63-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.A", "A");
64-
pub const TURBOPACK_MODULE_CONTEXT: &TurbopackRuntimeFunctionShortcut =
65-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.f", "f");
66-
pub const TURBOPACK_IMPORT: &TurbopackRuntimeFunctionShortcut =
67-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.i", "i");
68-
pub const TURBOPACK_ESM: &TurbopackRuntimeFunctionShortcut =
69-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.s", "s");
70-
pub const TURBOPACK_EXPORT_VALUE: &TurbopackRuntimeFunctionShortcut =
71-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.v", "v");
72-
pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut =
73-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.n", "n");
74-
pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut =
75-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.c", "c");
76-
pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut =
77-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.M", "M");
78-
pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut =
79-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.l", "l");
80-
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut =
81-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.L", "L");
82-
pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut =
83-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.C", "C");
84-
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut =
85-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.j", "j");
86-
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut =
87-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.P", "P");
88-
pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut =
89-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.U", "U");
90-
pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut =
91-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.R", "R");
92-
pub const TURBOPACK_WORKER_BLOB_URL: &TurbopackRuntimeFunctionShortcut =
93-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.b", "b");
94-
pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut =
95-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.a", "a");
96-
pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut =
97-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.x", "x");
98-
pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut =
99-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.y", "y");
100-
pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut =
101-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.k", "k");
102-
pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut =
103-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.z", "z");
104-
pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut =
105-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.t", "t");
106-
pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut =
107-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.w", "w");
108-
pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut =
109-
&TurbopackRuntimeFunctionShortcut::new("__turbopack_context__.u", "u");
56+
macro_rules! make_shortcut {
57+
($shortcut:expr) => {
58+
const {
59+
&TurbopackRuntimeFunctionShortcut::new(
60+
concat!("__turbopack_context__.", $shortcut),
61+
$shortcut,
62+
)
63+
}
64+
};
65+
}
66+
67+
pub const TURBOPACK_EXPORTS: &TurbopackRuntimeFunctionShortcut = make_shortcut!("e");
68+
pub const TURBOPACK_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("m");
69+
pub const TURBOPACK_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("r");
70+
pub const TURBOPACK_ASYNC_LOADER: &TurbopackRuntimeFunctionShortcut = make_shortcut!("A");
71+
pub const TURBOPACK_MODULE_CONTEXT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("f");
72+
pub const TURBOPACK_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("i");
73+
pub const TURBOPACK_ESM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("s");
74+
pub const TURBOPACK_EXPORT_VALUE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("v");
75+
pub const TURBOPACK_EXPORT_NAMESPACE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("n");
76+
pub const TURBOPACK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("c");
77+
pub const TURBOPACK_MODULES: &TurbopackRuntimeFunctionShortcut = make_shortcut!("M");
78+
pub const TURBOPACK_LOAD: &TurbopackRuntimeFunctionShortcut = make_shortcut!("l");
79+
pub const TURBOPACK_LOAD_BY_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("L");
80+
pub const TURBOPACK_CLEAR_CHUNK_CACHE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("C");
81+
pub const TURBOPACK_DYNAMIC: &TurbopackRuntimeFunctionShortcut = make_shortcut!("j");
82+
pub const TURBOPACK_RESOLVE_ABSOLUTE_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("P");
83+
pub const TURBOPACK_RELATIVE_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("U");
84+
pub const TURBOPACK_RESOLVE_MODULE_ID_PATH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("R");
85+
pub const TURBOPACK_WORKER_BLOB_URL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("b");
86+
pub const TURBOPACK_ASYNC_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("a");
87+
pub const TURBOPACK_EXTERNAL_REQUIRE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("x");
88+
pub const TURBOPACK_EXTERNAL_IMPORT: &TurbopackRuntimeFunctionShortcut = make_shortcut!("y");
89+
pub const TURBOPACK_REFRESH: &TurbopackRuntimeFunctionShortcut = make_shortcut!("k");
90+
pub const TURBOPACK_REQUIRE_STUB: &TurbopackRuntimeFunctionShortcut = make_shortcut!("z");
91+
pub const TURBOPACK_REQUIRE_REAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("t");
92+
pub const TURBOPACK_WASM: &TurbopackRuntimeFunctionShortcut = make_shortcut!("w");
93+
pub const TURBOPACK_WASM_MODULE: &TurbopackRuntimeFunctionShortcut = make_shortcut!("u");
94+
pub const TURBOPACK_GLOBAL: &TurbopackRuntimeFunctionShortcut = make_shortcut!("g");
11095

11196
/// Adding an entry to this list will automatically ensure that `__turbopack_XXX__` can be called
11297
/// from user code (by inserting a replacement into free_var_references)
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
console.log(global)
2+
let globalThis = 2
3+
4+
it('should rewrite global', () => {
5+
expect(typeof global).toBe('object')
6+
expect(typeof globalThis).toBe('number')
7+
})

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_build_runtime/output/[turbopack]_runtime.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/5c1d0_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_c0f7e0b0.js

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

turbopack/crates/turbopack-tests/tests/snapshot/runtime/default_dev_runtime/output/780ce_turbopack-tests_tests_snapshot_runtime_default_dev_runtime_input_index_c0f7e0b0.js.map

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)