[swift5] fix: make the urlsession client compile on Linux - #24872
[swift5] fix: make the urlsession client compile on Linux#24872wiebren wants to merge 2 commits into
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
|
The |
|
Hi @wiebren could you please also fix this in the swift 6 generator? Thanks |
Same fix as the swift5 commit: '#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 the other swift6 support files already carry, since URLSession and URLRequest live in FoundationNetworking on Linux. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WB3Hj4oCArRbHosPJrrRvA
|
@4brunu done - the swift6 urlsession template gets the same two guards (canImport(MobileCoreServices) around the import and the UTType fallback, plus the canImport(FoundationNetworking) import), and the swift6 samples are regenerated; the swift6 urlsessionLibrary sample builds clean with Swift 6.3.3. One note: the swift5 half of this PR also adds the urlsessionLibrary sample to the ubuntu-latest job of samples-swift5.yaml so the fix stays covered, but there is no GitHub Actions workflow building swift6 samples at all (swift6 is only exercised on Bitrise). If you'd like an equivalent samples-swift6.yaml I'm happy to add one, here or as a follow-up. |
Yes please |
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 and swift6 urlsession clients so they compile 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 #endifto both templates, 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 68d3a60. Summary will update on new commits.