That should still be fine if A in the example above doesn’t itself re-export Foundation, right? Or does the exception apply through the whole tree?
In isolation, I think that case would be fine.
In practice, for a project that has many more imports, it might not be as straightforward. Clang modules are notoriously leaky as well, at least in Apple's SDKs, which all use export * to avoid breaking clients when they migrate to modules if they rely on C's "importing a header means you can see everything it imports, transitively" textual inclusion behavior. So importing some other Clang module in that file, if it depends on Foundation, might cause the same issue to reemerge.
Of course, this may not be the case on non-Apple platforms where Foundation has a pure Swift implementation (ignoring CoreFoundation, of course), and that seems to be the focus of this discussion. I would hope that Apple platforms would also figure out how to represent FoundationEssentials/Internationalization as well so that we can truly write cross-platform packages without conditional compilation, but I realize there are decades worth of ABI and source compatibility concerns at play there, which are also entirely out of scope for the Swift open-source project.
Is anyone actually working on getting the changes merged? I've seen no activity in the repos that are blocking my projects from removing Foundation dependency:
As for InternalImportsByDefault, I think it should've been something that's defined in Package.swift (and through swiftc parameters when using cmake/etc.), so there is a single source of truth for which dependencies are allowed to be used in public APIs.
I am slowly working through some. Did PRs for protobuf: Move runtime to `FoundationEssentials` by madsodgaard · Pull Request #1993 · apple/swift-protobuf · GitHub and looking at openapi-runtime.
The changes should be quite simple if the package does not use anything from Foundation. So I am sure the maintainers would appreciate a PR ![]()
I mean the ESG update was literarily yesterday... Yes, SSWG projects will move to adopt this pattern.
It might make sense to add a CI step to validate that the produced products don't "accidentally" link Foundation. I think we did something like this in the AWS Lambda runtime. CC @sebsto
Indeed, I wrote a script that runs in the CI to check the absence of foundation-related libraries. The scripts builds a Lambda function and checks that the binary is not linked with libFoundation.so, libFoundationInternationalization.so, or lib_FoundationICU.so
The scrip is here
Happy to read your feedback
Is there a reason why straightforward PRs get ignored for weeks/months?
The swift-crypto PR is still open (although the person should just be blocked and the PR closed, so someone who doesn't do license laundering can create a new PR)
Then these are also already 2 weeks old:
Yesterday I went through my package-in-progress and Essentials-ized as many imports as possible. Of the source files that actually needed Foundation, in a few cases it was due to Unicode support, which I can probably do without, but more often due to the use of FileManager or Input/OutputStream.
It seems sad that I can’t use standard filesystem APIs without dragging in all of ICU too.
Is there any alternative? I know about FileDescriptor (sp?) but that would require writing a higher-level streaming API around it. It might also expose platform-specific behaviors. And as for a DIY FileManager, I know from experience that building a robust cross-platform API for manipulating the filesystem is nontrivial.
Can you try NIOFileSystem?
Or GitHub - swiftlang/swift-subprocess: Subprocess is a cross-platform package for spawning processes in Swift. · GitHub for Process
While measuring Android app sizes I kept running into the "which dependency still pulls in Foundation?" question, so I wrote a small tool for it: why-foundation.
It loads the resolved package graph, evaluates #if conditions, package traits and swiftSettings defines for Android or Linux, and lists every target that still pulls in Foundation, with the dependency path that leads there:
swift-protobuf › SwiftProtobuf
path: Shared → SwiftProtobuf (swift-protobuf)
resources in Package.swift (SwiftPM's generated resource accessor imports Foundation)
Sources/SwiftProtobuf/BinaryDelimited.swift:16 import Foundation
It also sorts the findings into targets that look easy to switch to FoundationEssentials, targets that use APIs only available in full Foundation (URLSession, NSLock, legacy formatters), and Apple-only targets. It works on packages and on the package dependencies of Xcode projects. I checked its verdicts against llvm-readelf on real Android builds.
It complements the CI linkage check @sebsto mentioned above: that one tells you Foundation got linked, this one tells you why.
One thing it surfaced: packages that declare PrivacyInfo.xcprivacy under #if canImport(Darwin) still link Foundation when you cross-compile for Android from a Mac, because the manifest is evaluated on the host. swift-crypto is one example. I opened swiftlang/swift-package-manager#10694, which limits privacy manifests to Apple platforms in Swift Build, so this gets fixed without changes to the packages.
Feedback welcome, especially false positives.
Update: it's now also a GitHub Action, so the CI check from the guidance above is one step:
- uses: niktishakov/why-foundation@v0
with:
product: MyLibrary
It downloads a prebuilt binary, so it takes seconds on Linux and macOS runners.
@niktishakov Isn't it fixed already in Swift 6.4.1? [6.4.1] Ignore undeclared PrivacyInfo.xcprivacy when not targeting Apple platforms by rconnell9 · Pull Request #10580 · swiftlang/swift-package-manager · GitHub
edit: nevermind, that's for when it's not declared in Package.swift
Right, swiftlang/swift-package-manager#10580 covers privacy manifests that aren’t declared in Package.swift. Packages like swift-crypto declare it under #if canImport(Darwin), which is still true when you cross-compile from a Mac, so they keep linking Foundation. swiftlang/swift-package-manager#10694 is the follow-up that was discussed in the review of swiftlang/swift-package-manager#10575.