Skip to content
Open
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
1 change: 1 addition & 0 deletions fixtures/pnp/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "pnp",
"packageManager": "yarn@4.3.1",
"dependencies": {
"@user/m1": "link:./shared-name-mismatch",
"extend": "3.0.2",
"is-even": "^1.0.0",
"is-odd": "^3.0.1",
Expand Down
1 change: 1 addition & 0 deletions fixtures/pnp/shared-name-mismatch/dist/a.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
module.exports = "feature";
8 changes: 8 additions & 0 deletions fixtures/pnp/shared-name-mismatch/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "shared-package",
"version": "1.0.0",
"exports": {
".": "./dist/a.js",
"./x": "./dist/a.js"
}
}
7 changes: 7 additions & 0 deletions fixtures/pnp/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ __metadata:
version: 8
cacheKey: 10c0

"@user/m1@link:./shared-name-mismatch::locator=pnp%40workspace%3A.":
version: 0.0.0-use.local
resolution: "@user/m1@link:./shared-name-mismatch::locator=pnp%40workspace%3A."
languageName: node
linkType: soft

"extend@npm:3.0.2":
version: 3.0.2
resolution: "extend@npm:3.0.2"
Expand Down Expand Up @@ -88,6 +94,7 @@ __metadata:
version: 0.0.0-use.local
resolution: "pnp@workspace:."
dependencies:
"@user/m1": "link:./shared-name-mismatch"
extend: "npm:3.0.2"
is-even: "npm:^1.0.0"
is-odd: "npm:^3.0.1"
Expand Down
29 changes: 17 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,21 +1022,26 @@ impl<Fs: FileSystem + Send + Sync> ResolverGeneric<Fs> {
match resolution {
Ok(pnp::Resolution::Resolved(path, subpath)) => {
let cached_path = self.cache.value(&path);

let export_resolution = self.load_package_self(&cached_path, specifier, ctx).await?;
// can be found in pnp cached folder
let normalized_subpath = subpath
.as_deref()
.map(|subpath| subpath.trim_start_matches('/'))
.filter(|subpath| !subpath.is_empty());

// Mirror enhanced-resolve's PnP pipeline by resolving exports from the
// request-derived subpath instead of PACKAGE_SELF name matching.
let exports_subpath =
normalized_subpath.map_or_else(String::new, |subpath| format!("/{subpath}"));
let export_resolution = self
.load_package_exports(specifier, &exports_subpath, &cached_path, ctx)
.await?;
if export_resolution.is_some() {
return Ok(export_resolution);
}
let is_bare = subpath.is_none();

let inner_request = subpath.map_or_else(
|| ".".to_string(),
|mut p| {
p.insert_str(0, "./");
p
},
);

let is_bare = normalized_subpath.is_none();

let inner_request = normalized_subpath
.map_or_else(|| ".".to_string(), |subpath| format!("./{subpath}"));

let mut inner_options = self.options().clone();
if is_bare {
Expand Down
40 changes: 40 additions & 0 deletions src/tests/pnp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -303,3 +303,43 @@ async fn resolve_pnp_transitive_dep_from_global_cache() {
"Expected isarray in global cache, got: {isarray}"
);
}

#[tokio::test]
async fn resolve_pnp_should_handle_exports_field_when_using_pnp() {
// Mimics enhanced-resolve: "should handle the exports field when using PnP"
let fixture = super::fixture_root().join("pnp");

let resolver = Resolver::new(ResolveOptions {
extensions: vec![".js".into()],
condition_names: vec!["import".into()],
..ResolveOptions::default()
});

assert_eq!(
resolver
.resolve(&fixture, "@user/m1")
.await
.map(|r| r.full_path()),
Ok(fixture.join("shared-name-mismatch/dist/a.js"))
);
}

#[tokio::test]
async fn resolve_pnp_should_handle_exports_field_when_using_pnp_with_sub_path() {
// Mimics enhanced-resolve: "should handle the exports field when using PnP (with sub path)"
let fixture = super::fixture_root().join("pnp");

let resolver = Resolver::new(ResolveOptions {
extensions: vec![".js".into()],
condition_names: vec!["import".into()],
..ResolveOptions::default()
});

assert_eq!(
resolver
.resolve(&fixture, "@user/m1/x")
.await
.map(|r| r.full_path()),
Ok(fixture.join("shared-name-mismatch/dist/a.js"))
);
}
Loading