antfu-collective / ni

💡 Use the right package manager
MIT License
6.25k stars 208 forks source link

Global installation of detected `yarn@>1` and `pnpm@<7` is broken #189

Closed auvred closed 9 months ago

auvred commented 9 months ago

Describe the bug

If the packageManager field in package.json is set to yarn@3.x.x, yarn@4.x.x or pnpm@6.x.x, etc and the requested package manager is not installed, the name of detected package is wrongly concatenated with its version: yarn@berry@4.0.0, pnpm@6@6.0.0

$ echo '{"packageManager": "yarn@4.0.0"}' > package.json && ni
[ni] Detected yarn@berry but it doesn't seem to be installed.

✔ Would you like to globally install yarn@berry (​https://yarnpkg.com/getting-started/install​)? … yes
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "berry@4.0.0" of package "yarn@berry@4.0.0": Tags may not have any characters that encodeURIComponent encodes.

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-11-27T14_24_37_837Z-debug-0.log
$ echo '{"packageManager": "pnpm@6.0.0"}' > package.json && ni
[ni] Detected pnpm@6 but it doesn't seem to be installed.

✔ Would you like to globally install pnpm@6 (​https://pnpm.io/6.x/installation​)? … yes
npm ERR! code EINVALIDTAGNAME
npm ERR! Invalid tag name "6@6.0.0" of package "pnpm@6@6.0.0": Tags may not have any characters that encodeURIComponent encodes.

npm ERR! A complete log of this run can be found in: /root/.npm/_logs/2023-11-27T14_20_04_290Z-debug-0.log

Reproduction

Make sure that neither pnpm nor yarn are installed globally. And then run echo '{"packageManager": "yarn@4.0.0"}' > package.json && ni or echo '{"packageManager": "pnpm@6.0.0"}' > package.json && ni

System Info

System:
    OS: Linux 6.2 Alpine Linux
    CPU: (16) x64 AMD Ryzen 7 5800H with Radeon Graphics
    Memory: 26.29 GB / 31.19 GB
    Container: Yes
    Shell: 1.35.0 - /bin/ash
  Binaries:
    Node: 18.18.2 - /usr/local/bin/node
    npm: 9.8.1 - /usr/local/bin/npm

Used Package Manager

npm

Validations

antfu commented 9 months ago

Nice spot. PR welcome :)