Skip to content

Commit 95a3f8b

Browse files
Validate global.json SDK version before rollForward optimization (#742)
1 parent 9a946fd commit 95a3f8b

4 files changed

Lines changed: 20 additions & 5 deletions

File tree

‎.github/workflows/e2e-tests.yml‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -273,14 +273,14 @@ jobs:
273273
shell: bash
274274
run: |
275275
mkdir subdirectory
276-
echo '{"sdk":{"version": "3.1.0","rollForward": "latestMajor"}}' > ./subdirectory/global.json
276+
echo '{"sdk":{"version": "8.0.100","rollForward": "latestMajor"}}' > ./subdirectory/global.json
277277
- name: Setup dotnet
278278
uses: ./
279279
with:
280280
global-json-file: ./subdirectory/global.json
281281
- name: Verify dotnet
282282
shell: pwsh
283-
run: __tests__/verify-dotnet.ps1 -Patterns "^(?!3)"
283+
run: __tests__/verify-dotnet.ps1 -Patterns "^(?!8)"
284284

285285
test-setup-global-json-rollforward-latestfeature:
286286
runs-on: ${{ matrix.operating-system }}

‎README.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ steps:
142142
working-directory: csharp
143143
```
144144

145-
> **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.
145+
> **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.
146146

147147
## Caching NuGet Packages
148148
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.

‎dist/setup/index.js‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79244,7 +79244,13 @@ function getVersionFromGlobalJson(globalJsonPath) {
7924479244
if (globalJson.sdk && globalJson.sdk.version) {
7924579245
version = globalJson.sdk.version;
7924679246
const rollForward = globalJson.sdk.rollForward;
79247-
if (rollForward) {
79247+
if (rollForward && !semver_1.default.prerelease(version)) {
79248+
const versionPattern = /^\d+\.\d+\.[1-9]\d{2,}$/;
79249+
if (!versionPattern.test(version)) {
79250+
throw new Error(`Version '${version}' is not valid for the 'sdk.version' value in global.json. ` +
79251+
`When 'rollForward' is specified, a full SDK version is required. ` +
79252+
`See: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json`);
79253+
}
7924879254
const [major, minor, featurePatch] = version.split('.');
7924979255
const feature = featurePatch.substring(0, 1);
7925079256
switch (rollForward) {

‎src/setup-dotnet.ts‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,16 @@ function getVersionFromGlobalJson(globalJsonPath: string): string {
207207
if (globalJson.sdk && globalJson.sdk.version) {
208208
version = globalJson.sdk.version;
209209
const rollForward = globalJson.sdk.rollForward;
210-
if (rollForward) {
210+
if (rollForward && !semver.prerelease(version)) {
211+
const versionPattern = /^\d+\.\d+\.[1-9]\d{2,}$/;
212+
if (!versionPattern.test(version)) {
213+
throw new Error(
214+
`Version '${version}' is not valid for the 'sdk.version' value in global.json. ` +
215+
`When 'rollForward' is specified, a full SDK version is required. ` +
216+
`See: https://learn.microsoft.com/en-us/dotnet/core/tools/global-json`
217+
);
218+
}
219+
211220
const [major, minor, featurePatch] = version.split('.');
212221
const feature = featurePatch.substring(0, 1);
213222

0 commit comments

Comments
 (0)