Unverified Commit f367ab34 authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

Turbopack: fix require handling with alternative (#98258)

Regression from 
- https://github.com/vercel/next.js/pull/97867 (which uncovered this bug
in this particular case with the default parameter assignment)
and
- https://github.com/vercel/next.js/pull/97697 (because previously
`unknown | WellKnownFunction` was just unknown. Now it stays `unknown |
WellKnownFunction`)

Fixes
```
./node_modules/.pnpm/@vue+compiler-sfc@3.5.26/node_modules/@vue/compiler-sfc/dist/compiler-sfc.esm-browser.js:45756:20
Error: Module not found: Can't resolve 'less'
  45754 | );
  45755 | const less = (source, map, options, load = require) => {
> 45756 |   const nodeLess = load("less");
        |                    ^^^^^^^^^^^^
```

because basically
```js
const load = globalThis.foobar || require
const x = load("less");
```
would get replaced to
```js
const load = globalThis.foobar || require
const x = __turbopack_context__.require(191912); 
```
which would be wrong if `globalThis.foobar` isn't actually `require` at
runtime.

Instead, if we are inside an alternative, don't emit a codegen
replacement but only add reference
parent f8657de1
......@@ -267,6 +267,7 @@ impl CodeGens {
}
pub trait IntoCodeGenReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>>;
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -94,7 +94,7 @@ impl ModuleReference for CjsAssetReference {
}
}
#[turbo_tasks::value]
#[turbo_tasks::value(shared)]
#[derive(Hash, Debug, ValueToString)]
#[value_to_string("require {request}")]
pub struct CjsRequireAssetReference {
......@@ -174,6 +174,10 @@ impl ModuleReference for CjsRequireAssetReference {
}
impl IntoCodeGenReference for CjsRequireAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......@@ -316,6 +320,10 @@ impl ModuleReference for CjsRequireResolveAssetReference {
}
impl IntoCodeGenReference for CjsRequireResolveAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -142,6 +142,10 @@ impl EmittedModuleReference for EmitReference {
}
impl IntoCodeGenReference for EmitReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
mut path: AstPath,
......@@ -215,6 +219,10 @@ impl ModuleReference for CollectReference {
}
impl IntoCodeGenReference for CollectReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
mut path: AstPath,
......
......@@ -118,6 +118,10 @@ impl ModuleReference for EsmAsyncAssetReference {
}
impl IntoCodeGenReference for EsmAsyncAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -50,6 +50,10 @@ impl ModuleReference for EsmModuleIdAssetReference {
}
impl IntoCodeGenReference for EsmModuleIdAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -112,6 +112,10 @@ impl ModuleReference for UrlAssetReference {
}
impl IntoCodeGenReference for UrlAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -996,6 +996,10 @@ impl ModuleReference for ImportMetaGlobAssetReference {
}
impl IntoCodeGenReference for ImportMetaGlobAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -313,6 +313,10 @@ impl ModuleReference for RequireContextAssetReference {
}
impl IntoCodeGenReference for RequireContextAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -220,6 +220,10 @@ impl ValueToString for ServiceWorkerAssetReference {
}
impl IntoCodeGenReference for ServiceWorkerAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
......@@ -308,6 +308,10 @@ impl ValueToString for WorkerAssetReference {
}
impl IntoCodeGenReference for WorkerAssetReference {
fn into_reference(self) -> ResolvedVc<Box<dyn ModuleReference>> {
ResolvedVc::upcast(self.resolved_cell())
}
fn into_code_gen_reference(
self,
path: AstPath,
......
globalThis.foobar = (v) => `this is ${v}`
const load = globalThis.foobar || require
const nodeLess = load('missing')
it("doesn't replace alternative with require", () => {
expect(nodeLess).toBe('this is missing')
})
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