-
Notifications
You must be signed in to change notification settings - Fork 11.9k
fix(@angular/cli): respect min-release-age during version resolution #33278
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
crimsondhaks
wants to merge
2
commits into
angular:main
Choose a base branch
from
crimsondhaks:fix/min-release-age-version-resolution
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
147 changes: 147 additions & 0 deletions
147
packages/angular/cli/src/package-managers/min-release-age.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| /** | ||
| * @fileoverview This file contains the logic for reading the user-configured | ||
| * "minimum release age" (a.k.a. install cooldown) from the active package | ||
| * manager. When configured, the CLI must respect the same gate during | ||
| * automatic version selection (e.g. `ng update`, `ng add`); otherwise it can | ||
| * pick a version that the package manager itself will refuse to install. | ||
| * | ||
| * Coverage notes: | ||
| * - **npm** reads `min-release-age` from `.npmrc` (value in days). | ||
| * See https://docs.npmjs.com/cli/v11/using-npm/config#min-release-age. | ||
| * - **pnpm 10.x** reads `minimum-release-age` from `.npmrc` (value in minutes). | ||
| * pnpm 11+ migrated the canonical setting to `minimumReleaseAge` in | ||
| * `pnpm-workspace.yaml`, which is not currently parsed by this utility. | ||
| * - **yarn-classic** has no native cooldown, but mirrors npm's `.npmrc` | ||
| * parsing, so we honor `min-release-age` when present. | ||
| * - **yarn (berry)** uses `npmMinimalAgeGate` in `.yarnrc.yml`, which is not | ||
| * currently parsed by this utility. | ||
| * - **bun** uses `install.minimumReleaseAge` in `bunfig.toml`, which is not | ||
| * currently parsed by this utility. | ||
| */ | ||
|
|
||
| import * as ini from 'ini'; | ||
| import { dirname, join } from 'node:path'; | ||
| import { Host } from './host'; | ||
| import { Logger } from './logger'; | ||
| import { PackageManagerDescriptor } from './package-manager-descriptor'; | ||
|
|
||
| const MS_PER_MINUTE = 60_000; | ||
| const MS_PER_DAY = 86_400_000; | ||
|
|
||
| /** | ||
| * Converts a value in `unit` to milliseconds. | ||
| */ | ||
| function toMs(value: number, unit: 'days' | 'minutes'): number { | ||
| return unit === 'days' ? value * MS_PER_DAY : value * MS_PER_MINUTE; | ||
| } | ||
|
|
||
| /** | ||
| * Reads and merges `.npmrc` files starting at `startDir` and walking up the | ||
| * directory tree until either a git repository root or the filesystem root is | ||
| * reached. | ||
| * | ||
| * Values defined in directories closer to `startDir` take precedence over | ||
| * those defined in ancestor directories. This mirrors how `npm` itself | ||
| * resolves project-level configuration. | ||
| * | ||
| * @returns The merged options as a plain object. Returns an empty object when | ||
| * no `.npmrc` files are found. | ||
| */ | ||
| async function readNpmrcChain( | ||
| host: Host, | ||
| startDir: string, | ||
| logger?: Logger, | ||
| ): Promise<Record<string, unknown>> { | ||
| const directoriesToVisit: string[] = []; | ||
|
|
||
| let currentDir = startDir; | ||
| while (true) { | ||
| directoriesToVisit.push(currentDir); | ||
|
|
||
| // Stop walking when we reach a git repository root, mirroring `discovery.ts`. | ||
| // `.git` may be a directory (regular repo), a file (submodules and | ||
| // worktrees use a `gitdir:` pointer file), or absent. We just need to | ||
| // know whether it exists; `host.stat` rejects when it doesn't. | ||
| try { | ||
| await host.stat(join(currentDir, '.git')); | ||
| break; | ||
| } catch { | ||
| // No `.git` here; continue searching upwards. | ||
| } | ||
|
|
||
| const parentDir = dirname(currentDir); | ||
| if (parentDir === currentDir) { | ||
| // Reached the filesystem root. | ||
| break; | ||
| } | ||
| currentDir = parentDir; | ||
| } | ||
|
|
||
| // Apply ancestor configs first so that closer-to-cwd values override them. | ||
| const merged: Record<string, unknown> = {}; | ||
| for (let i = directoriesToVisit.length - 1; i >= 0; i--) { | ||
| const npmrcPath = join(directoriesToVisit[i], '.npmrc'); | ||
| let contents: string; | ||
| try { | ||
| contents = await host.readFile(npmrcPath); | ||
| } catch { | ||
| // File not present or unreadable. | ||
| continue; | ||
| } | ||
|
|
||
| try { | ||
| const parsed = ini.parse(contents) as Record<string, unknown>; | ||
| Object.assign(merged, parsed); | ||
| logger?.debug(`Loaded options from '${npmrcPath}'.`); | ||
| } catch (e) { | ||
| logger?.debug(`Failed to parse '${npmrcPath}': ${e}.`); | ||
| } | ||
| } | ||
|
|
||
| return merged; | ||
| } | ||
|
|
||
| /** | ||
| * Determines the minimum release age (in milliseconds) configured for the | ||
| * given package manager. | ||
| * | ||
| * @param host A `Host` instance for reading configuration files. | ||
| * @param cwd The directory from which to start the configuration search. | ||
| * @param descriptor The active package manager's descriptor. | ||
| * @param logger An optional logger instance. | ||
| * @returns A non-negative number of milliseconds. Returns `0` when the active | ||
| * package manager has no minimum release age configured (or when this | ||
| * utility does not yet support reading the relevant configuration source). | ||
| */ | ||
| export async function getMinReleaseAgeMs( | ||
| host: Host, | ||
| cwd: string, | ||
| descriptor: PackageManagerDescriptor, | ||
| logger?: Logger, | ||
| ): Promise<number> { | ||
| const config = descriptor.minReleaseAge; | ||
| if (!config) { | ||
| return 0; | ||
| } | ||
|
|
||
| const npmrc = await readNpmrcChain(host, cwd, logger); | ||
| const rawValue = npmrc[config.key]; | ||
| if (rawValue === undefined || rawValue === null || rawValue === '') { | ||
| return 0; | ||
| } | ||
|
|
||
| const parsed = Number(rawValue); | ||
| if (!Number.isFinite(parsed) || parsed <= 0) { | ||
| return 0; | ||
| } | ||
|
|
||
| return toMs(parsed, config.unit); | ||
| } | ||
105 changes: 105 additions & 0 deletions
105
packages/angular/cli/src/package-managers/min-release-age_spec.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| /** | ||
| * @license | ||
| * Copyright Google LLC All Rights Reserved. | ||
| * | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://angular.dev/license | ||
| */ | ||
|
|
||
| import { getMinReleaseAgeMs } from './min-release-age'; | ||
| import { SUPPORTED_PACKAGE_MANAGERS } from './package-manager-descriptor'; | ||
| import { MockHost } from './testing/mock-host'; | ||
|
|
||
| const MS_PER_MINUTE = 60_000; | ||
| const MS_PER_DAY = 86_400_000; | ||
|
|
||
| describe('getMinReleaseAgeMs', () => { | ||
| it('returns 0 when the descriptor has no minReleaseAge configuration', async () => { | ||
| const host = new MockHost(); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.bun)).toBe(0); | ||
| }); | ||
|
|
||
| it('returns 0 when no .npmrc file is present', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/project/.git'); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe(0); | ||
| }); | ||
|
|
||
| it('reads npm `min-release-age` (in days) from project .npmrc', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/project/.git'); | ||
| host.setFile('/project/.npmrc', 'min-release-age=7\n'); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe( | ||
| 7 * MS_PER_DAY, | ||
| ); | ||
| }); | ||
|
|
||
| it('reads pnpm `minimum-release-age` (in minutes) from project .npmrc', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/project/.git'); | ||
| host.setFile('/project/.npmrc', 'minimum-release-age=1440\n'); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.pnpm)).toBe( | ||
| 1440 * MS_PER_MINUTE, | ||
| ); | ||
| }); | ||
|
|
||
| it('does not pick up an unrelated key', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/project/.git'); | ||
| host.setFile('/project/.npmrc', 'min-release-age=7\n'); | ||
|
|
||
| // pnpm uses `minimum-release-age`, not `min-release-age`. | ||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.pnpm)).toBe(0); | ||
| }); | ||
|
|
||
| it('walks up the directory tree until reaching the .git root', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/repo/.git'); | ||
| host.setFile('/repo/.npmrc', 'min-release-age=3\n'); | ||
|
|
||
| expect( | ||
| await getMinReleaseAgeMs(host, '/repo/packages/app', SUPPORTED_PACKAGE_MANAGERS.npm), | ||
| ).toBe(3 * MS_PER_DAY); | ||
| }); | ||
|
|
||
| it('lets values closer to the project override ancestor values', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/repo/.git'); | ||
| host.setFile('/repo/.npmrc', 'min-release-age=10\n'); | ||
| host.setFile('/repo/packages/app/.npmrc', 'min-release-age=2\n'); | ||
|
|
||
| expect( | ||
| await getMinReleaseAgeMs(host, '/repo/packages/app', SUPPORTED_PACKAGE_MANAGERS.npm), | ||
| ).toBe(2 * MS_PER_DAY); | ||
| }); | ||
|
|
||
| it('treats a `.git` file as a repo root (git submodules and worktrees)', async () => { | ||
| const host = new MockHost(); | ||
| // In a submodule or worktree `.git` is a regular file containing a | ||
| // `gitdir:` pointer, not a directory. The walk must still stop here. | ||
| host.setFile('/repo/.git', 'gitdir: /elsewhere/.git/modules/repo\n'); | ||
| host.setFile('/repo/.npmrc', 'min-release-age=4\n'); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/repo', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe( | ||
| 4 * MS_PER_DAY, | ||
| ); | ||
| }); | ||
|
|
||
| it('returns 0 for non-positive or non-numeric values', async () => { | ||
| const host = new MockHost(); | ||
| host.setDirectory('/project/.git'); | ||
| host.setFile('/project/.npmrc', 'min-release-age=not-a-number\n'); | ||
|
|
||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe(0); | ||
|
|
||
| host.setFile('/project/.npmrc', 'min-release-age=0\n'); | ||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe(0); | ||
|
|
||
| host.setFile('/project/.npmrc', 'min-release-age=-5\n'); | ||
| expect(await getMinReleaseAgeMs(host, '/project', SUPPORTED_PACKAGE_MANAGERS.npm)).toBe(0); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.