From 7a5b5427e060d506a01197466b290d6eae1f20a3 Mon Sep 17 00:00:00 2001 From: Claude Date: Thu, 12 Nov 2026 12:23:35 +0000 Subject: [PATCH] Read the Swift version from .swift-version when swift-version is empty Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_018MRASoiK7Suus62oHAcfcf --- README.md | 2 +- action.yml | 28 +++++++++++++++++++++------- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 8247750..04ae453 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ jobs: | Input | Required | Default | Description | | --- | --- | --- | --- | -| `swift-version` | yes | | Exact toolchain version, e.g. `6.4.1` or `6.3.3`. Ranges and `latest` are not supported. | +| `swift-version` | no | | Exact toolchain version, e.g. `6.4.1` or `6.3.3`. Ranges and `latest` are not supported. If empty, the version is read from `.swift-version` in the working directory (surrounding whitespace removed) and must also be an exact version. | | `swift-sdks` | no | | Cross-compilation Swift SDKs to install after the toolchain: `static-linux`, `wasm`, `android` (comma or space separated). | | `use-preinstalled` | no | `false` | **Unverified opt-in.** If `swift` on `PATH` already reports exactly `swift-version`, run and use it instead of installing. By default no pre-existing binary is ever executed. | | `windows-toolchain-variant` | no | `asserts` | Windows only. `asserts` keeps the installer's default `+Asserts` toolchain; `noasserts` installs the `+NoAsserts` one instead. Use `noasserts` on win-arm64, where the `+Asserts` `swift-package.exe` fails to start ([swiftlang/swift#92379](https://github.com/swiftlang/swift/issues/92379)). | diff --git a/action.yml b/action.yml index 143d775..bff28c0 100644 --- a/action.yml +++ b/action.yml @@ -7,8 +7,11 @@ branding: color: orange inputs: swift-version: - description: Exact toolchain version, e.g. "6.4.1". - required: true + description: >- + Exact toolchain version, e.g. "6.4.1". If empty, the version is read from .swift-version in the + working directory (surrounding whitespace removed); it must be an exact version too. + required: false + default: "" swift-sdks: description: >- Comma or space separated Swift SDKs to install after the toolchain: static-linux, wasm, android. @@ -67,10 +70,21 @@ runs: USE_PREINSTALLED: ${{ inputs.use-preinstalled }} run: | set -euo pipefail + if [ -z "$SWIFT_VERSION" ]; then + if [ ! -f .swift-version ]; then + echo "::error::swift-version is empty and no .swift-version file was found in $PWD" + exit 1 + fi + SWIFT_VERSION="$(< .swift-version)" + SWIFT_VERSION="${SWIFT_VERSION#"${SWIFT_VERSION%%[![:space:]]*}"}" + SWIFT_VERSION="${SWIFT_VERSION%"${SWIFT_VERSION##*[![:space:]]}"}" + echo "Read Swift version '$SWIFT_VERSION' from .swift-version" + fi if [[ ! "$SWIFT_VERSION" =~ ^[0-9]+\.[0-9]+(\.[0-9]+)?$ ]]; then echo "::error::swift-version must be an exact version like 6.4.1, got '$SWIFT_VERSION'" exit 1 fi + echo "swift-version=$SWIFT_VERSION" >> "$GITHUB_OUTPUT" # Pre-existing binaries are executed only on explicit opt-in. pattern="Swift version ${SWIFT_VERSION//./\\.}[ (]" if [ "$USE_PREINSTALLED" = true ] && command -v swift > /dev/null && swift --version 2>&1 | grep -Eq "$pattern"; then @@ -86,7 +100,7 @@ runs: if: steps.existing.outputs.found != 'true' && runner.os == 'Linux' shell: bash env: - SWIFT_VERSION: ${{ inputs.swift-version }} + SWIFT_VERSION: ${{ steps.existing.outputs.swift-version }} run: bash "$GITHUB_ACTION_PATH/plan-linux.sh" - name: Restore Swift toolchain cache @@ -112,7 +126,7 @@ runs: if: steps.existing.outputs.found != 'true' && runner.os == 'macOS' shell: bash env: - SWIFT_VERSION: ${{ inputs.swift-version }} + SWIFT_VERSION: ${{ steps.existing.outputs.swift-version }} run: bash "$GITHUB_ACTION_PATH/install-macos.sh" - name: Install Swift (Windows, Authenticode verified) @@ -120,7 +134,7 @@ runs: if: steps.existing.outputs.found != 'true' && runner.os == 'Windows' shell: pwsh env: - SWIFT_VERSION: ${{ inputs.swift-version }} + SWIFT_VERSION: ${{ steps.existing.outputs.swift-version }} WINDOWS_TOOLCHAIN_VARIANT: ${{ inputs.windows-toolchain-variant }} WINDOWS_MINIMAL: ${{ inputs.windows-minimal }} run: '& "$env:GITHUB_ACTION_PATH/install-windows.ps1"' @@ -129,7 +143,7 @@ runs: id: report shell: bash env: - SWIFT_VERSION: ${{ inputs.swift-version }} + SWIFT_VERSION: ${{ steps.existing.outputs.swift-version }} SOURCE: ${{ steps.linux.outputs.source || steps.macos.outputs.source || steps.windows.outputs.source || 'preinstalled' }} run: | set -euo pipefail @@ -148,6 +162,6 @@ runs: if: inputs.swift-sdks != '' shell: bash env: - SWIFT_VERSION: ${{ inputs.swift-version }} + SWIFT_VERSION: ${{ steps.existing.outputs.swift-version }} SWIFT_SDKS: ${{ inputs.swift-sdks }} run: bash "$GITHUB_ACTION_PATH/install-sdks.sh"