Skip to content

Commit 6f3d3d4

Browse files
authored
fix: npm protocol alias with pnp should be supported (#139)
1 parent 395df2a commit 6f3d3d4

File tree

5 files changed

+54
-4
lines changed

5 files changed

+54
-4
lines changed

fixtures/global-pnp/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "global-pnp",
3-
"packageManager": "[email protected].1",
3+
"packageManager": "[email protected].2",
44
"dependencies": {
55
"source-map-support": "^0.5.21"
66
}

fixtures/pnp/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
{
22
"name": "pnp",
3-
"packageManager": "[email protected].1",
3+
"packageManager": "[email protected].2",
44
"dependencies": {
55
"@atlaskit/pragmatic-drag-and-drop": "^1.5.2",
6+
"@custom/pragmatic-drag-and-drop": "npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2",
67
"beachball": "^2.52.0",
8+
"custom-minimist": "npm:minimist@^1.2.8",
79
"is-even": "^1.0.0",
810
"is-odd": "^3.0.1",
911
"lib": "link:./shared",
1012
"lodash.zip": "^4.2.0",
13+
"pragmatic-drag-and-drop": "npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2",
1114
"preact": "^10.26.5"
1215
}
1316
}

fixtures/pnp/yarn.lock

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ __metadata:
55
version: 8
66
cacheKey: 10c0
77

8-
"@atlaskit/pragmatic-drag-and-drop@npm:^1.5.2":
8+
"@atlaskit/pragmatic-drag-and-drop@npm:^1.5.2, @custom/pragmatic-drag-and-drop@npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2, pragmatic-drag-and-drop@npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2":
99
version: 1.5.2
1010
resolution: "@atlaskit/pragmatic-drag-and-drop@npm:1.5.2"
1111
dependencies:
@@ -188,6 +188,13 @@ __metadata:
188188
languageName: node
189189
linkType: hard
190190

191+
"custom-minimist@npm:minimist@^1.2.8":
192+
version: 1.2.8
193+
resolution: "minimist@npm:1.2.8"
194+
checksum: 10c0/19d3fcdca050087b84c2029841a093691a91259a47def2f18222f41e7645a0b7c44ef4b40e88a1e58a40c84d2ef0ee6047c55594d298146d0eb3f6b737c20ce6
195+
languageName: node
196+
linkType: hard
197+
191198
"dir-glob@npm:^3.0.1":
192199
version: 3.0.1
193200
resolution: "dir-glob@npm:3.0.1"
@@ -682,11 +689,14 @@ __metadata:
682689
resolution: "pnp@workspace:."
683690
dependencies:
684691
"@atlaskit/pragmatic-drag-and-drop": "npm:^1.5.2"
692+
"@custom/pragmatic-drag-and-drop": "npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2"
685693
beachball: "npm:^2.52.0"
694+
custom-minimist: "npm:minimist@^1.2.8"
686695
is-even: "npm:^1.0.0"
687696
is-odd: "npm:^3.0.1"
688697
lib: "link:./shared"
689698
lodash.zip: "npm:^4.2.0"
699+
pragmatic-drag-and-drop: "npm:@atlaskit/pragmatic-drag-and-drop@^1.5.2"
690700
preact: "npm:^10.26.5"
691701
languageName: unknown
692702
linkType: soft

src/lib.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
915915
let pkg_name = cached_path_string.rsplit_once("node_modules/").map_or(
916916
"",
917917
// remove trailing slash
918-
|last| last.1.strip_suffix("/").unwrap_or(last.1),
918+
|(_, last)| last.strip_suffix('/').unwrap_or(last),
919919
);
920920

921921
let inner_request = if pkg_name.is_empty() {
@@ -927,6 +927,15 @@ impl<C: Cache<Cp = FsCachedPath>> ResolverGeneric<C> {
927927
},
928928
)
929929
} else {
930+
let (first, rest) = specifier.split_once('/').unwrap_or((specifier, ""));
931+
// the original `pkg_name` in cached path could be different with specifier
932+
// due to alias like `"custom-minimist": "npm:minimist@^1.2.8"`
933+
// in this case, `specifier` is `pkg_name`'s source of truth
934+
let pkg_name = if first.starts_with('@') {
935+
&format!("{first}/{}", rest.split_once('/').unwrap_or((rest, "")).0)
936+
} else {
937+
first
938+
};
930939
let inner_specifier = specifier.strip_prefix(pkg_name).unwrap();
931940
String::from("./")
932941
+ inner_specifier.strip_prefix("/").unwrap_or(inner_specifier)

src/tests/pnp.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,34 @@ fn resolve_pnp_nested_package_json() {
132132
);
133133
}
134134

135+
#[test]
136+
fn resolve_npm_protocol_alias() {
137+
let fixture = super::fixture_root().join("pnp");
138+
139+
let resolver = Resolver::default();
140+
141+
assert_eq!(
142+
resolver.resolve(&fixture, "custom-minimist").map(|r| r.full_path()),
143+
Ok(fixture.join(
144+
".yarn/cache/minimist-npm-1.2.8-d7af7b1dce-19d3fcdca0.zip/node_modules/minimist/index.js"
145+
))
146+
);
147+
148+
assert_eq!(
149+
resolver.resolve(&fixture, "@custom/pragmatic-drag-and-drop").map(|r| r.full_path()),
150+
Ok(fixture.join(
151+
".yarn/cache/@atlaskit-pragmatic-drag-and-drop-npm-1.5.2-3241d4f843-1dace49fa3.zip/node_modules/@atlaskit/pragmatic-drag-and-drop/dist/cjs/index.js"
152+
))
153+
);
154+
155+
assert_eq!(
156+
resolver.resolve(&fixture, "pragmatic-drag-and-drop").map(|r| r.full_path()),
157+
Ok(fixture.join(
158+
".yarn/cache/@atlaskit-pragmatic-drag-and-drop-npm-1.5.2-3241d4f843-1dace49fa3.zip/node_modules/@atlaskit/pragmatic-drag-and-drop/dist/cjs/index.js"
159+
))
160+
);
161+
}
162+
135163
// Windows is blocked by upstream
136164
// see also https://github.com/yarnpkg/pnp-rs/pull/10
137165
#[cfg(not(windows))]

0 commit comments

Comments
 (0)