From 38000794f29a24e11a6f5ea41ad28facb7a255d1 Mon Sep 17 00:00:00 2001 From: Benedek Kozma Date: Tue, 21 Apr 2026 19:58:43 +0200 Subject: [PATCH] Split FoundationEssentials extensions from NIOFoundationCompat (#3567) Split FoundationEssentials extensions from NIOFoundationCompat ### Motivation: Resolves #3038 ### Modifications: Split NIOFoundationCompat into two and only import FoundationEssentials and NIOFoundationEssentialsCompat where appropriate. ### Result: ByteBuffer/Codable extensions can be used without vendoring the code or linking to Foundation. Co-authored-by: Johannes Weiss Co-authored-by: Fabian Fett --- Package.swift | 14 +++++++++++--- .../NIOFSFoundationCompat/Data+FileSystem.swift | 6 +++++- Sources/NIOFSFoundationCompat/Date+FileInfo.swift | 4 ++++ Sources/NIOFoundationCompat/Exports.swift | 15 +++++++++++++++ .../ByteBuffer-foundation.swift | 7 ++++++- .../Codable+ByteBuffer.swift | 7 ++++++- .../Data+FileSystem.swift | 6 +++++- .../Date+FileInfo.swift | 4 ++++ 8 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 Sources/NIOFoundationCompat/Exports.swift rename Sources/{NIOFoundationCompat => NIOFoundationEssentialsCompat}/ByteBuffer-foundation.swift (99%) rename Sources/{NIOFoundationCompat => NIOFoundationEssentialsCompat}/Codable+ByteBuffer.swift (98%) diff --git a/Package.swift b/Package.swift index 28c3bf6be..04d7ef870 100644 --- a/Package.swift +++ b/Package.swift @@ -51,6 +51,7 @@ let package = Package( .library(name: "NIOTLS", targets: ["NIOTLS"]), .library(name: "NIOHTTP1", targets: ["NIOHTTP1"]), .library(name: "NIOConcurrencyHelpers", targets: ["NIOConcurrencyHelpers"]), + .library(name: "NIOFoundationEssentialsCompat", targets: ["NIOFoundationEssentialsCompat"]), .library(name: "NIOFoundationCompat", targets: ["NIOFoundationCompat"]), .library(name: "NIOWebSocket", targets: ["NIOWebSocket"]), .library(name: "NIOTestUtils", targets: ["NIOTestUtils"]), @@ -130,13 +131,20 @@ let package = Package( swiftSettings: swiftSettings ), .target( - name: "NIOFoundationCompat", + name: "NIOFoundationEssentialsCompat", dependencies: [ .target(name: "NIO", condition: .when(platforms: historicalNIOPosixDependencyRequired)), "NIOCore", ], swiftSettings: swiftSettings ), + .target( + name: "NIOFoundationCompat", + dependencies: [ + "NIOFoundationEssentialsCompat" + ], + swiftSettings: swiftSettings + ), .target( name: "CNIOAtomics", dependencies: [], @@ -259,7 +267,7 @@ let package = Package( name: "NIOFSFoundationCompat", dependencies: [ "NIOFS", - "NIOFoundationCompat", + "NIOFoundationEssentialsCompat", ], path: "Sources/NIOFSFoundationCompat", swiftSettings: swiftSettings @@ -287,7 +295,7 @@ let package = Package( name: "_NIOFileSystemFoundationCompat", dependencies: [ "_NIOFileSystem", - "NIOFoundationCompat", + "NIOFoundationEssentialsCompat", ], path: "Sources/_NIOFileSystemFoundationCompat", swiftSettings: swiftSettings diff --git a/Sources/NIOFSFoundationCompat/Data+FileSystem.swift b/Sources/NIOFSFoundationCompat/Data+FileSystem.swift index 2ddcddbce..ab71a05bf 100644 --- a/Sources/NIOFSFoundationCompat/Data+FileSystem.swift +++ b/Sources/NIOFSFoundationCompat/Data+FileSystem.swift @@ -15,8 +15,12 @@ #if canImport(Darwin) || os(Linux) || os(Android) import NIOFS import NIOCore -import NIOFoundationCompat +import NIOFoundationEssentialsCompat +#if canImport(FoundationEssentials) +import struct FoundationEssentials.Data +#else import struct Foundation.Data +#endif @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) extension Data { diff --git a/Sources/NIOFSFoundationCompat/Date+FileInfo.swift b/Sources/NIOFSFoundationCompat/Date+FileInfo.swift index 5b4733cc2..1d07078ec 100644 --- a/Sources/NIOFSFoundationCompat/Date+FileInfo.swift +++ b/Sources/NIOFSFoundationCompat/Date+FileInfo.swift @@ -14,7 +14,11 @@ import NIOFS +#if canImport(FoundationEssentials) +import struct FoundationEssentials.Date +#else import struct Foundation.Date +#endif extension Date { public init(timespec: FileInfo.Timespec) { diff --git a/Sources/NIOFoundationCompat/Exports.swift b/Sources/NIOFoundationCompat/Exports.swift new file mode 100644 index 000000000..314a3d935 --- /dev/null +++ b/Sources/NIOFoundationCompat/Exports.swift @@ -0,0 +1,15 @@ +//===----------------------------------------------------------------------===// +// +// This source file is part of the SwiftNIO open source project +// +// Copyright (c) 2026 Apple Inc. and the SwiftNIO project authors +// Licensed under Apache License v2.0 +// +// See LICENSE.txt for license information +// See CONTRIBUTORS.txt for the list of SwiftNIO project authors +// +// SPDX-License-Identifier: Apache-2.0 +// +//===----------------------------------------------------------------------===// + +@_exported import NIOFoundationEssentialsCompat diff --git a/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift b/Sources/NIOFoundationEssentialsCompat/ByteBuffer-foundation.swift similarity index 99% rename from Sources/NIOFoundationCompat/ByteBuffer-foundation.swift rename to Sources/NIOFoundationEssentialsCompat/ByteBuffer-foundation.swift index bdc90cd12..4fbc116d5 100644 --- a/Sources/NIOFoundationCompat/ByteBuffer-foundation.swift +++ b/Sources/NIOFoundationEssentialsCompat/ByteBuffer-foundation.swift @@ -12,9 +12,14 @@ // //===----------------------------------------------------------------------===// -import Foundation import NIOCore +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + /// Errors that may be thrown by ByteBuffer methods that call into Foundation. public enum ByteBufferFoundationError: Error { /// Attempting to encode the given string failed. diff --git a/Sources/NIOFoundationCompat/Codable+ByteBuffer.swift b/Sources/NIOFoundationEssentialsCompat/Codable+ByteBuffer.swift similarity index 98% rename from Sources/NIOFoundationCompat/Codable+ByteBuffer.swift rename to Sources/NIOFoundationEssentialsCompat/Codable+ByteBuffer.swift index 5e22a414f..e0cdcb9bd 100644 --- a/Sources/NIOFoundationCompat/Codable+ByteBuffer.swift +++ b/Sources/NIOFoundationEssentialsCompat/Codable+ByteBuffer.swift @@ -12,9 +12,14 @@ // //===----------------------------------------------------------------------===// -import Foundation import NIOCore +#if canImport(FoundationEssentials) +import FoundationEssentials +#else +import Foundation +#endif + extension ByteBuffer { /// Attempts to decode the `length` bytes from `index` using the `JSONDecoder` `decoder` as `T`. /// diff --git a/Sources/_NIOFileSystemFoundationCompat/Data+FileSystem.swift b/Sources/_NIOFileSystemFoundationCompat/Data+FileSystem.swift index 069a450a6..474a79c2d 100644 --- a/Sources/_NIOFileSystemFoundationCompat/Data+FileSystem.swift +++ b/Sources/_NIOFileSystemFoundationCompat/Data+FileSystem.swift @@ -15,8 +15,12 @@ #if canImport(Darwin) || os(Linux) || os(Android) import _NIOFileSystem import NIOCore -import NIOFoundationCompat +import NIOFoundationEssentialsCompat +#if canImport(FoundationEssentials) +import struct FoundationEssentials.Data +#else import struct Foundation.Data +#endif @available(macOS 10.15, iOS 13.0, watchOS 6.0, tvOS 13.0, *) extension Data { diff --git a/Sources/_NIOFileSystemFoundationCompat/Date+FileInfo.swift b/Sources/_NIOFileSystemFoundationCompat/Date+FileInfo.swift index 9acb95a35..678a28ecc 100644 --- a/Sources/_NIOFileSystemFoundationCompat/Date+FileInfo.swift +++ b/Sources/_NIOFileSystemFoundationCompat/Date+FileInfo.swift @@ -14,7 +14,11 @@ import _NIOFileSystem +#if canImport(FoundationEssentials) +import struct FoundationEssentials.Date +#else import struct Foundation.Date +#endif extension Date { public init(timespec: FileInfo.Timespec) {