diff --git a/README.md b/README.md index a83114e48..18bcf88e9 100644 --- a/README.md +++ b/README.md @@ -177,7 +177,7 @@ This command doesn't change the global version used when running the package manager from outside the project (use the \`-g,--global\` flag if you wish to do this). -### `corepack install <-g,--global> [--all] [... name@version]` +### `corepack install <-g,--global> [--all] [... name[@]]` | Option | Description | | --------------------- | ------------------------------------------ | @@ -189,7 +189,7 @@ Package managers thus installed will be configured as the new default when calling their respective binaries outside of projects defining the `packageManager` field. -### `corepack pack [--all] [... name@version]` +### `corepack pack [--all] [... name[@]]` | Option | Description | | --------------------- | ------------------------------------------ | @@ -200,7 +200,7 @@ calling their respective binaries outside of projects defining the Download the selected package managers and store them inside a tarball suitable for use with `corepack install -g`. -### `corepack use ` +### `corepack use ]>` When run, this command will retrieve the latest release matching the provided descriptor, assign it to the project's package.json file, and automatically @@ -215,7 +215,7 @@ it. Unlike `corepack use` this command doesn't take a package manager name nor a version range, as it will always select the latest available version from the same major line. Should you need to upgrade to a new major, use an explicit -`corepack use {name}@latest` call. +`corepack use {name}@latest` call (or simply `corepack use {name}`). ## Environment Variables diff --git a/sources/specUtils.ts b/sources/specUtils.ts index f1bc1f434..e52f200ba 100644 --- a/sources/specUtils.ts +++ b/sources/specUtils.ts @@ -11,16 +11,16 @@ export function parseSpec(raw: unknown, source: string, {enforceExactVersion = t if (typeof raw !== `string`) throw new UsageError(`Invalid package manager specification in ${source}; expected a string`); - const match = raw.match(/^(?!_)(.+)@(.+)$/); - if (match === null || (enforceExactVersion && !semver.valid(match[2]))) - throw new UsageError(`Invalid package manager specification in ${source}; expected a semver version${enforceExactVersion ? `` : `, range, or tag`}`); + const match = raw.match(/^(?!_)([^@]+)(?:@(.+))?$/); + if (match === null || (enforceExactVersion && (!match[2] || !semver.valid(match[2])))) + throw new UsageError(`Invalid package manager specification in ${source} (${raw}); expected a semver version${enforceExactVersion ? `` : `, range, or tag`}`); if (!isSupportedPackageManager(match[1])) throw new UsageError(`Unsupported package manager specification (${match})`); return { name: match[1], - range: match[2], + range: match[2] ?? `*`, }; } @@ -57,7 +57,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra case `NoProject`: case `NoSpec`: { return fallbackLocator; - } break; + } case `Found`: { if (result.spec.name !== locator.name) { @@ -69,7 +69,7 @@ export async function findProjectSpec(initialCwd: string, locator: Locator, {tra } else { return result.spec; } - } break; + } } } } diff --git a/tests/main.test.ts b/tests/main.test.ts index 3538ff3dd..83bacb94c 100644 --- a/tests/main.test.ts +++ b/tests/main.test.ts @@ -267,6 +267,25 @@ it(`should allow to call "corepack install -g" with a tag`, async () => { }); }); +it(`should allow to call "corepack install -g" without any range`, async () => { + await xfs.mktempPromise(async cwd => { + await expect(runCli(cwd, [`install`, `-g`, `yarn`])).resolves.toMatchObject({ + exitCode: 0, + stderr: ``, + }); + + await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), { + // empty package.json file + }); + + await expect(runCli(cwd, [`yarn`, `--version`])).resolves.toMatchObject({ + stdout: expect.not.stringMatching(/^[123]\./), + stderr: ``, + exitCode: 0, + }); + }); +}); + it(`should allow to call "corepack install" without arguments within a configured project`, async () => { await xfs.mktempPromise(async cwd => { await xfs.writeJsonPromise(ppath.join(cwd, `package.json` as Filename), { diff --git a/tests/nock/8LXMft4IyEWeaqoiynS5FA-1.dat b/tests/nock/8LXMft4IyEWeaqoiynS5FA-1.dat new file mode 100644 index 000000000..e4e960864 Binary files /dev/null and b/tests/nock/8LXMft4IyEWeaqoiynS5FA-1.dat differ diff --git a/tests/nock/8LXMft4IyEWeaqoiynS5FA-2.dat b/tests/nock/8LXMft4IyEWeaqoiynS5FA-2.dat new file mode 100644 index 000000000..b03652e61 Binary files /dev/null and b/tests/nock/8LXMft4IyEWeaqoiynS5FA-2.dat differ diff --git a/tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat b/tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat index bca89b9a5..77ad35844 100644 Binary files a/tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat and b/tests/nock/AL__3okpCdfjA6kGuG2rFQ-1.dat differ diff --git a/tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat b/tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat index 6f24034e5..884ebf33e 100644 Binary files a/tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat and b/tests/nock/bNE0FYc3WlnFGzjHaIdf5A-1.dat differ