Skip to content

Replace import Foundation with FoundationEssentials#897

Open
madsodgaard wants to merge 14 commits intoswift-server:mainfrom
madsodgaard:foundation-essentials
Open

Replace import Foundation with FoundationEssentials#897
madsodgaard wants to merge 14 commits intoswift-server:mainfrom
madsodgaard:foundation-essentials

Conversation

@madsodgaard
Copy link
Contributor

@madsodgaard madsodgaard commented Mar 3, 2026

Replaces all the foundation imports.

One issue is that HTTPClient.init?(httpsURLWithSocketPath socketPath: String, uri: String = "/") uses addingPercentEncoding() from Foundation. So instead, we use a pure Swift impl. that does the same.

We also need to disable default traits from swift-configuration to prevent linking Foundation, because the JSON trait does that.

This also adds a linkage test to prevent regressions to CI.

@t089
Copy link
Contributor

t089 commented Mar 3, 2026

One option could be to vendor / copy the percent encoding from FoundationEssentials (too bad this is not public api)

https://github.com/swiftlang/swift-foundation/blob/479abe7e3a9df37c02a2a228d482f2d8978b3f91/Sources/FoundationEssentials/URL/URLEncoder.swift#L55

@madsodgaard
Copy link
Contributor Author

madsodgaard commented Mar 3, 2026

@t089 Yeah, that is similar to what I did for swift-openapi-runtime apple/swift-openapi-runtime#176

@t089
Copy link
Contributor

t089 commented Mar 3, 2026

Feels like we should create a swift-percent-encoding nano package ... if only there was a package that could host such foundational essentials ...

Package.swift Outdated
.product(name: "NIOHTTPCompression", package: "swift-nio-extras"),
.product(name: "NIOSOCKS", package: "swift-nio-extras"),
.product(name: "NIOTransportServices", package: "swift-nio-transport-services"),
.product(name: "NIOTransportServices", package: "swift-nio-transport-services", condition: .when(platforms: [.macOS, .iOS, .tvOS, .watchOS, .macCatalyst, .visionOS])),
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think this is a breaking change, weirdly. The transitive import is safe on all platforms: NIOTS should completely become empty on all non-Apple platforms. If it doesn't, we should fix that.

Copy link
Contributor Author

@madsodgaard madsodgaard Mar 3, 2026

Choose a reason for hiding this comment

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

The issue is that even though I have guarded the "import NIOTransportServices" behind #canImport(Network), I am still seeing symbols from Foundation being linked. Which is because NIOTransportServices depend on NIOFoundationCompat

root@53b151c5ecbb:/workspace/Tests/LinkageTest/.build/aarch64-unknown-linux-gnu/debug# nm -u linkageTest | swift demangle | grep "Foundation"
                 U static Foundation.JSONSerialization.jsonObject(with: FoundationEssentials.Data, options: Foundation.JSONSerialization.ReadingOptions) throws -> Any
                 U type metadata for Foundation.JSONSerialization.ReadingOptions
                 U protocol conformance descriptor for Foundation.JSONSerialization.ReadingOptions : Swift.SetAlgebra in Foundation
                 U type metadata accessor for Foundation.JSONSerialization
                 U Foundation.RunLoop.run(mode: Foundation.RunLoop.Mode, before: FoundationEssentials.Date) -> Swift.Bool
                 U Foundation.RunLoop.Mode.default.unsafeMutableAddressor : Foundation.RunLoop.Mode
                 U static Foundation.RunLoop.current.getter : Foundation.RunLoop
                 U type metadata accessor for Foundation.RunLoop

Copy link
Member

Choose a reason for hiding this comment

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

I think then we should fix NIOTS first, shouldn't we?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Is there any un-guarded import of NIOFoundationCompat there? If there is we should fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, there does not seem to be.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ugh gross. Ok, this is probably acceptable.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yeah, I don't quite understand why its leaking still...

@Lukasa Lukasa added the 🔨 semver/patch No public API change. label Mar 3, 2026
@madsodgaard
Copy link
Contributor Author

madsodgaard commented Mar 4, 2026

Unfortunately the URLComponents APIs did not behave as a I expected. So, I had to move away from them, and use a Swift-only impl of addingPercentEncoding, which we also did for swift-openapi-runtime.

@madsodgaard madsodgaard requested review from Lukasa and weissi March 4, 2026 10:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

🔨 semver/patch No public API change.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants