Validate global.json SDK version before rollForward optimization (#742)

This commit is contained in:
Priya Gupta
2026-06-16 22:56:42 +05:30
committed by GitHub
parent 9a946fdbd5
commit 95a3f8b067
4 changed files with 20 additions and 5 deletions

View File

@@ -273,14 +273,14 @@ jobs:
shell: bash shell: bash
run: | run: |
mkdir subdirectory mkdir subdirectory
echo '{"sdk":{"version": "3.1.0","rollForward": "latestMajor"}}' > ./subdirectory/global.json echo '{"sdk":{"version": "8.0.100","rollForward": "latestMajor"}}' > ./subdirectory/global.json
- name: Setup dotnet - name: Setup dotnet
uses: ./ uses: ./
with: with:
global-json-file: ./subdirectory/global.json global-json-file: ./subdirectory/global.json
- name: Verify dotnet - name: Verify dotnet
shell: pwsh shell: pwsh
run: __tests__/verify-dotnet.ps1 -Patterns "^(?!3)" run: __tests__/verify-dotnet.ps1 -Patterns "^(?!8)"
test-setup-global-json-rollforward-latestfeature: test-setup-global-json-rollforward-latestfeature:
runs-on: ${{ matrix.operating-system }} runs-on: ${{ matrix.operating-system }}

View File

@@ -142,7 +142,7 @@ steps:
working-directory: csharp 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. > **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.
## Caching NuGet Packages ## Caching NuGet Packages
The action has a built-in functionality for caching and restoring dependencies. It uses [toolkit/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under the hood for caching global packages data but requires less configuration settings. The `cache` input is optional, and caching is turned off by default. The action has a built-in functionality for caching and restoring dependencies. It uses [toolkit/cache](https://github.com/actions/toolkit/tree/main/packages/cache) under the hood for caching global packages data but requires less configuration settings. The `cache` input is optional, and caching is turned off by default.

8
dist/setup/index.js vendored
View File

@@ -79244,7 +79244,13 @@ function getVersionFromGlobalJson(globalJsonPath) {
if (globalJson.sdk && globalJson.sdk.version) { if (globalJson.sdk && globalJson.sdk.version) {
version = globalJson.sdk.version; version = globalJson.sdk.version;
const rollForward = globalJson.sdk.rollForward; const rollForward = globalJson.sdk.rollForward;
if (rollForward) { if (rollForward && !semver_1.default.prerelease(version)) {
const versionPattern = /^\d+\.\d+\.[1-9]\d{2,}$/;
if (!versionPattern.test(version)) {
throw new Error(`Version '${version}' is not valid for the 'sdk.version' value in global.json. ` +
`When 'rollForward' is specified, a full SDK version is required. ` +
`See: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json`);
}
const [major, minor, featurePatch] = version.split('.'); const [major, minor, featurePatch] = version.split('.');
const feature = featurePatch.substring(0, 1); const feature = featurePatch.substring(0, 1);
switch (rollForward) { switch (rollForward) {

View File

@@ -207,7 +207,16 @@ function getVersionFromGlobalJson(globalJsonPath: string): string {
if (globalJson.sdk && globalJson.sdk.version) { if (globalJson.sdk && globalJson.sdk.version) {
version = globalJson.sdk.version; version = globalJson.sdk.version;
const rollForward = globalJson.sdk.rollForward; const rollForward = globalJson.sdk.rollForward;
if (rollForward) { if (rollForward && !semver.prerelease(version)) {
const versionPattern = /^\d+\.\d+\.[1-9]\d{2,}$/;
if (!versionPattern.test(version)) {
throw new Error(
`Version '${version}' is not valid for the 'sdk.version' value in global.json. ` +
`When 'rollForward' is specified, a full SDK version is required. ` +
`See: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json`
);
}
const [major, minor, featurePatch] = version.split('.'); const [major, minor, featurePatch] = version.split('.');
const feature = featurePatch.substring(0, 1); const feature = featurePatch.substring(0, 1);