From a2cb8a25478e3270b14f685793255cad48b4f1cd Mon Sep 17 00:00:00 2001 From: monsieurleberre Date: Sat, 21 Nov 2026 07:42:03 -0300 Subject: [PATCH 1/3] feat(csharp-ci): fail when a packable net10.0 target resolves Microsoft.Extensions or AspNetCore 11 --- .github/actionlint.yaml | 3 + .github/workflows/build-and-test.yaml | 25 ++ .github/workflows/csharp-ci.yaml | 21 +- CHANGELOG.md | 4 + README.md | 13 + scripts/guard-net10-package-majors.sh | 56 ++++ scripts/net10-package-majors.jq | 38 +++ test/guard-net10-package-majors_test.sh | 345 ++++++++++++++++++++++++ 8 files changed, 504 insertions(+), 1 deletion(-) create mode 100755 scripts/guard-net10-package-majors.sh create mode 100644 scripts/net10-package-majors.jq create mode 100755 test/guard-net10-package-majors_test.sh diff --git a/.github/actionlint.yaml b/.github/actionlint.yaml index cc592be..f34d934 100644 --- a/.github/actionlint.yaml +++ b/.github/actionlint.yaml @@ -1,3 +1,6 @@ +self-hosted-runner: + labels: + - windows-11-arm paths: .github/workflows/*.yaml: # actionlint 1.7.7's job-context schema predates job.workflow_sha (documented in the GitHub Actions contexts reference) diff --git a/.github/workflows/build-and-test.yaml b/.github/workflows/build-and-test.yaml index 3c76faa..9a860e2 100644 --- a/.github/workflows/build-and-test.yaml +++ b/.github/workflows/build-and-test.yaml @@ -83,3 +83,28 @@ jobs: - name: Run python tests run: python3 -m unittest discover -s test -p '*_test.py' + + test-net10-guard: + name: test net10.0 package guard (${{ matrix.os }}) + runs-on: ${{ matrix.os }} + permissions: + contents: read + strategy: + fail-fast: false + matrix: + os: [ubuntu-latest, macos-latest, windows-latest, windows-11-arm] + defaults: + run: + shell: bash + steps: + - uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 + with: + persist-credentials: false + + - name: Setup .NET + uses: actions/setup-dotnet@a98b56852c35b8e3190ac28c8c2271da59106c68 # v6.0.0 + with: + dotnet-version: '10.0.x' + + - name: Test net10.0 package guard + run: bash test/guard-net10-package-majors_test.sh diff --git a/.github/workflows/csharp-ci.yaml b/.github/workflows/csharp-ci.yaml index 92c4ff4..d5a2bc6 100644 --- a/.github/workflows/csharp-ci.yaml +++ b/.github/workflows/csharp-ci.yaml @@ -200,6 +200,19 @@ on: required: false type: string default: '' + net10-package-exceptions: + description: >- + Comma or whitespace separated `package@major` pairs (for example + `Microsoft.Extensions.Options@11`) that the net10.0 package guard + accepts. After restore, the guard fails every `build-and-test` shard + when a packable project's net10.0 target (a RID-specific + `net10.0/` target included) resolves a `Microsoft.Extensions.*` + or `Microsoft.AspNetCore.*` package at major 11 or higher, directly + or transitively. The package id match is case-insensitive and the + major must match exactly. Empty accepts nothing. + required: false + type: string + default: '' artifact-prefix: description: >- Prefix applied to per-run artifact names (coverage-percent, @@ -287,7 +300,6 @@ jobs: global-json-file: ${{ inputs.dotnet-version == '' && ((inputs.working-directory == '.' || inputs.working-directory == '') && 'global.json' || format('{0}/global.json', inputs.working-directory)) || '' }} - name: Checkout CI helpers - if: ${{ matrix.coverage }} uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 with: repository: peacefulstudio/github-actions @@ -316,6 +328,13 @@ jobs: set -euo pipefail dotnet restore + - name: Guard net10.0 package majors + env: + NET10_PACKAGE_EXCEPTIONS: ${{ inputs.net10-package-exceptions }} + run: | + set -euo pipefail + "$GITHUB_WORKSPACE/.github-actions-helpers/scripts/guard-net10-package-majors.sh" + - name: Build run: | set -euo pipefail diff --git a/CHANGELOG.md b/CHANGELOG.md index 31f21f8..4db2395 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Changed + +- `csharp-ci.yaml` fails every `build-and-test` shard, in a new `Guard net10.0 package majors` step between `Restore` and `Build`, when a packable project's `net10.0` target resolves a `Microsoft.Extensions.*` or `Microsoft.AspNetCore.*` package at major 11 or later, direct or transitive. A library that ships a `net10.0` asset would otherwise force its consumers onto the 11.x line, and CI stayed green because `net10.0` builds and tests fine with 11.x packages. The step reads the resolved graph from each project's `obj/project.assets.json`, `net10.0/` targets included, so a minor or patch bump of any package that pulls in 11.x is caught, not only a bump of a named package. It checks only projects that MSBuild evaluates as packable with a `net10.0` target framework (`TargetFrameworks` may use spaces, newlines or any letter case): test projects, non-packable apps and `net11.0`-only projects are untouched, and the `net11.0` target of a multi-targeted library is not read. One `::error::` annotation per project names the project, each package, its resolved version and the top-level package that pulled it in. A packable `net10.0` project with no restore assets (one outside the restored solution) gets a warning and is skipped. New optional input `net10-package-exceptions` (default empty) takes comma or whitespace separated `package@major` pairs to accept; it reaches the step only through the environment and a malformed entry fails the step before anything is checked. The check is on by default with no caller change, so a caller whose `net10.0` target resolves 11.x goes red until it pins the package to 10.x or lists an exception. The step needs `jq`, which the GitHub-hosted images and the self-hosted Hetzner runners provide, and it strips the carriage returns `jq` and `dotnet` print under Git Bash on Windows so that a Windows shard checks the same projects as a Linux one. The helper checkout (`Checkout CI helpers`) now runs on every shard, not only the coverage shard. `scripts/guard-net10-package-majors.sh` carries the logic, and `test/guard-net10-package-majors_test.sh` runs it against fixture projects on Linux, macOS, Windows and Windows ARM in `build-and-test.yaml`. + ## [2.8.1] - 2026-11-06 ### Fixed diff --git a/README.md b/README.md index 9d2b107..a685310 100644 --- a/README.md +++ b/README.md @@ -150,6 +150,19 @@ choosing which shard carries coverage — use `build-matrix` instead (see | `coverage-pr-comment-header` | `csharp-coverage` | Sticky-comment header (make unique per workspace if a repo calls this workflow more than once). | | `pack` | `false` | When true, also runs `dotnet pack` and uploads `.nupkg` artifacts. | | `pack-project` | *(empty)* | Project to pack. Required when `pack: true`. | +| `net10-package-exceptions` | *(empty)* | Comma or whitespace separated `package@major` pairs (e.g. `Microsoft.Extensions.Options@11`) the net10.0 package guard accepts. See the net10.0 package guard note below. | + +**net10.0 package guard**: after restore, a `Guard net10.0 package majors` step +fails the build when a packable project with a `net10.0` target framework +resolves any `Microsoft.Extensions.*` or `Microsoft.AspNetCore.*` package at +major 11 or later, directly or transitively (it reads `obj/project.assets.json`, +including `net10.0/` targets). A `net10.0` package that requires the 11.x +line would otherwise pull its consumers off 10.x while CI stays green. The error +names the project, the package, its version and the top-level package that +brought it in. Accept a known case with +`net10-package-exceptions: 'Microsoft.Extensions.Options@11'`. Test projects, +non-packable projects and `net11.0`-only projects are not checked. The step +needs `jq` on the runner. **Restore is credential-free**: `dotnet restore` and `dotnet pack` run with no token and resolve every package from nuget.org. The private GitHub Packages feed diff --git a/scripts/guard-net10-package-majors.sh b/scripts/guard-net10-package-majors.sh new file mode 100755 index 0000000..2638986 --- /dev/null +++ b/scripts/guard-net10-package-majors.sh @@ -0,0 +1,56 @@ +#!/usr/bin/env bash +# Copyright (c) 2026 Peaceful Studio OÜ +# SPDX-License-Identifier: Apache-2.0 +set -euo pipefail + +here="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)" + +for tool in jq dotnet; do + if ! command -v "$tool" > /dev/null; then + echo "::error::the net10.0 package guard needs '$tool' on the runner PATH" + exit 3 + fi +done + +# jq.exe and dotnet.exe print CRLF line endings under Git Bash on Windows (jq 1.7.1, windows-latest), which leaves a trailing \r on every captured value +strip_cr() { tr -d '\r'; } +jq_clean() { jq "$@" | strip_cr; } + +filter="$(strip_cr < "$here/net10-package-majors.jq")" + +entries="$(printf '%s' "${NET10_PACKAGE_EXCEPTIONS:-}" | tr ',[:space:]' '\n' | sed '/^$/d')" +malformed="$(grep -Ev '^[A-Za-z0-9][A-Za-z0-9._-]*@[1-9][0-9]*$' <<< "$entries" || true)" +if [ -n "$malformed" ]; then + echo "::error::net10-package-exceptions entries must look like package@major, got: $(paste -sd ' ' - <<< "$malformed")" + exit 2 +fi +allowed="$(printf '%s\n' "$entries" | jq_clean -R -s 'split("\n") | map(select(length > 0) | ascii_downcase)')" + +failed=0 +while IFS= read -r project; do + [ -n "$project" ] || continue + display="${project#./}" + properties="$(dotnet msbuild "$project" -nologo -getProperty:IsPackable -getProperty:TargetFramework -getProperty:TargetFrameworks -getProperty:ProjectAssetsFile -getProperty:MSBuildProjectExtensionsPath | strip_cr)" + packable="$(jq_clean -r '.Properties.IsPackable' <<< "$properties")" + frameworks=";$(jq_clean -r '.Properties.TargetFrameworks + ";" + .Properties.TargetFramework' <<< "$properties" | tr -d '[:space:]');" + assets="$(jq_clean -r '.Properties.ProjectAssetsFile' <<< "$properties")" + if [ -z "$assets" ]; then + assets="$(jq_clean -r '.Properties.MSBuildProjectExtensionsPath' <<< "$properties")project.assets.json" + fi + if [ "$packable" != "true" ] || ! grep -Eqi ';net10\.0(-[^;]*)?;' <<< "$frameworks"; then + continue + fi + if command -v cygpath > /dev/null; then + assets="$(cygpath -u "$assets")" + fi + if [ ! -f "$assets" ]; then + echo "::warning::$display is packable for net10.0 but has no restore assets at $assets, so its package versions were not checked" + continue + fi + findings="$(jq_clean -r --argjson allowed "$allowed" "$filter" < "$assets" | sort -u | paste -sd ';' - | sed 's/;/; /g')" + if [ -n "$findings" ]; then + echo "::error::$display resolves $findings for its net10.0 target. A net10.0 package must not require Microsoft.Extensions or Microsoft.AspNetCore 11 or later; pin it to 10.x, or accept it with the net10-package-exceptions input." + failed=1 + fi +done < <(find . \( -name obj -o -name bin -o -name node_modules -o -name .git -o -name .github-actions-helpers \) -prune -o \( -name '*.csproj' -o -name '*.fsproj' -o -name '*.vbproj' \) -print | sort) +exit "$failed" diff --git a/scripts/net10-package-majors.jq b/scripts/net10-package-majors.jq new file mode 100644 index 0000000..032b0d8 --- /dev/null +++ b/scripts/net10-package-majors.jq @@ -0,0 +1,38 @@ +def package_name: split("/")[0]; +def package_version: split("/")[1]; +def watched: test("^(microsoft\\.extensions|microsoft\\.aspnetcore)\\."; "i"); +def major: package_version | split(".")[0] | tonumber; +def first_direct($reverse; $direct; $start): + def walk($frontier; $seen): + if ($frontier | length) == 0 then null + else + ([$frontier[] | select(. as $n | $direct | index($n | ascii_downcase))]) as $hits + | if ($hits | length) > 0 then $hits[0] + else + ([$frontier[] | ($reverse[ascii_downcase] // [])[]] | unique - $seen) as $next + | walk($next; $seen + $next) + end + end; + walk([$start]; [$start]); +. as $assets +| ($assets.targets // {}) | to_entries[] +| select(.key | test("^net10\\.0(-[^/]+)?(/.*)?$"; "i")) +| .key as $target +| .value as $libraries +| ([($assets.projectFileDependencyGroups[$target | split("/")[0]] // [])[] | split(" ")[0] | ascii_downcase]) as $direct +| (reduce ($libraries | to_entries[]) as $library ({}; + reduce (($library.value.dependencies // {}) | keys[]) as $dependency (.; + .[$dependency | ascii_downcase] += [$library.key | package_name]))) as $reverse +| $libraries | to_entries[] +| select(.value.type == "package") +| select(.key | package_name | watched) +| select(.key | major >= 11) +| (.key | package_name) as $package +| (.key | package_version) as $version +| (.key | major) as $major +| select(($allowed | index(($package | ascii_downcase) + "@" + ($major | tostring))) | not) +| first_direct($reverse; $direct; $package) as $top +| if $top == null then "\($package) \($version) (via a project reference)" + elif ($top | ascii_downcase) == ($package | ascii_downcase) then "\($package) \($version) (direct dependency)" + else "\($package) \($version) (via \($top))" + end diff --git a/test/guard-net10-package-majors_test.sh b/test/guard-net10-package-majors_test.sh new file mode 100755 index 0000000..6521f13 --- /dev/null +++ b/test/guard-net10-package-majors_test.sh @@ -0,0 +1,345 @@ +#!/usr/bin/env bash +# Copyright (c) 2026 Peaceful Studio OÜ +# SPDX-License-Identifier: Apache-2.0 +set -euo pipefail + +here="$(cd "$(dirname "$0")" && pwd)" +script="$here/../scripts/guard-net10-package-majors.sh" + +fail=0 +check() { + local name="$1" expected="$2" actual="$3" + if [ "$expected" = "$actual" ]; then + echo "ok - $name" + else + echo "FAIL - $name: expected [$expected] got [$actual]" + fail=1 + fi +} + +check_contains() { + local name="$1" needle="$2" hay="$3" + case "$hay" in + *"$needle"*) echo "ok - $name" ;; + *) + echo "FAIL - $name: output lacks [$needle]" + printf '%s\n' "$hay" | sed 's/^/ | /' + fail=1 + ;; + esac +} + +work="$(mktemp -d)" +trap 'rm -rf "$work"' EXIT + +real_jq="$(command -v jq)" +real_dotnet="$(command -v dotnet)" +crlf_bin="$work/crlf-bin" +mkdir -p "$crlf_bin" +for tool in jq dotnet; do + real="$real_jq" + [ "$tool" = dotnet ] && real="$real_dotnet" + cat > "$crlf_bin/$tool" < "\$out" || rc=\$? +awk '{ printf "%s\r\n", \$0 }' "\$out" +rm -f "\$out" +exit "\$rc" +SHIM + chmod +x "$crlf_bin/$tool" +done + +new_repo() { + mktemp -d "$work/repo.XXXXXX" +} + +add_project() { + local repo="$1" name="$2" frameworks="$3" packable="$4" assets="$5" + mkdir -p "$repo/$name/obj" + cat > "$repo/$name/$name.csproj" < + + $frameworks + $packable + + +XML + if [ -n "$assets" ]; then + printf '%s\n' "$assets" > "$repo/$name/obj/project.assets.json" + fi +} + +run_guard() { + local repo="$1" exceptions="${2:-}" path_prefix="${3:-}" errf + errf="$(mktemp)" + set +e + out="$(cd "$repo" && DOTNET_NOLOGO=true NET10_PACKAGE_EXCEPTIONS="$exceptions" PATH="${path_prefix:+$path_prefix:}$PATH" bash "$script" 2>"$errf")" + rc=$? + set -e + out="$out$(cat "$errf")" + rm -f "$errf" +} + +SERILOG_ON_EXTENSIONS_11='{ + "version": 3, + "targets": { + "net10.0": { + "Serilog.AspNetCore/10.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "11.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/11.0.0": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Serilog.AspNetCore >= 10.0.0" ] } +}' + +SERILOG_ON_EXTENSIONS_10='{ + "version": 3, + "targets": { + "net10.0": { + "Serilog.AspNetCore/10.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "10.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/10.0.7": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Serilog.AspNetCore >= 10.0.0" ] } +}' + +EXTENSIONS_11_ONLY_ON_NET11='{ + "version": 3, + "targets": { + "net10.0": { + "Serilog.AspNetCore/10.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "10.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/10.0.7": { "type": "package" } + }, + "net11.0": { + "Serilog.AspNetCore/11.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "11.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/11.0.0": { "type": "package" } + } + }, + "projectFileDependencyGroups": { + "net10.0": [ "Serilog.AspNetCore >= 10.0.0" ], + "net11.0": [ "Serilog.AspNetCore >= 11.0.0" ] + } +}' + +EXTENSIONS_11_ONLY_ON_RID_TARGET='{ + "version": 3, + "targets": { + "net10.0": { + "Serilog.AspNetCore/10.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "10.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/10.0.7": { "type": "package" } + }, + "net10.0/linux-x64": { + "Serilog.AspNetCore/10.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensions.Logging.Abstractions": "11.0.0" } + }, + "Microsoft.Extensions.Logging.Abstractions/11.0.0": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Serilog.AspNetCore >= 10.0.0" ] } +}' + +ASPNETCORE_PRERELEASE_DIRECT='{ + "version": 3, + "targets": { + "net10.0": { + "Microsoft.AspNetCore.OpenApi/11.0.0-rc.2.26459.1": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Microsoft.AspNetCore.OpenApi >= 11.0.0-rc.2.26459.1" ] } +}' + +EF_CORE_11_ONLY='{ + "version": 3, + "targets": { + "net10.0": { + "Microsoft.EntityFrameworkCore/11.0.0": { + "type": "package", + "dependencies": { "Microsoft.Extensionsish.Thing": "11.0.0" } + }, + "Microsoft.Extensionsish.Thing/11.0.0": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Microsoft.EntityFrameworkCore >= 11.0.0" ] } +}' + +TWO_PATHS_TO_EXTENSIONS_11='{ + "version": 3, + "targets": { + "net10.0": { + "Library.A/1.0.0": { + "type": "package", + "dependencies": { "Middle.Package": "1.0.0" } + }, + "Middle.Package/1.0.0": { + "type": "package", + "dependencies": { + "Microsoft.Extensions.Options": "11.0.0", + "Microsoft.AspNetCore.Http.Abstractions": "11.0.0" + } + }, + "Microsoft.Extensions.Options/11.0.0": { "type": "package" }, + "Microsoft.AspNetCore.Http.Abstractions/11.0.0": { "type": "package" } + } + }, + "projectFileDependencyGroups": { "net10.0": [ "Library.A >= 1.0.0" ] } +}' + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "packable net10.0 target on Microsoft.Extensions 11 exits 1" 1 "$rc" +check_contains "failure names the project" "Lib/Lib.csproj" "$out" +check_contains "failure names the package and version" "Microsoft.Extensions.Logging.Abstractions 11.0.0" "$out" +check_contains "failure names the top-level package" "via Serilog.AspNetCore" "$out" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_10" +run_guard "$repo" +check "packable net10.0 target on Microsoft.Extensions 10 exits 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$EXTENSIONS_11_ONLY_ON_NET11" +run_guard "$repo" +check "11.x only on the net11.0 target exits 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "$EXTENSIONS_11_ONLY_ON_RID_TARGET" +run_guard "$repo" +check "11.x on a net10.0/linux-x64 target exits 1" 1 "$rc" +check_contains "RID target offender is named" "Microsoft.Extensions.Logging.Abstractions 11.0.0 (via Serilog.AspNetCore)" "$out" + +repo="$(new_repo)" +add_project "$repo" App "net10.0" false "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "non-packable project exits 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net11.0" true "${SERILOG_ON_EXTENSIONS_11//net10.0/net11.0}" +run_guard "$repo" +check "packable net11.0-only project exits 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "$ASPNETCORE_PRERELEASE_DIRECT" +run_guard "$repo" +check "direct Microsoft.AspNetCore prerelease 11 exits 1" 1 "$rc" +check_contains "direct dependency is labelled direct" "Microsoft.AspNetCore.OpenApi 11.0.0-rc.2.26459.1 (direct dependency)" "$out" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "$EF_CORE_11_ONLY" +run_guard "$repo" +check "packages outside the two prefixes at 11 exit 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "$TWO_PATHS_TO_EXTENSIONS_11" +run_guard "$repo" +check "two offenders of one project share one annotation" 1 "$(grep -c '^::error' <<< "$out")" +check_contains "second offender is in the same annotation" "Microsoft.AspNetCore.Http.Abstractions 11.0.0 (via Library.A)" "$out" +check_contains "transitive offender names its top-level package" "Microsoft.Extensions.Options 11.0.0 (via Library.A)" "$out" + +repo="$(new_repo)" +add_project "$repo" Lib "netstandard2.0; net10.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "TargetFrameworks with a space after the semicolon exits 1" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "netstandard2.0; + net10.0; + net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "TargetFrameworks spread over several lines exits 1" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib " NET10.0 " true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "net10.0 spelled in upper case with padding exits 1" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "" "$crlf_bin" +check "jq and dotnet emitting CRLF still exit 1 on an offender" 1 "$rc" +check_contains "CRLF output still names the package" "Microsoft.Extensions.Logging.Abstractions 11.0.0 (via Serilog.AspNetCore)" "$out" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_10" +run_guard "$repo" "" "$crlf_bin" +check "jq and dotnet emitting CRLF exit 0 on a clean project" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Logging.Abstractions@11" "$crlf_bin" +check "CRLF output does not break the exception match" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Logging.Abstractions@11" +check "exception matching package and major exits 0" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "microsoft.extensions.logging.abstractions@11" +check "exception matches the package id case-insensitively" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Logging.Abstractions@12" +check "exception for another major still exits 1" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Options@11" +check "exception for another package still exits 1" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$TWO_PATHS_TO_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Options@11,Microsoft.AspNetCore.Http.Abstractions@11" +check "comma separated exceptions cover both offenders" 0 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$TWO_PATHS_TO_EXTENSIONS_11" +run_guard "$repo" "Microsoft.Extensions.Options@11 + Microsoft.AspNetCore.Http.Abstractions@11" +check "newline separated exceptions cover both offenders" 0 "$rc" + +for malformed in \ + "Microsoft.Extensions.Logging.Abstractions" \ + "Microsoft.Extensions.Logging.Abstractions@" \ + "Microsoft.Extensions.Logging.Abstractions@11.0" \ + "Microsoft.Extensions.Logging.Abstractions@x" \ + "\$(touch\${IFS}pwned)@11" \ + "\";exit\${IFS}0;\"@11"; do + repo="$(new_repo)" + add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" + run_guard "$repo" "$malformed" + check "malformed exception [$malformed] exits 2" 2 "$rc" + check_contains "malformed exception [$malformed] is named" "$malformed" "$out" + check "malformed exception [$malformed] runs nothing" "no" "$([ -e "$repo/pwned" ] && echo yes || echo no)" +done + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "" +run_guard "$repo" +check "packable net10.0 project outside the restored workspace exits 0" 0 "$rc" +check_contains "unrestored project is named in a warning" "::warning::Lib/Lib.csproj" "$out" + +repo="$(new_repo)" +run_guard "$repo" +check "repository without projects exits 0" 0 "$rc" + +exit "$fail" From 68d6c100bc7aad58a2cf079b22db3e03c9e4b408 Mon Sep 17 00:00:00 2001 From: monsieurleberre Date: Sat, 21 Nov 2026 07:43:01 -0300 Subject: [PATCH 2/3] chore(csharp-ci): add the SPDX header to the guard filter --- scripts/net10-package-majors.jq | 3 +++ 1 file changed, 3 insertions(+) diff --git a/scripts/net10-package-majors.jq b/scripts/net10-package-majors.jq index 032b0d8..cae4242 100644 --- a/scripts/net10-package-majors.jq +++ b/scripts/net10-package-majors.jq @@ -1,3 +1,6 @@ +# Copyright (c) 2026 Peaceful Studio OÜ +# SPDX-License-Identifier: Apache-2.0 + def package_name: split("/")[0]; def package_version: split("/")[1]; def watched: test("^(microsoft\\.extensions|microsoft\\.aspnetcore)\\."; "i"); From 16c31278fcc3bfe8a334c016e0b0de3190a152d6 Mon Sep 17 00:00:00 2001 From: monsieurleberre Date: Sat, 21 Nov 2026 09:16:28 -0300 Subject: [PATCH 3/3] fix(csharp-ci): evaluate guard packability in Release, ignore IsPackable casing, report the checked count --- CHANGELOG.md | 2 +- scripts/guard-net10-package-majors.sh | 7 ++-- test/guard-net10-package-majors_test.sh | 46 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 4db2395..dfa4392 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,7 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed -- `csharp-ci.yaml` fails every `build-and-test` shard, in a new `Guard net10.0 package majors` step between `Restore` and `Build`, when a packable project's `net10.0` target resolves a `Microsoft.Extensions.*` or `Microsoft.AspNetCore.*` package at major 11 or later, direct or transitive. A library that ships a `net10.0` asset would otherwise force its consumers onto the 11.x line, and CI stayed green because `net10.0` builds and tests fine with 11.x packages. The step reads the resolved graph from each project's `obj/project.assets.json`, `net10.0/` targets included, so a minor or patch bump of any package that pulls in 11.x is caught, not only a bump of a named package. It checks only projects that MSBuild evaluates as packable with a `net10.0` target framework (`TargetFrameworks` may use spaces, newlines or any letter case): test projects, non-packable apps and `net11.0`-only projects are untouched, and the `net11.0` target of a multi-targeted library is not read. One `::error::` annotation per project names the project, each package, its resolved version and the top-level package that pulled it in. A packable `net10.0` project with no restore assets (one outside the restored solution) gets a warning and is skipped. New optional input `net10-package-exceptions` (default empty) takes comma or whitespace separated `package@major` pairs to accept; it reaches the step only through the environment and a malformed entry fails the step before anything is checked. The check is on by default with no caller change, so a caller whose `net10.0` target resolves 11.x goes red until it pins the package to 10.x or lists an exception. The step needs `jq`, which the GitHub-hosted images and the self-hosted Hetzner runners provide, and it strips the carriage returns `jq` and `dotnet` print under Git Bash on Windows so that a Windows shard checks the same projects as a Linux one. The helper checkout (`Checkout CI helpers`) now runs on every shard, not only the coverage shard. `scripts/guard-net10-package-majors.sh` carries the logic, and `test/guard-net10-package-majors_test.sh` runs it against fixture projects on Linux, macOS, Windows and Windows ARM in `build-and-test.yaml`. +- `csharp-ci.yaml` fails every `build-and-test` shard, in a new `Guard net10.0 package majors` step between `Restore` and `Build`, when a packable project's `net10.0` target resolves a `Microsoft.Extensions.*` or `Microsoft.AspNetCore.*` package at major 11 or later, direct or transitive. A library that ships a `net10.0` asset would otherwise force its consumers onto the 11.x line, and CI stayed green because `net10.0` builds and tests fine with 11.x packages. The step reads the resolved graph from each project's `obj/project.assets.json`, `net10.0/` targets included, so a minor or patch bump of any package that pulls in 11.x is caught, not only a bump of a named package. It checks only projects that MSBuild evaluates as packable in the `Release` configuration (the one `Build` and `Pack` use, `IsPackable` in any letter case) with a `net10.0` target framework (`TargetFrameworks` may use spaces, newlines or any letter case): test projects, non-packable apps and `net11.0`-only projects are untouched, and the `net11.0` target of a multi-targeted library is not read. One `::error::` annotation per project names the project, each package, its resolved version and the top-level package that pulled it in. A packable `net10.0` project with no restore assets (one outside the restored solution) gets a warning and is skipped. A passing step prints `net10.0 package guard: checked N packable project(s)` so a green run shows how many projects it covered. The scan covers the descendants of `working-directory`. New optional input `net10-package-exceptions` (default empty) takes comma or whitespace separated `package@major` pairs to accept; it reaches the step only through the environment and a malformed entry fails the step before anything is checked. The check is on by default with no caller change, so a caller whose `net10.0` target resolves 11.x goes red until it pins the package to 10.x or lists an exception. The step needs `jq`, which the GitHub-hosted images and the self-hosted Hetzner runners provide, and it strips the carriage returns `jq` and `dotnet` print under Git Bash on Windows so that a Windows shard checks the same projects as a Linux one. The helper checkout (`Checkout CI helpers`) now runs on every shard, not only the coverage shard. `scripts/guard-net10-package-majors.sh` carries the logic, and `test/guard-net10-package-majors_test.sh` runs it against fixture projects on Linux, macOS, Windows and Windows ARM in `build-and-test.yaml`. ## [2.8.1] - 2026-11-06 diff --git a/scripts/guard-net10-package-majors.sh b/scripts/guard-net10-package-majors.sh index 2638986..22f8627 100755 --- a/scripts/guard-net10-package-majors.sh +++ b/scripts/guard-net10-package-majors.sh @@ -27,11 +27,12 @@ fi allowed="$(printf '%s\n' "$entries" | jq_clean -R -s 'split("\n") | map(select(length > 0) | ascii_downcase)')" failed=0 +checked=0 while IFS= read -r project; do [ -n "$project" ] || continue display="${project#./}" - properties="$(dotnet msbuild "$project" -nologo -getProperty:IsPackable -getProperty:TargetFramework -getProperty:TargetFrameworks -getProperty:ProjectAssetsFile -getProperty:MSBuildProjectExtensionsPath | strip_cr)" - packable="$(jq_clean -r '.Properties.IsPackable' <<< "$properties")" + properties="$(dotnet msbuild "$project" -nologo -property:Configuration=Release -getProperty:IsPackable -getProperty:TargetFramework -getProperty:TargetFrameworks -getProperty:ProjectAssetsFile -getProperty:MSBuildProjectExtensionsPath | strip_cr)" + packable="$(jq_clean -r '.Properties.IsPackable | ascii_downcase' <<< "$properties")" frameworks=";$(jq_clean -r '.Properties.TargetFrameworks + ";" + .Properties.TargetFramework' <<< "$properties" | tr -d '[:space:]');" assets="$(jq_clean -r '.Properties.ProjectAssetsFile' <<< "$properties")" if [ -z "$assets" ]; then @@ -47,10 +48,12 @@ while IFS= read -r project; do echo "::warning::$display is packable for net10.0 but has no restore assets at $assets, so its package versions were not checked" continue fi + checked=$((checked + 1)) findings="$(jq_clean -r --argjson allowed "$allowed" "$filter" < "$assets" | sort -u | paste -sd ';' - | sed 's/;/; /g')" if [ -n "$findings" ]; then echo "::error::$display resolves $findings for its net10.0 target. A net10.0 package must not require Microsoft.Extensions or Microsoft.AspNetCore 11 or later; pin it to 10.x, or accept it with the net10-package-exceptions input." failed=1 fi done < <(find . \( -name obj -o -name bin -o -name node_modules -o -name .git -o -name .github-actions-helpers \) -prune -o \( -name '*.csproj' -o -name '*.fsproj' -o -name '*.vbproj' \) -print | sort) +echo "net10.0 package guard: checked $checked packable project(s)" exit "$failed" diff --git a/test/guard-net10-package-majors_test.sh b/test/guard-net10-package-majors_test.sh index 6521f13..b72ec68 100755 --- a/test/guard-net10-package-majors_test.sh +++ b/test/guard-net10-package-majors_test.sh @@ -71,6 +71,21 @@ XML fi } +add_release_only_packable_project() { + local repo="$1" name="$2" assets="$3" + mkdir -p "$repo/$name/obj" + cat > "$repo/$name/$name.csproj" < + + net10.0 + false + true + + +XML + printf '%s\n' "$assets" > "$repo/$name/obj/project.assets.json" +} + run_guard() { local repo="$1" exceptions="${2:-}" path_prefix="${3:-}" errf errf="$(mktemp)" @@ -270,6 +285,37 @@ add_project "$repo" Lib " NET10.0 " true "$SERILOG_ON_EXTENSIONS_11" run_guard "$repo" check "net10.0 spelled in upper case with padding exits 1" 1 "$rc" +for spelling in True TRUE tRuE; do + repo="$(new_repo)" + add_project "$repo" Lib "net10.0;net11.0" "$spelling" "$SERILOG_ON_EXTENSIONS_11" + run_guard "$repo" + check "IsPackable spelled $spelling exits 1 on an offender" 1 "$rc" +done + +repo="$(new_repo)" +add_release_only_packable_project "$repo" Lib "$SERILOG_ON_EXTENSIONS_11" +run_guard "$repo" +check "project packable only in Release exits 1 on an offender" 1 "$rc" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_10" +add_project "$repo" Other "net10.0" true "$SERILOG_ON_EXTENSIONS_10" +add_project "$repo" App "net10.0" false "$SERILOG_ON_EXTENSIONS_10" +add_project "$repo" Legacy "netstandard2.0" true "$SERILOG_ON_EXTENSIONS_10" +run_guard "$repo" +check "clean run exits 0" 0 "$rc" +check_contains "clean run reports how many packable net10.0 projects it checked" "net10.0 package guard: checked 2 packable project(s)" "$out" + +repo="$(new_repo)" +add_project "$repo" App "net10.0" false "$SERILOG_ON_EXTENSIONS_10" +run_guard "$repo" +check_contains "run with no packable net10.0 project reports zero" "net10.0 package guard: checked 0 packable project(s)" "$out" + +repo="$(new_repo)" +add_project "$repo" Lib "net10.0" true "" +run_guard "$repo" +check_contains "unrestored project is not counted as checked" "net10.0 package guard: checked 0 packable project(s)" "$out" + repo="$(new_repo)" add_project "$repo" Lib "net10.0;net11.0" true "$SERILOG_ON_EXTENSIONS_11" run_guard "$repo" "" "$crlf_bin"