diff --git a/README.md b/README.md index 5823ea6..c160e50 100644 --- a/README.md +++ b/README.md @@ -201,7 +201,7 @@ steps: working-directory: csharp ``` -> **Note**: The action supports `latest*` variants of the [rollForward](https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#rollforward) field in `global.json`. When set to `latestPatch`, `latestFeature`, `latestMinor`, or `latestMajor`, the action installs the appropriate SDK version. For prerelease versions, the exact pinned version is always installed regardless of the `rollForward` setting. +> **Note**: The action supports `latest*` variants of the [rollForward](https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#rollforward) field in `global.json`. When set to `latestPatch`, `latestFeature`, `latestMinor`, or `latestMajor`, the action installs the appropriate SDK version. With `latestMajor`, the action installs the newest active GA release, which can be an STS release rather than LTS; `dotnet-quality` is not applied. If the installed SDK is older than the `sdk.version` declared in `global.json`, the action fails. For prerelease versions, the exact pinned version is always installed regardless of the `rollForward` setting. > **Important**: When using `rollForward` in `global.json`, the `sdk.version` field must be a fully-qualified SDK version (e.g., `8.0.100`, `10.0.100`). Wildcard versions (e.g., `10.0.*`) and runtime-style versions (e.g., `8.0.0`) are not supported. See the [.NET SDK version specification](https://learn.microsoft.com/en-us/dotnet/core/tools/global-json#version) for details. diff --git a/__tests__/installer.test.ts b/__tests__/installer.test.ts index 423420c..60b2f87 100644 --- a/__tests__/installer.test.ts +++ b/__tests__/installer.test.ts @@ -33,6 +33,35 @@ jest.unstable_mockModule('@actions/core', () => ({ jest.unstable_mockModule('@actions/io', () => ({ which: jest.fn() })); +// Keeps resolution of the releases index deterministic and offline. +const releasesIndex = { + 'releases-index': [ + { + 'channel-version': '12.0', + 'support-phase': 'preview', + 'release-type': 'lts' + }, + { + 'channel-version': '11.0', + 'support-phase': 'active', + 'release-type': 'sts' + }, + { + 'channel-version': '10.0', + 'support-phase': 'active', + 'release-type': 'lts' + }, + {'channel-version': '9.0', 'support-phase': 'eol', 'release-type': 'sts'} + ] +}; +jest.unstable_mockModule('@actions/http-client', () => ({ + // A plain class, so jest.resetAllMocks() does not clear the response. + HttpClient: class { + async getJson() { + return {statusCode: 200, result: releasesIndex, headers: {}}; + } + } +})); jest.unstable_mockModule('fs', () => { const actual = jest.requireActual('fs') as typeof import('fs'); const chmodSync = jest.fn(); @@ -586,7 +615,7 @@ describe('installer tests', () => { getExecOutputSpy.mockImplementation(() => Promise.resolve({ exitCode: 0, - stdout: 'Fictitious dotnet version 1.2.3 is installed', + stdout: 'Fictitious dotnet version 99.0.100 is installed', stderr: '' }) ); @@ -1244,6 +1273,90 @@ describe('installer tests', () => { ); }); + describe('global.json online resolution tests', () => { + const mockInstalledVersion = (version: string) => + getExecOutputSpy.mockImplementation(() => + Promise.resolve({ + exitCode: 0, + stdout: `Fictitious dotnet version ${version} is installed`, + stderr: '' + }) + ); + const installArguments = () => + (getExecOutputSpy.mock.calls.at(-1)![1] as string[]).join(' '); + const channelArgument = (channel: string) => + IS_WINDOWS ? `-Channel ${channel}` : `--channel ${channel}`; + + beforeEach(() => { + readdirSyncSpy.mockReturnValue(makeDirents([])); + }); + + it('installs the newest active channel for latestMajor even when it is STS', async () => { + mockInstalledVersion('11.0.100'); + + const installedVersion = await new installer.DotnetCoreInstaller( + '', + '', + undefined, + undefined, + true, + '10.0.100', + 'latestMajor' + ).installDotnet(); + + expect(installedVersion).toBe('11.0.100'); + expect(installArguments()).toContain(channelArgument('11.0')); + }); + + it('keeps installing from the LTS channel for an empty version without latestMajor', async () => { + mockInstalledVersion('10.0.100'); + + await new installer.DotnetCoreInstaller( + '', + '', + undefined, + undefined, + true + ).installDotnet(); + + expect(installArguments()).toContain(channelArgument('LTS')); + }); + + it('fails when the installed SDK in the newest channel is below the global.json minimum', async () => { + mockInstalledVersion('11.0.100'); + + await expect( + new installer.DotnetCoreInstaller( + '', + '', + undefined, + undefined, + true, + '11.0.200', + 'latestMajor' + ).installDotnet() + ).rejects.toThrow( + 'The installed .NET SDK 11.0.100 is older than the global.json minimum version 11.0.200.' + ); + }); + + it('accepts an installed SDK equal to the global.json minimum', async () => { + mockInstalledVersion('11.0.200'); + + const installedVersion = await new installer.DotnetCoreInstaller( + '', + '', + undefined, + undefined, + true, + '11.0.200', + 'latestMajor' + ).installDotnet(); + + expect(installedVersion).toBe('11.0.200'); + }); + }); + describe('skip-lts-prepass tests', () => { it('should run the LTS pre-pass by default (skip-lts-prepass not set)', async () => { const inputVersion = '9.0.100'; diff --git a/__tests__/latest-version.test.ts b/__tests__/latest-version.test.ts index 1dbc697..a1d6d98 100644 --- a/__tests__/latest-version.test.ts +++ b/__tests__/latest-version.test.ts @@ -117,6 +117,45 @@ describe('DotnetVersionResolver with latest', () => { expect(version.value).toBe('9.0'); }); + it('should resolve an empty version (global.json latestMajor) to the highest stable release of any support type', async () => { + getJsonMock.mockResolvedValue({result: mockReleases}); + + const resolver = new DotnetVersionResolver( + '', + '', + undefined, + 'latestMajor' + ); + const version = await resolver.createDotnetVersion(); + + expect(version.value).toBe('9.0'); + expect(version.type.toLowerCase()).toContain('channel'); + expect(version.qualityFlag).toBe(false); + }); + + it('should keep resolving an empty version (global.json latestMajor) to a stable release when quality is preview', async () => { + getJsonMock.mockResolvedValue({result: mockReleases}); + + const resolver = new DotnetVersionResolver( + '', + 'preview', + undefined, + 'latestMajor' + ); + const version = await resolver.createDotnetVersion(); + + expect(version.value).toBe('9.0'); + expect(version.qualityFlag).toBe(false); + }); + + it('should keep resolving an empty version without latestMajor to the LTS channel', async () => { + const resolver = new DotnetVersionResolver(''); + const version = await resolver.createDotnetVersion(); + + expect(version.value).toBe('LTS'); + expect(getJsonMock).not.toHaveBeenCalled(); + }); + it('should warn if channel is provided but version is not latest', async () => { getJsonMock.mockResolvedValue({result: mockReleases}); diff --git a/dist/setup/index.js b/dist/setup/index.js index a416aca..dd1a226 100644 --- a/dist/setup/index.js +++ b/dist/setup/index.js @@ -43980,11 +43980,13 @@ function channelForMajor(major) { class DotnetVersionResolver { quality; dotnetChannel; + rollForward; inputVersion; resolvedArgument; - constructor(version, quality = '', dotnetChannel) { + constructor(version, quality = '', dotnetChannel, rollForward) { this.quality = quality; this.dotnetChannel = dotnetChannel; + this.rollForward = rollForward; this.inputVersion = version.trim(); this.resolvedArgument = { type: '', value: '', qualityFlag: false }; } @@ -44018,6 +44020,12 @@ class DotnetVersionResolver { latestChannelMajorTag >= QUALITY_INPUT_MINIMAL_MAJOR_TAG; return; } + if (this.inputVersion === '' && this.rollForward === 'latestMajor') { + // global.json latestMajor: newest GA release, LTS or STS + this.resolvedArgument.type = 'channel'; + this.resolvedArgument.value = await this.getLatestVersion('', ''); + return; + } if (this.dotnetChannel) { warning(`The 'dotnet-channel' input is only supported when 'dotnet-version' is set to 'latest'.`); } @@ -44080,7 +44088,7 @@ class DotnetVersionResolver { } return this.resolvedArgument; } - async getLatestVersion(channelFilter) { + async getLatestVersion(channelFilter, quality = this.quality) { const httpClient = new lib_HttpClient('actions/setup-dotnet', [], { allowRetries: true, maxRetries: 3 @@ -44096,7 +44104,7 @@ class DotnetVersionResolver { releasesInfo = releasesInfo.filter(info => info['support-phase'] !== 'eol'); // Filter out preview versions if quality is not 'preview' or 'daily' // If quality is not specified, we assume strict stability (GA only) - const normalizedQuality = (this.quality || '').toLowerCase(); + const normalizedQuality = (quality || '').toLowerCase(); if (!['preview', 'daily'].includes(normalizedQuality)) { releasesInfo = releasesInfo.filter(info => info['support-phase'] !== 'preview'); } @@ -44584,7 +44592,7 @@ class DotnetCoreInstaller { } info(`'check-latest' is false but no locally installed .NET SDK satisfies the '${this.version}' request. Falling back to online installation.`); } - const versionResolver = new DotnetVersionResolver(this.version, this.quality, this.dotnetChannel); + const versionResolver = new DotnetVersionResolver(this.version, this.quality, this.dotnetChannel, this.rollForward); const dotnetVersion = await versionResolver.createDotnetVersion(); const architectureArguments = isCrossArch ? [ @@ -44636,7 +44644,14 @@ class DotnetCoreInstaller { if (dotnetInstallOutput.exitCode) { throw new Error(`Failed to install dotnet, exit code: ${dotnetInstallOutput.exitCode}. ${dotnetInstallOutput.stderr}`); } - return this.parseInstalledVersion(dotnetInstallOutput.stdout); + const installedVersion = this.parseInstalledVersion(dotnetInstallOutput.stdout); + if (installedVersion && + this.minimumVersion && + semver_default().valid(installedVersion) && + semver_default().lt(installedVersion, this.minimumVersion)) { + throw new Error(`The installed .NET SDK ${installedVersion} is older than the global.json minimum version ${this.minimumVersion}.`); + } + return installedVersion; } async installRuntime() { this.validateRuntimeVersion(); diff --git a/src/installer.ts b/src/installer.ts index d4c9857..4c59207 100644 --- a/src/installer.ts +++ b/src/installer.ts @@ -78,7 +78,8 @@ export class DotnetVersionResolver { constructor( version: string, private quality: QualityOptions = '', - private dotnetChannel?: string + private dotnetChannel?: string, + private rollForward?: string ) { this.inputVersion = version.trim(); this.resolvedArgument = {type: '', value: '', qualityFlag: false}; @@ -118,6 +119,13 @@ export class DotnetVersionResolver { return; } + if (this.inputVersion === '' && this.rollForward === 'latestMajor') { + // global.json latestMajor: newest GA release, LTS or STS + this.resolvedArgument.type = 'channel'; + this.resolvedArgument.value = await this.getLatestVersion('', ''); + return; + } + if (this.dotnetChannel) { core.warning( `The 'dotnet-channel' input is only supported when 'dotnet-version' is set to 'latest'.` @@ -192,7 +200,10 @@ export class DotnetVersionResolver { return this.resolvedArgument; } - private async getLatestVersion(channelFilter: string): Promise { + private async getLatestVersion( + channelFilter: string, + quality: QualityOptions = this.quality + ): Promise { const httpClient = new hc.HttpClient('actions/setup-dotnet', [], { allowRetries: true, maxRetries: 3 @@ -216,7 +227,7 @@ export class DotnetVersionResolver { // Filter out preview versions if quality is not 'preview' or 'daily' // If quality is not specified, we assume strict stability (GA only) - const normalizedQuality = (this.quality || '').toLowerCase(); + const normalizedQuality = (quality || '').toLowerCase(); if (!['preview', 'daily'].includes(normalizedQuality)) { releasesInfo = releasesInfo.filter( info => info['support-phase'] !== 'preview' @@ -877,7 +888,8 @@ export class DotnetCoreInstaller { const versionResolver = new DotnetVersionResolver( this.version, this.quality, - this.dotnetChannel + this.dotnetChannel, + this.rollForward ); const dotnetVersion = await versionResolver.createDotnetVersion(); @@ -942,7 +954,20 @@ export class DotnetCoreInstaller { ); } - return this.parseInstalledVersion(dotnetInstallOutput.stdout); + const installedVersion = this.parseInstalledVersion( + dotnetInstallOutput.stdout + ); + if ( + installedVersion && + this.minimumVersion && + semver.valid(installedVersion) && + semver.lt(installedVersion, this.minimumVersion) + ) { + throw new Error( + `The installed .NET SDK ${installedVersion} is older than the global.json minimum version ${this.minimumVersion}.` + ); + } + return installedVersion; } public async installRuntime(): Promise {