Turbopack: properly remove unused TS enums with scope hoisting#88205
Turbopack: properly remove unused TS enums with scope hoisting#88205
Conversation
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
1 similar comment
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
| let factory = __turbopack_modules__.get( | ||
| [...__turbopack_modules__.keys()].find((m) => | ||
| m.endsWith( | ||
| 'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)' | ||
| ) | ||
| ) | ||
| ) |
There was a problem hiding this comment.
| let factory = __turbopack_modules__.get( | |
| [...__turbopack_modules__.keys()].find((m) => | |
| m.endsWith( | |
| 'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)' | |
| ) | |
| ) | |
| ) | |
| const moduleKey = [...__turbopack_modules__.keys()].find((m) => | |
| m.endsWith( | |
| 'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)' | |
| ) | |
| ) | |
| if (!moduleKey) { | |
| throw new Error( | |
| 'Could not find module with key ending in "scope-hoisting/pure-comments/input/index.js [test] (ecmascript)". Available modules: ' + | |
| Array.from(__turbopack_modules__.keys()).join(', ') | |
| ) | |
| } | |
| let factory = __turbopack_modules__.get(moduleKey) |
Test code accesses turbopack_modules.get() on undefined result from .find(), causing potential runtime error
View Details
📝 Patch Details
diff --git a/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/input/index.js b/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/input/index.js
index 2b60f2c51b..66579eaf3d 100644
--- a/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/input/index.js
+++ b/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/input/index.js
@@ -1,13 +1,20 @@
import './a.ts'
it('should retain PURE comments with scope hoisting', () => {
- let factory = __turbopack_modules__.get(
- [...__turbopack_modules__.keys()].find((m) =>
- m.endsWith(
- 'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)'
- )
+ const moduleKey = [...__turbopack_modules__.keys()].find((m) =>
+ m.endsWith(
+ 'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)'
)
)
+
+ if (!moduleKey) {
+ throw new Error(
+ 'Could not find module with key ending in "scope-hoisting/pure-comments/input/index.js [test] (ecmascript)". Available modules: ' +
+ Array.from(__turbopack_modules__.keys()).join(', ')
+ )
+ }
+
+ let factory = __turbopack_modules__.get(moduleKey)
expect(factory.toString()).not.toContain('THIS_SHOULD_BE_REMOVED')
})
Analysis
The original test code had a critical error handling issue:
-
The Problem: The code called
[...__turbopack_modules__.keys()].find((m) => m.endsWith(...))and immediately passed the result to.get()without checking if.find()returnedundefined. -
Why It Fails: If no module key matches the
endsWithcondition (which could happen if the module key format doesn't match expectations),.find()returnsundefined. Then.get(undefined)is called, which would returnundefined, and calling.toString()onundefinedwould throw a TypeError: "Cannot read property 'toString' of undefined". -
Root Cause: The test had two potential failure points:
- The hardcoded string
'scope-hoisting/pure-comments/input/index.js [test] (ecmascript)'could fail to match the actual module key format - Even if the string matched, there was no defensive check for when
.find()returnsundefined
- The hardcoded string
-
The Fix: The fix extracts the result of
.find()into amoduleKeyvariable, adds an explicit null check, and provides a detailed error message that lists all available modules if the expected module is not found. This makes the test fail gracefully with useful debugging information instead of a confusing TypeError.
This is important because it:
- Prevents TypeError at runtime when module lookup fails
- Provides clear error messages for debugging when the expected module is not found
- Makes the test more robust to changes in module key format
- Follows defensive programming principles for external module access
| ) | ||
| ) | ||
|
|
||
| expect(factory.toString()).not.toContain('THIS_SHOULD_BE_REMOVED') |
There was a problem hiding this comment.
Test may pass for wrong reason due to unused exports being tree-shaken before scope hoisting processes them
View Details
📝 Patch Details
diff --git a/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/options.json b/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/options.json
index 3dc1021aa8..4e71b0cc33 100644
--- a/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/options.json
+++ b/turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/options.json
@@ -1,3 +1,4 @@
{
- "minify": true
+ "minify": true,
+ "remove_unused_exports": false
}
Analysis
The test "should retain PURE comments with scope hoisting" in /turbopack/crates/turbopack-tests/tests/execution/turbopack/scope-hoisting/pure-comments/input/index.js has a logic flaw:
The Issue:
- The test imports
./a.tswhich exports an unused enumUnusedwith a memberTHIS_SHOULD_BE_REMOVED - The import statement has no destructuring/usage of the enum
- The test checks that the string 'THIS_SHOULD_BE_REMOVED' doesn't appear in the compiled factory
- The default test options have
remove_unused_exports: true(from TestOptions::default()) - This means unused exports are removed before scope hoisting processes the module
Why it's a problem:
- The test is meant to verify that PURE comments are retained during scope hoisting
- If the enum is completely tree-shaken away before scope hoisting, the string won't appear in the output
- But this would be for the WRONG reason - not because PURE comments are being preserved, but because the code was removed entirely
- The test would pass even if PURE comments were being incorrectly stripped during scope hoisting
The Fix:
Modified options.json to set "remove_unused_exports": false, which ensures:
- The enum in
a.tsis kept in the bundle despite being unused - Scope hoisting will actually process the module
- The test correctly verifies that PURE comments (if any) are preserved during scope hoisting
- The test now fails if PURE comments are incorrectly stripped
This fix ensures the test actually validates the intended behavior instead of passing for an accidental reason.
Stats from current PR✅ No significant changes detected📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles: **430 kB** → **430 kB**
|
| Canary | PR | Change | |
|---|---|---|---|
| middleware-b..fest.js gzip | 787 B | 791 B | ✓ |
| Total | 787 B | 791 B |
Build Details
Build Manifests
| Canary | PR | Change | |
|---|---|---|---|
| _buildManifest.js gzip | 447 B | 452 B | 🔴 +5 B (+1%) |
| Total | 447 B | 452 B |
📦 Webpack
Client
Main Bundles
| Canary | PR | Change | |
|---|---|---|---|
| 2086.HASH.js gzip | 169 B | N/A | - |
| 2161-HASH.js gzip | 5.4 kB | N/A | - |
| 2747-HASH.js gzip | 4.48 kB | N/A | - |
| 4322-HASH.js gzip | 52.5 kB | N/A | - |
| ec793fe8-HASH.js gzip | 62.3 kB | N/A | - |
| framework-HASH.js gzip | 59.8 kB | 59.8 kB | ✓ |
| main-app-HASH.js gzip | 251 B | 254 B | 🔴 +3 B (+1%) |
| main-HASH.js gzip | 38.4 kB | 38.8 kB | ✓ |
| webpack-HASH.js gzip | 1.68 kB | 1.69 kB | ✓ |
| 1596.HASH.js gzip | N/A | 169 B | - |
| 2658-HASH.js gzip | N/A | 52.2 kB | - |
| 6349-HASH.js gzip | N/A | 4.46 kB | - |
| 7019-HASH.js gzip | N/A | 5.42 kB | - |
| b17a3386-HASH.js gzip | N/A | 62.3 kB | - |
| Total | 225 kB | 225 kB |
Polyfills
| Canary | PR | Change | |
|---|---|---|---|
| polyfills-HASH.js gzip | 39.4 kB | 39.4 kB | ✓ |
| Total | 39.4 kB | 39.4 kB | ✓ |
Pages
| Canary | PR | Change | |
|---|---|---|---|
| _app-HASH.js gzip | 194 B | 193 B | ✓ |
| _error-HASH.js gzip | 182 B | 182 B | ✓ |
| css-HASH.js gzip | 336 B | 335 B | ✓ |
| dynamic-HASH.js gzip | 1.8 kB | 1.8 kB | ✓ |
| edge-ssr-HASH.js gzip | 256 B | 256 B | ✓ |
| head-HASH.js gzip | 352 B | 349 B | ✓ |
| hooks-HASH.js gzip | 385 B | 384 B | ✓ |
| image-HASH.js gzip | 580 B | 580 B | ✓ |
| index-HASH.js gzip | 259 B | 258 B | ✓ |
| link-HASH.js gzip | 2.5 kB | 2.51 kB | ✓ |
| routerDirect..HASH.js gzip | 319 B | 317 B | ✓ |
| script-HASH.js gzip | 385 B | 387 B | ✓ |
| withRouter-HASH.js gzip | 316 B | 315 B | ✓ |
| 1afbb74e6ecf..834.css gzip | 106 B | 106 B | ✓ |
| Total | 7.97 kB | 7.96 kB | ✅ -8 B |
Server
Edge SSR
| Canary | PR | Change | |
|---|---|---|---|
| edge-ssr.js gzip | 124 kB | 124 kB | ✓ |
| page.js gzip | 240 kB | 240 kB | ✓ |
| Total | 364 kB | 365 kB |
Middleware
| Canary | PR | Change | |
|---|---|---|---|
| middleware-b..fest.js gzip | 653 B | 656 B | ✓ |
| middleware-r..fest.js gzip | 155 B | 156 B | ✓ |
| middleware.js gzip | 32.8 kB | 32.9 kB | ✓ |
| edge-runtime..pack.js gzip | 846 B | 846 B | ✓ |
| Total | 34.4 kB | 34.6 kB |
Build Details
Build Manifests
| Canary | PR | Change | |
|---|---|---|---|
| _buildManifest.js gzip | 738 B | 738 B | ✓ |
| Total | 738 B | 738 B | ✓ |
Build Cache
| Canary | PR | Change | |
|---|---|---|---|
| 0.pack gzip | 3.62 MB | 3.63 MB | 🔴 +7.21 kB (+0%) |
| index.pack gzip | 99.6 kB | 98.5 kB | 🟢 1.12 kB (-1%) |
| index.pack.old gzip | 97.9 kB | 100 kB | 🔴 +2.42 kB (+2%) |
| Total | 3.82 MB | 3.82 MB |
🔄 Shared (bundler-independent)
Runtimes
| Canary | PR | Change | |
|---|---|---|---|
| app-page-exp...dev.js gzip | 303 kB | 303 kB | ✓ |
| app-page-exp..prod.js gzip | 158 kB | 158 kB | ✓ |
| app-page-tur...dev.js gzip | 302 kB | 302 kB | ✓ |
| app-page-tur..prod.js gzip | 158 kB | 158 kB | ✓ |
| app-page-tur...dev.js gzip | 299 kB | 299 kB | ✓ |
| app-page-tur..prod.js gzip | 156 kB | 156 kB | ✓ |
| app-page.run...dev.js gzip | 299 kB | 299 kB | ✓ |
| app-page.run..prod.js gzip | 156 kB | 156 kB | ✓ |
| app-route-ex...dev.js gzip | 68.7 kB | 68.7 kB | ✓ |
| app-route-ex..prod.js gzip | 47.5 kB | 47.5 kB | ✓ |
| app-route-tu...dev.js gzip | 68.7 kB | 68.7 kB | ✓ |
| app-route-tu..prod.js gzip | 47.5 kB | 47.5 kB | ✓ |
| app-route-tu...dev.js gzip | 68.3 kB | 68.3 kB | ✓ |
| app-route-tu..prod.js gzip | 47.3 kB | 47.3 kB | ✓ |
| app-route.ru...dev.js gzip | 68.3 kB | 68.3 kB | ✓ |
| app-route.ru..prod.js gzip | 47.3 kB | 47.3 kB | ✓ |
| dist_client_...dev.js gzip | 324 B | 324 B | ✓ |
| dist_client_...dev.js gzip | 326 B | 326 B | ✓ |
| dist_client_...dev.js gzip | 318 B | 318 B | ✓ |
| dist_client_...dev.js gzip | 317 B | 317 B | ✓ |
| pages-api-tu...dev.js gzip | 41.1 kB | 41.1 kB | ✓ |
| pages-api-tu..prod.js gzip | 31.2 kB | 31.2 kB | ✓ |
| pages-api.ru...dev.js gzip | 41 kB | 41 kB | ✓ |
| pages-api.ru..prod.js gzip | 31.2 kB | 31.2 kB | ✓ |
| pages-turbo....dev.js gzip | 50.8 kB | 50.8 kB | ✓ |
| pages-turbo...prod.js gzip | 38.2 kB | 38.2 kB | ✓ |
| pages.runtim...dev.js gzip | 50.7 kB | 50.7 kB | ✓ |
| pages.runtim..prod.js gzip | 38.2 kB | 38.2 kB | ✓ |
| server.runti..prod.js gzip | 62.2 kB | 62.2 kB | ✓ |
| Total | 2.68 MB | 2.68 MB | ✓ |

Only adds a test so far
Closes #88009
Closes PACK-6424