Skip to content

[6.0] Handle .xcprivacy files in TargetSourcesBuilder.swift #7816

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Sources/PackageLoading/TargetSourcesBuilder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,24 @@ public struct FileRuleDescription: Sendable {
)
}()

/// File rule to copy `.xcprivacy` (in the Xcode build system).
public static let xcprivacyCopied: FileRuleDescription = {
.init(
rule: .copyResource,
toolsVersion: .v6_0,
fileTypes: ["xcprivacy"]
)
}()

/// File rule to ignore `.xcprivacy` (in the SwiftPM build system).
public static let xcprivacyIgnored: FileRuleDescription = {
.init(
rule: .ignored,
toolsVersion: .v6_0,
fileTypes: ["xcprivacy"]
)
}()

/// List of all the builtin rules.
public static let builtinRules: [FileRuleDescription] = [
swift,
Expand All @@ -739,11 +757,13 @@ public struct FileRuleDescription: Sendable {
stringCatalog,
coredata,
metal,
xcprivacyCopied,
]

/// List of file types that apply just to the SwiftPM build system.
public static let swiftpmFileTypes: [FileRuleDescription] = [
docc,
xcprivacyIgnored,
]

/// List of file directory extensions that should be treated as opaque, non source, directories.
Expand Down
8 changes: 8 additions & 0 deletions Sources/SPMTestSupport/Observability.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import Basics
import func XCTest.XCTAssertTrue
import func XCTest.XCTAssertEqual
import func XCTest.XCTFail

Expand Down Expand Up @@ -167,6 +168,13 @@ public class DiagnosticsTestResult {
self.uncheckedDiagnostics = diagnostics
}

package func checkIsEmpty(
file: StaticString = #file,
line: UInt = #line
) {
XCTAssertTrue(self.uncheckedDiagnostics.isEmpty, file: file, line: line)
}

@discardableResult
public func check(
diagnostic message: StringPattern,
Expand Down
97 changes: 96 additions & 1 deletion Tests/PackageLoadingTests/PackageBuilderTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,27 @@ final class PackageBuilderTests: XCTestCase {
}
}

func testXCPrivacyIgnored() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Sources/foo/PrivacyInfo.xcprivacy",
"/Sources/foo/Foo.swift")

let manifest = Manifest.createRootManifest(
displayName: "pkg",
path: .root,
targets: [
try TargetDescription(name: "foo"),
]
)
PackageBuilderTester(manifest, in: fs) { package, _ in
package.checkModule("foo") { module in
module.check(c99name: "foo", type: .library)
module.checkSources(root: "/Sources/foo", paths: "Foo.swift")
module.checkResources(resources: [])
}
}
}

func testMixedSources() throws {
let foo: AbsolutePath = "/Sources/foo"

Expand Down Expand Up @@ -2951,7 +2972,81 @@ final class PackageBuilderTests: XCTestCase {
}
}
}


func testXcodeResources6_0AndLater() throws {
// In SwiftTools 6.0 and later, xcprivacy file types are only supported when explicitly passed via additionalFileRules.

let root: AbsolutePath = "/Foo"
let foo = root.appending(components: "Sources", "Foo")

let fs = InMemoryFileSystem(emptyFiles:
foo.appending(components: "foo.swift").pathString,
foo.appending(components: "Foo.xcassets").pathString,
foo.appending(components: "Foo.xcstrings").pathString,
foo.appending(components: "Foo.xib").pathString,
foo.appending(components: "Foo.xcdatamodel").pathString,
foo.appending(components: "Foo.metal").pathString,
foo.appending(components: "PrivacyInfo.xcprivacy").pathString
)

let manifest = Manifest.createRootManifest(
displayName: "Foo",
toolsVersion: .v6_0,
targets: [
try TargetDescription(name: "Foo"),
]
)

PackageBuilderTester(manifest, path: root, supportXCBuildTypes: true, in: fs) { result, diagnostics in
result.checkModule("Foo") { result in
result.checkSources(sources: ["foo.swift"])
result.checkResources(resources: [
foo.appending(components: "Foo.xib").pathString,
foo.appending(components: "Foo.xcdatamodel").pathString,
foo.appending(components: "Foo.xcassets").pathString,
foo.appending(components: "Foo.xcstrings").pathString,
foo.appending(components: "Foo.metal").pathString,
foo.appending(components: "PrivacyInfo.xcprivacy").pathString,
])
}
}
}

func testXCPrivacyNoDiagnostics() throws {
// In SwiftTools 6.0 and later, xcprivacy file types should not produce diagnostics messages when included
// as resources and built with `swift build`.

let root: AbsolutePath = "/Foo"
let foo = root.appending(components: "Sources", "Foo")

let fs = InMemoryFileSystem(emptyFiles:
foo.appending(components: "foo.swift").pathString,
foo.appending(components: "PrivacyInfo.xcprivacy").pathString
)

let manifest = Manifest.createRootManifest(
displayName: "Foo",
toolsVersion: .v6_0,
targets: [
try TargetDescription(
name: "Foo",
resources: [.init(rule: .copy, path: "PrivacyInfo.xcprivacy")]
),
]
)

PackageBuilderTester(manifest, path: root, supportXCBuildTypes: false, in: fs) { result, diagnostics in
result.checkModule("Foo") { result in
result.checkSources(sources: ["foo.swift"])
result.checkResources(resources: [
foo.appending(components: "PrivacyInfo.xcprivacy").pathString,
])
}

diagnostics.checkIsEmpty()
}
}

func testSnippetsLinkProductLibraries() throws {
let root = AbsolutePath("/Foo")
let internalSourcesDir = root.appending(components: "Sources", "Internal")
Expand Down