From f86ce9b18bce2c3d24b14b0141ddde548ef8a032 Mon Sep 17 00:00:00 2001 From: Rhys Arkins Date: Mon, 11 Mar 2024 19:12:54 +0100 Subject: [PATCH] feat(npm): support corepack packageManager URLs Closes #27656 --- lib/modules/manager/npm/post-update/utils.ts | 6 ++- .../manager/npm/post-update/yarn.spec.ts | 47 +++++++++++++++++++ lib/modules/manager/npm/schema.ts | 2 +- 3 files changed, 53 insertions(+), 2 deletions(-) diff --git a/lib/modules/manager/npm/post-update/utils.ts b/lib/modules/manager/npm/post-update/utils.ts index e799e3165285dc..6b3e246d11c27e 100644 --- a/lib/modules/manager/npm/post-update/utils.ts +++ b/lib/modules/manager/npm/post-update/utils.ts @@ -1,3 +1,4 @@ +import semver from 'semver'; import upath from 'upath'; import { logger } from '../../../../logger'; import { readLocalFile } from '../../../../util/fs'; @@ -35,7 +36,10 @@ export function getPackageManagerVersion( logger.debug( `Found ${name} constraint in package.json packageManager: ${version}`, ); - return version; + if (semver.valid(version)) { + return version; + } + return null; } if (pkg.engines?.[name]) { const version = pkg.engines[name]; diff --git a/lib/modules/manager/npm/post-update/yarn.spec.ts b/lib/modules/manager/npm/post-update/yarn.spec.ts index 75be65cb52fa1b..005675b1ab6650 100644 --- a/lib/modules/manager/npm/post-update/yarn.spec.ts +++ b/lib/modules/manager/npm/post-update/yarn.spec.ts @@ -443,6 +443,53 @@ describe('modules/manager/npm/post-update/yarn', () => { expect(res.lockFile).toBe('package-lock-contents'); }); + it('supports packageManager url corepack', async () => { + process.env.CONTAINERBASE = 'true'; + GlobalConfig.set({ + localDir: '.', + binarySource: 'install', + cacheDir: '/tmp/cache', + }); + const yarnLockContents = `__metadata: + version: 6 + cacheKey: 8`; + Fixtures.mock( + { + 'package.json': + '{ "packageManager": "yarn@https://nexus-proxy.repo.local.company.net/nexus/content/groups/npm-all/@yarnpkg/cli-dist/-/cli-dist-3.7.0.tgz#sha224.a06723957ae0292e21f598a453" }', + 'yarn.lock': yarnLockContents, + }, + 'some-dir', + ); + mockedFunction(getPkgReleases).mockResolvedValueOnce({ + releases: [{ version: '0.10.0' }], + }); + const execSnapshots = mockExecAll({ + stdout: '2.1.0', + stderr: '', + }); + const config = partial>({ + managerData: { hasPackageManager: true }, + }); + const res = await yarnHelper.generateLockFile('some-dir', {}, config); + expect(execSnapshots).toMatchObject([ + { cmd: 'install-tool node 16.16.0', options: { cwd: 'some-dir' } }, + { cmd: 'install-tool corepack 0.10.0', options: { cwd: 'some-dir' } }, + { + cmd: 'yarn install --mode=update-lockfile', + options: { + cwd: 'some-dir', + env: { + YARN_ENABLE_GLOBAL_CACHE: '1', + YARN_ENABLE_IMMUTABLE_INSTALLS: 'false', + YARN_HTTP_TIMEOUT: '100000', + }, + }, + }, + ]); + expect(res.lockFile).toBe(yarnLockContents); + }); + it('supports corepack on grouping', async () => { process.env.CONTAINERBASE = 'true'; GlobalConfig.set({ diff --git a/lib/modules/manager/npm/schema.ts b/lib/modules/manager/npm/schema.ts index 00847ca8c0210e..79d986fea78507 100644 --- a/lib/modules/manager/npm/schema.ts +++ b/lib/modules/manager/npm/schema.ts @@ -4,7 +4,7 @@ import { Json, LooseRecord } from '../../../util/schema-utils'; export const PackageManagerSchema = z .string() .transform((val) => val.split('@')) - .transform(([name, version]) => ({ name, version })); + .transform(([name, ...version]) => ({ name, version: version.join('@') })); export const PackageJsonSchema = z.object({ engines: LooseRecord(z.string()).optional(),