Skip to content

Commit 9345ea0

Browse files
authored
fix(cli): use pm-specific commands for vp info/view (#1895)
## Summary Use package-manager-native commands for `vp info` / `vp view` and update affected snap tests. ## Why Avoid routing `pnpm`/`yarn` metadata lookup through `npm view`, which can cause resolver/compatibility failures. ## Changes - `crates/vite_install/src/commands/view.rs` - `npm` → `npm view` - `pnpm` → `pnpm view` - `bun` → `bun info` - `yarn 1.x` → `yarn info` - `yarn berry` → `yarn npm info` closes #1893
1 parent 4c793b7 commit 9345ea0

6 files changed

Lines changed: 205 additions & 80 deletions

File tree

crates/vite_install/src/commands/view.rs

Lines changed: 94 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -31,39 +31,81 @@ impl PackageManager {
3131
}
3232

3333
/// Resolve the view command.
34-
/// All package managers delegate to npm view (pnpm and yarn use npm internally).
35-
/// Bun uses `bun info` as a native alternative.
34+
/// npm/pnpm/bun use their native `view`/`info` subcommand,
35+
/// yarn uses `yarn info` (Classic) and `yarn npm info` (Berry).
3636
#[must_use]
3737
pub fn resolve_view_command(&self, options: &ViewCommandOptions) -> ResolveCommandResult {
3838
let envs = HashMap::from([("PATH".to_string(), format_path_env(self.get_bin_prefix()))]);
3939
let mut args: Vec<String> = Vec::new();
4040

41-
let bin_name: String = if self.client == PackageManagerType::Bun {
42-
args.push("info".into());
43-
args.push(options.package.to_string());
41+
let bin_name: String = match self.client {
42+
PackageManagerType::Bun => {
43+
args.push("info".into());
44+
args.push(options.package.to_string());
4445

45-
if let Some(field) = options.field {
46-
args.push(field.to_string());
47-
}
46+
if let Some(field) = options.field {
47+
args.push(field.to_string());
48+
}
49+
50+
if options.json {
51+
args.push("--json".into());
52+
}
4853

49-
if options.json {
50-
args.push("--json".into());
54+
"bun".into()
5155
}
56+
PackageManagerType::Yarn => {
57+
if self.is_yarn_berry() {
58+
args.push("npm".into());
59+
args.push("info".into());
60+
args.push(options.package.to_string());
61+
62+
if let Some(field) = options.field {
63+
args.push("--fields".into());
64+
args.push(field.to_string());
65+
}
66+
} else {
67+
args.push("info".into());
68+
args.push(options.package.to_string());
69+
70+
if let Some(field) = options.field {
71+
args.push(field.to_string());
72+
}
73+
}
74+
75+
if options.json {
76+
args.push("--json".into());
77+
}
78+
79+
"yarn".into()
80+
}
81+
PackageManagerType::Npm => {
82+
args.push("view".into());
83+
args.push(options.package.to_string());
5284

53-
"bun".into()
54-
} else {
55-
args.push("view".into());
56-
args.push(options.package.to_string());
85+
if let Some(field) = options.field {
86+
args.push(field.to_string());
87+
}
5788

58-
if let Some(field) = options.field {
59-
args.push(field.to_string());
60-
}
89+
if options.json {
90+
args.push("--json".into());
91+
}
6192

62-
if options.json {
63-
args.push("--json".into());
93+
"npm".into()
6494
}
95+
PackageManagerType::Pnpm => {
96+
args.push("view".into());
97+
args.push(options.package.to_string());
6598

66-
"npm".into()
99+
if let Some(field) = options.field {
100+
args.push(field.to_string());
101+
}
102+
103+
if options.json {
104+
args.push("--json".into());
105+
}
106+
107+
"pnpm".into()
108+
}
67109
};
68110

69111
// Add pass-through args
@@ -106,15 +148,15 @@ mod tests {
106148
}
107149

108150
#[test]
109-
fn test_pnpm_view_uses_npm() {
151+
fn test_pnpm_view_uses_pnpm() {
110152
let pm = create_mock_package_manager(PackageManagerType::Pnpm, "10.0.0");
111153
let result = pm.resolve_view_command(&ViewCommandOptions {
112154
package: "react",
113155
field: None,
114156
json: false,
115157
pass_through_args: None,
116158
});
117-
assert_eq!(result.bin_path, "npm");
159+
assert_eq!(result.bin_path, "pnpm");
118160
assert_eq!(result.args, vec!["view", "react"]);
119161
}
120162

@@ -132,16 +174,42 @@ mod tests {
132174
}
133175

134176
#[test]
135-
fn test_yarn_view_uses_npm() {
177+
fn test_yarn_view_uses_info() {
136178
let pm = create_mock_package_manager(PackageManagerType::Yarn, "1.22.0");
137179
let result = pm.resolve_view_command(&ViewCommandOptions {
138180
package: "lodash",
139181
field: None,
140182
json: true,
141183
pass_through_args: None,
142184
});
143-
assert_eq!(result.bin_path, "npm");
144-
assert_eq!(result.args, vec!["view", "lodash", "--json"]);
185+
assert_eq!(result.bin_path, "yarn");
186+
assert_eq!(result.args, vec!["info", "lodash", "--json"]);
187+
}
188+
189+
#[test]
190+
fn test_yarn_berry_view_uses_yarn_npm_info() {
191+
let pm = create_mock_package_manager(PackageManagerType::Yarn, "4.0.0");
192+
let result = pm.resolve_view_command(&ViewCommandOptions {
193+
package: "lodash",
194+
field: None,
195+
json: true,
196+
pass_through_args: None,
197+
});
198+
assert_eq!(result.bin_path, "yarn");
199+
assert_eq!(result.args, vec!["npm", "info", "lodash", "--json"]);
200+
}
201+
202+
#[test]
203+
fn test_yarn_berry_view_uses_fields_option_for_view_field() {
204+
let pm = create_mock_package_manager(PackageManagerType::Yarn, "4.0.0");
205+
let result = pm.resolve_view_command(&ViewCommandOptions {
206+
package: "lodash",
207+
field: Some("dist.tarball"),
208+
json: false,
209+
pass_through_args: None,
210+
});
211+
assert_eq!(result.bin_path, "yarn");
212+
assert_eq!(result.args, vec!["npm", "info", "lodash", "--fields", "dist.tarball"]);
145213
}
146214

147215
#[test]
@@ -153,7 +221,7 @@ mod tests {
153221
json: false,
154222
pass_through_args: None,
155223
});
156-
assert_eq!(result.bin_path, "npm");
224+
assert_eq!(result.bin_path, "pnpm");
157225
assert_eq!(result.args, vec!["view", "react", "dist.tarball"]);
158226
}
159227
}

packages/cli/snap-tests-global/command-view-pnpm11/snap.txt

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ Documentation: https://viteplus.dev/guide/install
1616

1717

1818
> vp pm view testnpm2 # should view lodash package information (uses npm view)
19-
2019
testnpm2@<semver> | ISC | deps: none | versions: 2
2120

2221
dist
@@ -25,19 +24,16 @@ dist
2524
.integrity: sha512-<hash>
2625

2726
maintainers:
28-
- fengmk2 <fengmk2@gmail.com>
27+
- fengmk2
2928

3029
dist-tags:
3130
latest: <semver>
3231
release-1: <semver>
3332

34-
published over a year ago by fengmk2 <fengmk2@gmail.com>
35-
3633
> vp pm view testnpm2 version # should view lodash version field (uses npm view)
3734
1.0.1
3835

3936
> vp pm view testnpm2@1.0.0 # should view specific version of lodash (uses npm view)
40-
4137
testnpm2@<semver> | ISC | deps: none | versions: 2
4238

4339
dist
@@ -46,14 +42,12 @@ dist
4642
.integrity: sha512-<hash>
4743

4844
maintainers:
49-
- fengmk2 <fengmk2@gmail.com>
45+
- fengmk2
5046

5147
dist-tags:
5248
latest: <semver>
5349
release-1: <semver>
5450

55-
published over a year ago by fengmk2 <fengmk2@gmail.com>
56-
5751
> vp pm view testnpm2 dist.tarball # should view nested field (uses npm view)
5852
https://registry.<domain>/testnpm2/-/testnpm2-1.0.1.tgz
5953

@@ -65,7 +59,6 @@ https://registry.<domain>/testnpm2/-/testnpm2-1.0.1.tgz
6559
"1.0.1"
6660

6761
> vp pm view testnpm2 -- --loglevel=warn # should support pass through arguments (uses npm view)
68-
6962
testnpm2@<semver> | ISC | deps: none | versions: 2
7063

7164
dist
@@ -74,10 +67,8 @@ dist
7467
.integrity: sha512-<hash>
7568

7669
maintainers:
77-
- fengmk2 <fengmk2@gmail.com>
70+
- fengmk2
7871

7972
dist-tags:
8073
latest: <semver>
8174
release-1: <semver>
82-
83-
published over a year ago by fengmk2 <fengmk2@gmail.com>
Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,59 @@
1-
> vp pm view testnpm2 # should view testnpm2 package information (uses npm view)
1+
> vp pm view testnpm2 # should view testnpm2 package information (uses yarn info)
2+
yarn info v<semver>
3+
{
4+
name: 'testnpm2',
5+
time: {
6+
modified: '<date>T<date>.474Z',
7+
created: '<date>T<date>.187Z',
8+
'0.0.1': '<date>T<date>.187Z',
9+
'0.0.2': '<date>T<date>.290Z',
10+
'0.0.3': '<date>T<date>.614Z',
11+
'0.0.4': '<date>T<date>.134Z',
12+
'0.0.5': '<date>T<date>.517Z',
13+
'0.0.6': '<date>T<date>.300Z',
14+
'0.0.7': '<date>T<date>.493Z',
15+
'0.0.8': '<date>T<date>.636Z',
16+
'1.0.0': '<date>T<date>.382Z',
17+
'1.0.1': '<date>T<date>.560Z',
18+
'2.0.0': '<date>T<date>.747Z',
19+
'2.0.1': '<date>T<date>.572Z'
20+
},
21+
maintainers: [
22+
{
23+
name: 'fengmk2',
24+
email: 'fengmk2@gmail.com'
25+
}
26+
],
27+
'dist-tags': {
28+
latest: '1.0.1',
29+
'release-1': '1.0.1'
30+
},
31+
versions: [
32+
'1.0.0',
33+
'1.0.1'
34+
],
35+
license: 'ISC',
36+
version: '1.0.1',
37+
main: 'index.js',
38+
scripts: {
39+
test: 'echo "Error: no test specified" && exit 1'
40+
},
41+
dist: {
42+
shasum: <hash>
43+
tarball: 'https://registry.<domain>/testnpm2/-/testnpm2-1.0.1.tgz',
44+
integrity: 'sha512-F4AQ+KmzhbOSlt7ae+X2O8IJktFZAcN6OK169TT4ny7M3e4Vje7NITZTOU31AtEk9L/Z8lrCrqinl/eY6WPuEw==',
45+
signatures: [
46+
{
47+
keyid: 'SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA',
48+
sig: 'MEQCICqyUi6OO0qltJG0Z2fI021Q87C6zFIWH9h2lb9PsyRKAiAHU26fIlW7Om8JPh2BEx72YAAVP2yXS2bvf9vzc/yjaw=='
49+
}
50+
]
51+
},
52+
directories: {}
53+
}
54+
Done in <variable>ms.
255

3-
testnpm2@<semver> | ISC | deps: none | versions: 2
4-
5-
dist
6-
.tarball: https://registry.<domain>/testnpm2/-/testnpm2-1.0.1.tgz
7-
.shasum: <hash>
8-
.integrity: sha512-<hash>
9-
10-
maintainers:
11-
- fengmk2 <fengmk2@gmail.com>
12-
13-
dist-tags:
14-
latest: <semver>
15-
release-1: <semver>
16-
17-
published over a year ago by fengmk2 <fengmk2@gmail.com>
18-
19-
> vp pm view testnpm2 version # should view testnpm2 version field (uses npm view)
20-
1.0.1
56+
> vp pm view testnpm2 version # should view testnpm2 version field (uses yarn info)
57+
yarn info v<semver>
58+
<semver>
59+
Done in <variable>ms.
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"commands": [
3-
"vp pm view testnpm2 # should view testnpm2 package information (uses npm view)",
4-
"vp pm view testnpm2 version # should view testnpm2 version field (uses npm view)"
3+
"vp pm view testnpm2 # should view testnpm2 package information (uses yarn info)",
4+
"vp pm view testnpm2 version # should view testnpm2 version field (uses yarn info)"
55
]
66
}
Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,47 @@
1-
> vp pm view testnpm2 # should view testnpm2 package information (uses npm view)
1+
> vp pm view testnpm2 # should view testnpm2 package information (uses yarn npm info)
2+
{
3+
name: 'testnpm2',
4+
time: {
5+
modified: '<date>T<date>.474Z',
6+
created: '<date>T<date>.187Z',
7+
'0.0.1': '<date>T<date>.187Z',
8+
'0.0.2': '<date>T<date>.290Z',
9+
'0.0.3': '<date>T<date>.614Z',
10+
'0.0.4': '<date>T<date>.134Z',
11+
'0.0.5': '<date>T<date>.517Z',
12+
'0.0.6': '<date>T<date>.300Z',
13+
'0.0.7': '<date>T<date>.493Z',
14+
'0.0.8': '<date>T<date>.636Z',
15+
'1.0.0': '<date>T<date>.382Z',
16+
'1.0.1': '<date>T<date>.560Z',
17+
'2.0.0': '<date>T<date>.747Z',
18+
'2.0.1': '<date>T<date>.572Z'
19+
},
20+
maintainers: [
21+
{
22+
name: 'fengmk2',
23+
email: 'fengmk2@gmail.com'
24+
}
25+
],
26+
'dist-tags': {
27+
latest: '1.0.1',
28+
'release-1': '1.0.1'
29+
},
30+
versions: [
31+
'1.0.0',
32+
'1.0.1'
33+
],
34+
license: 'ISC',
35+
version: '1.0.1',
36+
main: 'index.js',
37+
scripts: {
38+
test: 'echo "Error: no test specified" && exit 1'
39+
},
40+
directories: {}
41+
}
242

3-
testnpm2@<semver> | ISC | deps: none | versions: 2
4-
5-
dist
6-
.tarball: https://registry.<domain>/testnpm2/-/testnpm2-1.0.1.tgz
7-
.shasum: <hash>
8-
.integrity: sha512-<hash>
9-
10-
maintainers:
11-
- fengmk2 <fengmk2@gmail.com>
12-
13-
dist-tags:
14-
latest: <semver>
15-
release-1: <semver>
16-
17-
published over a year ago by fengmk2 <fengmk2@gmail.com>
18-
19-
> vp pm view testnpm2 version # should view testnpm2 version field (uses npm view)
20-
1.0.1
43+
> vp pm view testnpm2 version # should view testnpm2 version field (uses yarn npm info)
44+
{
45+
name: 'testnpm2',
46+
version: '1.0.1'
47+
}

0 commit comments

Comments
 (0)