Unverified Commit 5ac2327e authored by Niklas Mischkulnig's avatar Niklas Mischkulnig Committed by GitHub

[16.3] Turbopack: retain conditions when replacing resolve request keys (#97453)

Backports #97372
parent 0ccb3e7f
...@@ -627,6 +627,7 @@ async function readNormalizedNFT(next, name) { ...@@ -627,6 +627,7 @@ async function readNormalizedNFT(next, name) {
"./.next/server/server-reference-manifest.js", "./.next/server/server-reference-manifest.js",
"./.next/server/server-reference-manifest.json", "./.next/server/server-reference-manifest.json",
"/node_modules/@swc/helpers/cjs/_interop_require_default.cjs", "/node_modules/@swc/helpers/cjs/_interop_require_default.cjs",
"/node_modules/@swc/helpers/esm/_interop_require_default.js",
"/node_modules/next/dist/build/adapter/setup-node-env.external.js", "/node_modules/next/dist/build/adapter/setup-node-env.external.js",
"/node_modules/next/dist/client/components/hooks-server-context.js", "/node_modules/next/dist/client/components/hooks-server-context.js",
"/node_modules/next/dist/client/components/static-generation-bailout.js", "/node_modules/next/dist/client/components/static-generation-bailout.js",
......
...@@ -974,7 +974,7 @@ impl ResolveResult { ...@@ -974,7 +974,7 @@ impl ResolveResult {
fn with_replaced_request_key( fn with_replaced_request_key(
&self, &self,
old_request_key: RcStr, old_request_key: RcStr,
request_key: RequestKey, new_request_key: RcStr,
) -> Result<Vc<Self>> { ) -> Result<Vc<Self>> {
let new_primary = self let new_primary = self
.primary .primary
...@@ -983,11 +983,8 @@ impl ResolveResult { ...@@ -983,11 +983,8 @@ impl ResolveResult {
let remaining = k.request.as_ref()?.strip_prefix(&*old_request_key)?; let remaining = k.request.as_ref()?.strip_prefix(&*old_request_key)?;
Some(( Some((
RequestKey { RequestKey {
request: request_key request: Some(format!("{new_request_key}{remaining}").into()),
.request conditions: k.conditions.clone(),
.as_ref()
.map(|r| format!("{r}{remaining}").into()),
conditions: request_key.conditions.clone(),
}, },
v.clone(), v.clone(),
)) ))
...@@ -2665,12 +2662,11 @@ async fn apply_in_package( ...@@ -2665,12 +2662,11 @@ async fn apply_in_package(
}; };
let refs = refs.clone(); let refs = refs.clone();
let request_key = RequestKey::new(request.clone());
if value.as_bool() == Some(false) { if value.as_bool() == Some(false) {
return Ok(Some(ResolveResultOrCell::Value( return Ok(Some(ResolveResultOrCell::Value(
ResolveResult::primary_with_affecting_sources( ResolveResult::primary_with_affecting_sources(
request_key, RequestKey::new(request.clone()),
ResolveResultItem::Ignore, ResolveResultItem::Ignore,
refs, refs,
), ),
...@@ -2689,7 +2685,7 @@ async fn apply_in_package( ...@@ -2689,7 +2685,7 @@ async fn apply_in_package(
.with_fragment(fragment.clone()), .with_fragment(fragment.clone()),
options, options,
) )
.with_replaced_request_key(value.into(), request_key); .with_replaced_request_key(value.into(), request.clone());
if options_value.collect_affecting_sources && !refs.is_empty() { if options_value.collect_affecting_sources && !refs.is_empty() {
result = result.with_affecting_sources(refs.into_iter().map(|src| *src).collect()); result = result.with_affecting_sources(refs.into_iter().map(|src| *src).collect());
} }
...@@ -2831,7 +2827,7 @@ async fn resolve_module_request( ...@@ -2831,7 +2827,7 @@ async fn resolve_module_request(
fragment.clone(), fragment.clone(),
options, options,
) )
.with_replaced_request_key(rcstr!("."), RequestKey::new(name.clone())), .with_replaced_request_key(rcstr!("."), name.clone()),
); );
} }
FindPackageItem::PackageFile { name, file } => { FindPackageItem::PackageFile { name, file } => {
...@@ -2848,7 +2844,7 @@ async fn resolve_module_request( ...@@ -2848,7 +2844,7 @@ async fn resolve_module_request(
) )
.await? .await?
.into_cell() .into_cell()
.with_replaced_request_key(rcstr!("."), RequestKey::new(name.clone())); .with_replaced_request_key(rcstr!("."), name.clone());
results.push(resolved_result) results.push(resolved_result)
} }
} }
......
...@@ -4,3 +4,6 @@ integration/**/dist ...@@ -4,3 +4,6 @@ integration/**/dist
!integration/**/node_modules !integration/**/node_modules
!integration/**/.pnpm !integration/**/.pnpm
# Some unit cases need a checked-in node_modules to model a package layout
!test/unit/**/node_modules
{
"name": "@scope/helpers",
"version": "1.0.0",
"type": "module",
"exports": {
"./_/helper": {
"module-sync": "./esm/helper.js",
"webpack": "./esm/helper.js",
"import": "./esm/helper.js",
"default": "./cjs/helper.cjs"
}
}
}
;[
// The `@scope/helpers` package is reachable through two `node_modules` directories (as in a pnpm
// install, where the virtual store hoists every package into `node_modules/.pnpm/node_modules`).
// Resolving the same request in more than one directory must not drop the alternatives a single
// directory resolved to: both the `module-sync` target (picked by `require()` on Node >= 22.12)
// and the `default` target (picked by older Node versions) have to be traced.
'package.json',
'test/unit/module-sync-condition-cjs-nested-symlink/input.js',
'test/unit/module-sync-condition-cjs-nested-symlink/node_modules/.store/helpers@1.0.0/node_modules/@scope/helpers/cjs/helper.cjs',
'test/unit/module-sync-condition-cjs-nested-symlink/node_modules/.store/helpers@1.0.0/node_modules/@scope/helpers/esm/helper.js',
'test/unit/module-sync-condition-cjs-nested-symlink/node_modules/.store/helpers@1.0.0/node_modules/@scope/helpers/package.json',
'test/unit/module-sync-condition-cjs-nested-symlink/node_modules/.store/pkg@1.0.0/node_modules/pkg/index.js',
'test/unit/module-sync-condition-cjs-nested-symlink/node_modules/.store/pkg@1.0.0/node_modules/pkg/package.json',
]
const { test } = require('test-pkg-sync-cjs-subpath/_/helper')
console.log(test)
;[
// A CommonJS `require()` of a subpath export whose conditions are ordered `module-sync`,
// `webpack`, `import`, `default` - the shape published packages use to hand an ESM file to
// `require()` on Node >= 22.12 and a CommonJS file to older Node versions. Both targets have to be
// traced, because which one the runtime picks depends on its Node version.
'test/unit/module-sync-condition-cjs-subpath/cjs/helper.cjs',
'test/unit/module-sync-condition-cjs-subpath/esm/helper.mjs',
'test/unit/module-sync-condition-cjs-subpath/input.js',
'test/unit/module-sync-condition-cjs-subpath/package.json',
]
{
"name": "test-pkg-sync-cjs-subpath",
"type": "commonjs",
"exports": {
"./_/helper": {
"module-sync": "./esm/helper.mjs",
"webpack": "./esm/helper.mjs",
"import": "./esm/helper.mjs",
"default": "./cjs/helper.cjs"
}
}
}
...@@ -126,6 +126,12 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc; ...@@ -126,6 +126,12 @@ static ALLOC: turbo_tasks_malloc::TurboMalloc = turbo_tasks_malloc::TurboMalloc;
#[case::module_sync_condition_cjs("module-sync-condition-cjs")] #[case::module_sync_condition_cjs("module-sync-condition-cjs")]
// Turbopack always includes the module-sync version, regardless of the current Node version // Turbopack always includes the module-sync version, regardless of the current Node version
// #[case::module_sync_condition_cjs_node20("module-sync-condition-cjs-node20")] // #[case::module_sync_condition_cjs_node20("module-sync-condition-cjs-node20")]
// A `require()` of a subpath export that hands the `module-sync` condition an ESM file and
// `default` a CommonJS one (not a case that any of the above cover): both have to be traced.
#[case::module_sync_condition_cjs_subpath("module-sync-condition-cjs-subpath")]
// The same, but with the package reachable through two `node_modules` directories, as in a pnpm
// install: merging the results of both must not drop either target.
#[case::module_sync_condition_cjs_nested_symlink("module-sync-condition-cjs-nested-symlink")]
#[case::module_sync_condition_es("module-sync-condition-es")] #[case::module_sync_condition_es("module-sync-condition-es")]
#[case::module_sync_condition_es_nested("module-sync-condition-es-nested")] #[case::module_sync_condition_es_nested("module-sync-condition-es-nested")]
// Turbopack always includes the module-sync version, regardless of the current Node version // Turbopack always includes the module-sync version, regardless of the current Node version
......
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