Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions turbopack/crates/turbopack-core/src/resolve/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ use crate::{
parse::{Request, stringify_data_uri},
pattern::{Pattern, PatternMatch, read_matches},
plugin::{AfterResolvePlugin, AfterResolvePluginCondition, BeforeResolvePlugin},
remap::{ExportsField, ImportsField, ReplacedSubpathValueResult},
remap::{ExportImport, ExportsField, ImportsField, ReplacedSubpathValueResult},
},
source::{OptionSource, Source, Sources},
};
Expand Down Expand Up @@ -2950,6 +2950,7 @@ async fn resolve_into_package(
conditions,
unspecified_conditions,
query,
ExportImport::Export,
)
.await?,
);
Expand Down Expand Up @@ -3229,6 +3230,7 @@ async fn handle_exports_imports_field(
conditions: &BTreeMap<RcStr, ConditionValue>,
unspecified_conditions: &ConditionValue,
query: RcStr,
kind: ExportImport,
) -> Result<Vc<ResolveResult>> {
let mut results = Vec::new();
let mut conditions_state = FxHashMap::default();
Expand Down Expand Up @@ -3263,12 +3265,13 @@ async fn handle_exports_imports_field(
} in results
{
if let Some(result_path) = result_path.with_normalized_path() {
let request = *Request::parse(Pattern::Concatenation(vec![
Pattern::Constant(rcstr!("./")),
result_path.clone(),
]))
.to_resolved()
.await?;
let request =
Pattern::Concatenation(vec![Pattern::Constant(rcstr!("./")), result_path.clone()]);
let request = match kind {
ExportImport::Export => request,
ExportImport::Import => Pattern::alternatives(vec![request, result_path.clone()]),
};
let request = *Request::parse(request).to_resolved().await?;
Comment on lines +3268 to +3274
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Using Pattern::alternatives to try both a relative path (prefixed with ./) and the original path for subpath imports can lead to ambiguous resolution. According to the Node.js specification, subpath import targets are either relative (starting with ./ or ../) or bare package specifiers. Trying both might cause a bare specifier like "#dep": "my-pkg" to accidentally resolve to a local file ./my-pkg if it exists, which deviates from standard Node.js behavior.

If the intention is to strictly follow the spec, result_path.clone() should be sufficient for ExportImport::Import, as Request::parse will correctly handle the relativeness based on the prefix. If backward compatibility for missing ./ prefixes is required, the current approach is a safe fallback, but the code can be simplified to avoid redundant shadowing and improve readability.

            let request = match kind {
                ExportImport::Export => {
                    Pattern::Concatenation(vec![Pattern::Constant(rcstr!("./")), result_path.clone()])
                }
                ExportImport::Import => Pattern::alternatives(vec![
                    Pattern::Concatenation(vec![
                        Pattern::Constant(rcstr!("./")),
                        result_path.clone(),
                    ]),
                    result_path.clone(),
                ]),
            };
            let request = *Request::parse(request).to_resolved().await?;


let resolve_result = Box::pin(resolve_internal_inline(
package_path.clone(),
Expand Down Expand Up @@ -3370,6 +3373,7 @@ async fn resolve_package_internal_with_imports_field(
conditions,
unspecified_conditions,
RcStr::default(),
ExportImport::Import,
)
.await
}
Expand Down
2 changes: 1 addition & 1 deletion turbopack/crates/turbopack-core/src/resolve/remap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use crate::resolve::{

/// A small helper type to differentiate parsing exports and imports fields.
#[derive(Copy, Clone)]
enum ExportImport {
pub(crate) enum ExportImport {
Export,
Import,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import foo from '#foo'
import dep from '#dep'
import depInNm from '#dep-in-nm'
import pattern from '#pattern/pat.js'
import conditionalImport from '#conditional'
const conditionalRequire = require('#conditional')

console.log(foo, dep, pattern, conditionalImport, conditionalRequire)
console.log(foo, dep, depInNm, pattern, conditionalImport, conditionalRequire)

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"imports": {
"#foo": "./foo.js",
"#dep": "dep",
"#dep-in-nm": "dep-in-nm",
"#conditional": {
"import": "./import.mjs",
"require": "./require.cjs"
},
"#pattern/*.js": "./*.js"
},
"dependencies": {
"dep": "./dep"
"dep": "./dep",
"dep-in-nm": "dep-in-nm"
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
(globalThis["TURBOPACK"] || (globalThis["TURBOPACK"] = [])).push([
"output/0rv8_turbopack-tests_tests_snapshot_imports_subpath-imports_input_index_1jr1_4n.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_imports_subpath-imports_input_14v1xaq._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/subpath-imports/input/index.js [test] (ecmascript)"]}
"output/0rv8_turbopack-tests_tests_snapshot_imports_subpath-imports_input_index_01i6qyk.js",
{"otherChunks":["output/1do3_crates_turbopack-tests_tests_snapshot_imports_subpath-imports_input_1p6gt_f._.js"],"runtimeModuleIds":["[project]/turbopack/crates/turbopack-tests/tests/snapshot/imports/subpath-imports/input/index.js [test] (ecmascript)"]}
]);
// Dummy runtime

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading