[swift5] fix: make the urlsession client compile on Linux - #24872
Open
wiebren wants to merge 1 commit into
Open
Conversation
'#if !os(macOS)' is true on Linux and Windows, so URLSessionImplementations imported the Apple-only MobileCoreServices exactly where it does not exist. Guard the import and the UTType fallback in mimeType(for:) with canImport(MobileCoreServices), and add the canImport(FoundationNetworking) import that every other support file already carries, since URLSession and URLRequest live in FoundationNetworking on Linux. Build the urlsessionLibrary petstore sample on ubuntu-latest in CI so it stays fixed: the existing Linux job only builds alamofireLibrary, which never compiles this file. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01CxDNCjqJycKTfzVWg2SeTJ
Author
|
The |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The swift5 urlsession client does not compile on Linux:
and once past that,
The cause
URLSessionImplementations.mustacheguards the Apple-only module with!os(macOS)is true on Linux and Windows, so the import fires exactly where the moduledoes not exist. The guard came in with #15060 ("feat(swift5): allow to swift build in
linux"), which fixed the macOS build and the one Linux job CI runs — but that job builds
only the
alamofireLibrarysample, which never compilesURLSessionImplementations.swift,so the regression was invisible.
Behind the import, the same file uses
URLSession/URLRequestwithout the#if canImport(FoundationNetworking)import that every other support template(
APIs.mustache,Configuration.mustache,Models.mustache,Extensions.mustache,JSONDataEncoding.mustache) already carries — on Linux those types live inFoundationNetworking, not Foundation. And the pre-macOS-11 fallback in
mimeType(for:)calls
UTTypeCreatePreferredIdentifierForTagunconditionally;if #availableis a runtimecheck, so that branch is still compiled on Linux and fails even after the import is
fixed.
The fix
The standard guards, in both places:
#if canImport(FoundationNetworking) import FoundationNetworking #endif— same as theother support files;
#if canImport(MobileCoreServices)around the import and around theUTTypeCreatePreferredIdentifierForTagfallback inmimeType(for:). The function'searly returns are otherwise untouched; every path that previously returned
"application/octet-stream"now falls through to the single trailing return of the samevalue (keeping a
returninside the compiled-out branch would trip swiftc's"will never be executed" warning on Linux), so behavior on Apple platforms is unchanged:
UTType lookup on macOS 11+/iOS 14+, MobileCoreServices fallback on older OS versions.
To keep this fixed, the CI matrix now builds the
urlsessionLibrarypetstore sample onubuntu-latestnext to the existingalamofireLibraryentry.The swift6 generator's twin template has the identical
#if !os(macOS)guard and the samefix applies there; happy to extend this PR or follow up, whichever you prefer.
Tests / verification
./bin/generate-samples.sh ./bin/configs/swift5-*.yaml; thediff is confined to the generated
URLSessionImplementations.swiftfiles.swift buildin a
swift:6.1container) — on master the same build fails with the errors above. (Theonly diagnostics left are pre-existing Sendable warnings, e.g.
SessionDelegatevs.Linux FoundationNetworking's
URLSessionTaskDelegate: Sendable— unrelated to thischange.)
mimeType(for:)fallback still runs on Apple platforms older than the UTType APIs.Existing upstream issues: none found (searched
MobileCoreServices,swift linux,FoundationNetworking swift5).PR checklist
./bin/generate-samples.sh ./bin/configs/swift5-*.yaml; docs export unchanged).Generated with Claude Code
Summary by cubic
Fixes the swift5 urlsession client so it compiles on Linux. Previously the
MobileCoreServicesimport was guarded by#if !os(macOS)(true on Linux and Windows) andURLSession/URLRequestwere used without importingFoundationNetworking, which hosts those types on Linux.#if canImport(FoundationNetworking) import FoundationNetworking #endif, matching the other support templates.MobileCoreServicesimport and theUTTypeCreatePreferredIdentifierForTagfallback withcanImport(MobileCoreServices).mimeType(for:)to end with a single trailing return of"application/octet-stream", so Apple-platform behavior is unchanged.urlsessionLibrarypetstore sample onubuntu-latest; the previous Linux job only builtalamofireLibrary, which never compiles this file.Written for commit 9d023bc. Summary will update on new commits.