Skip to content

Map SWIFT_VERSION to a valid -swift-version language mode - #44

Merged
k-kohey merged 1 commit into
k-kohey:mainfrom
brandan-prnd:fix/swift-version-map
Oct 15, 2026
Merged

k-kohey merged 1 commit into
k-kohey:mainfrom
brandan-prnd:fix/swift-version-map

Conversation

@brandan-prnd

Copy link
Copy Markdown
Contributor

Summary

-swift-version only accepts Swift language modes (4, 4.2, 5, 6), but SWIFT_VERSION in an Xcode project is an arbitrary x.y build setting (6.0, 5.9, 4.0, …). Xcode normalizes it before calling swiftc; axe only trimmed a trailing .0, so any other value was passed through verbatim and the preview thunk failed to compile.

This PR maps any x.y to its major version (keeping 4.2, which is a real language mode) and drops the special case that preserved 4.0.

Problem

Projects that set SWIFT_VERSION = 5.9 in an xcconfig cannot capture any preview:

invalid value '5.9' in '-swift-version 5.9'
note: valid arguments to '-swift-version' are '4', '4.2', '5', '6'

The previous normalization was:

if strings.HasSuffix(sv, ".0") && sv != "4.0" {
    sv = strings.TrimSuffix(sv, ".0")
}

It handles 6.0 → 6 and 5.0 → 5, but 5.9 (or 5.10, 6.1, …) is left untouched. The 4.0 exception also turns out to be wrong: swiftc rejects 4.0 exactly like 5.9.

Fix

// SWIFT_VERSION may be "6.0" or "5.9" but -swift-version only accepts "4", "4.2", "5", "6".
if major, _, ok := strings.Cut(sv, "."); ok && sv != "4.2" {
    sv = major
}
  • Any x.y becomes x.

  • 4.2 is kept as-is because it is a distinct language mode.

  • The literal 4.0 is no longer preserved. Verified with Swift 6.3.3:

    -swift-version 4    OK
    -swift-version 4.0  error: invalid value '4.0' in '-swift-version 4.0'
    -swift-version 4.2  OK
    

Verification

  • TestCompileThunk_SwiftVersionTrimming now covers 5.9 → 5 and 4.0 → 4 alongside the existing cases.
  • Verified end to end on a project with SWIFT_VERSION=5.9: the thunk is compiled with -swift-version 5 and the capture succeeds.

swiftc only accepts language modes (4, 4.2, 5, 6) for -swift-version, but
SWIFT_VERSION in Xcode projects is an arbitrary x.y value (6.0, 5.9, 4.0, ...).
Xcode normalizes it before invoking swiftc; axe only trimmed a trailing .0,
so anything else was passed through and the preview thunk failed to
compile with e.g.:

  invalid value '5.9' in '-swift-version 5.9'

Map any x.y to its major. 4.2 is a distinct language mode and is kept
as-is. The previous special case that kept "4.0" literal is dropped: swiftc
rejects it the same way (verified with Swift 6.3.3):

  -swift-version 4    OK
  -swift-version 4.0  error: invalid value '4.0' in '-swift-version 4.0'
  -swift-version 4.2  OK

@k-kohey k-kohey left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for your contribution!

@k-kohey
k-kohey merged commit 22aa049 into k-kohey:main Oct 15, 2026
2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants