Skip to content

Commit 5389245

Browse files
committed
Improve check for unsafe flags
We prohibit unsafe flags in package dependencies, but the check was only looking at targets which are direct dependencies of a product, not transitive ones. rdar://problem/66499615
1 parent 9f06667 commit 5389245

File tree

3 files changed

+19
-6
lines changed

3 files changed

+19
-6
lines changed

Sources/PackageGraph/PackageGraphLoader.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -570,8 +570,11 @@ private final class ResolvedTargetBuilder: ResolvedBuilder<ResolvedTarget> {
570570
}
571571

572572
func diagnoseInvalidUseOfUnsafeFlags(_ product: ResolvedProduct) {
573+
let inputTargets = product.targets
574+
let recursiveDependencies = inputTargets.lazy.flatMap { $0.recursiveDependencies() }
575+
let reachableTargets = Set(inputTargets).union(recursiveDependencies.compactMap { $0.target })
573576
// Diagnose if any target in this product uses an unsafe flag.
574-
for target in product.targets {
577+
for target in reachableTargets {
575578
let declarations = target.underlyingTarget.buildSettings.assignments.keys
576579
for decl in declarations {
577580
if BuildSettings.Declaration.unsafeSettings.contains(decl) {

Tests/PackageGraphTests/PackageGraphTests.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -887,9 +887,11 @@ class PackageGraphTests: XCTestCase {
887887
func testUnsafeFlags() throws {
888888
let fs = InMemoryFileSystem(emptyFiles:
889889
"/Foo/Sources/Foo/foo.swift",
890+
"/Foo/Sources/Foo2/foo.swift",
890891
"/Bar/Sources/Bar/bar.swift",
891892
"/Bar/Sources/Bar2/bar.swift",
892893
"/Bar/Sources/Bar3/bar.swift",
894+
"/Bar/Sources/TransitiveBar/bar.swift",
893895
"<end>"
894896
)
895897

@@ -906,14 +908,16 @@ class PackageGraphTests: XCTestCase {
906908
],
907909
targets: [
908910
TargetDescription(name: "Foo", dependencies: ["Bar"]),
911+
TargetDescription(name: "Foo2", dependencies: ["TransitiveBar"]),
909912
]),
910913
Manifest.createV4Manifest(
911914
name: "Bar",
912915
path: "/Bar",
913916
url: "/Bar",
914917
packageKind: .local,
915918
products: [
916-
ProductDescription(name: "Bar", targets: ["Bar", "Bar2", "Bar3"])
919+
ProductDescription(name: "Bar", targets: ["Bar", "Bar2", "Bar3"]),
920+
ProductDescription(name: "TransitiveBar", targets: ["TransitiveBar"]),
917921
],
918922
targets: [
919923
TargetDescription(
@@ -933,14 +937,19 @@ class PackageGraphTests: XCTestCase {
933937
TargetDescription(
934938
name: "Bar3"
935939
),
940+
TargetDescription(
941+
name: "TransitiveBar",
942+
dependencies: ["Bar2"]
943+
),
936944
]),
937945
]
938946
)
939947

940-
XCTAssertEqual(diagnostics.diagnostics.count, 2)
948+
XCTAssertEqual(diagnostics.diagnostics.count, 3)
941949
DiagnosticsEngineTester(diagnostics, ignoreNotes: true) { result in
942-
result.check(diagnostic: .contains("the target 'Bar' in product 'Bar' contains unsafe build flags"), behavior: .error)
943-
result.check(diagnostic: .contains("the target 'Bar2' in product 'Bar' contains unsafe build flags"), behavior: .error)
950+
result.checkUnordered(diagnostic: .contains("the target 'Bar2' in product 'TransitiveBar' contains unsafe build flags"), behavior: .error)
951+
result.checkUnordered(diagnostic: .contains("the target 'Bar' in product 'Bar' contains unsafe build flags"), behavior: .error)
952+
result.checkUnordered(diagnostic: .contains("the target 'Bar2' in product 'Bar' contains unsafe build flags"), behavior: .error)
944953
}
945954
}
946955

Tests/WorkspaceTests/WorkspaceTests.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3843,7 +3843,8 @@ final class WorkspaceTests: XCTestCase {
38433843
// We should only see errors about use of unsafe flag in the version-based dependency.
38443844
workspace.checkPackageGraph(roots: ["Foo", "Bar"]) { (graph, diagnostics) in
38453845
DiagnosticsEngineTester(diagnostics, ignoreNotes: true) { result in
3846-
result.check(diagnostic: .equal("the target 'Baz' in product 'Baz' contains unsafe build flags"), behavior: .error)
3846+
result.checkUnordered(diagnostic: .equal("the target 'Baz' in product 'Baz' contains unsafe build flags"), behavior: .error)
3847+
result.checkUnordered(diagnostic: .equal("the target 'Bar' in product 'Baz' contains unsafe build flags"), behavior: .error)
38473848
}
38483849
}
38493850
}

0 commit comments

Comments
 (0)