Skip to content

Improve check for unsafe flags #2845

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
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
2 changes: 1 addition & 1 deletion Sources/PackageGraph/PackageGraphLoader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ private final class ResolvedTargetBuilder: ResolvedBuilder<ResolvedTarget> {

func diagnoseInvalidUseOfUnsafeFlags(_ product: ResolvedProduct) {
// Diagnose if any target in this product uses an unsafe flag.
for target in product.targets {
for target in product.recursiveTargetDependencies() {
let declarations = target.underlyingTarget.buildSettings.assignments.keys
for decl in declarations {
if BuildSettings.Declaration.unsafeSettings.contains(decl) {
Expand Down
6 changes: 6 additions & 0 deletions Sources/PackageModel/ResolvedModels.swift
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,12 @@ public final class ResolvedProduct: ObjectIdentifierProtocol, CustomStringConver
// contain Swift code we don't know about as part of this build).
return targets.contains { $0.underlyingTarget is SwiftTarget }
}

/// Returns the recursive target dependencies.
public func recursiveTargetDependencies() -> [ResolvedTarget] {
let recursiveDependencies = targets.lazy.flatMap { $0.recursiveTargetDependencies() }
return Array(Set(targets).union(recursiveDependencies))
}
}

extension ResolvedTarget.Dependency: CustomStringConvertible {
Expand Down
17 changes: 13 additions & 4 deletions Tests/PackageGraphTests/PackageGraphTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -887,9 +887,11 @@ class PackageGraphTests: XCTestCase {
func testUnsafeFlags() throws {
let fs = InMemoryFileSystem(emptyFiles:
"/Foo/Sources/Foo/foo.swift",
"/Foo/Sources/Foo2/foo.swift",
"/Bar/Sources/Bar/bar.swift",
"/Bar/Sources/Bar2/bar.swift",
"/Bar/Sources/Bar3/bar.swift",
"/Bar/Sources/TransitiveBar/bar.swift",
"<end>"
)

Expand All @@ -906,14 +908,16 @@ class PackageGraphTests: XCTestCase {
],
targets: [
TargetDescription(name: "Foo", dependencies: ["Bar"]),
TargetDescription(name: "Foo2", dependencies: ["TransitiveBar"]),
]),
Manifest.createV4Manifest(
name: "Bar",
path: "/Bar",
url: "/Bar",
packageKind: .local,
products: [
ProductDescription(name: "Bar", targets: ["Bar", "Bar2", "Bar3"])
ProductDescription(name: "Bar", targets: ["Bar", "Bar2", "Bar3"]),
ProductDescription(name: "TransitiveBar", targets: ["TransitiveBar"]),
],
targets: [
TargetDescription(
Expand All @@ -933,14 +937,19 @@ class PackageGraphTests: XCTestCase {
TargetDescription(
name: "Bar3"
),
TargetDescription(
name: "TransitiveBar",
dependencies: ["Bar2"]
),
]),
]
)

XCTAssertEqual(diagnostics.diagnostics.count, 2)
XCTAssertEqual(diagnostics.diagnostics.count, 3)
DiagnosticsEngineTester(diagnostics, ignoreNotes: true) { result in
result.check(diagnostic: .contains("the target 'Bar' in product 'Bar' contains unsafe build flags"), behavior: .error)
result.check(diagnostic: .contains("the target 'Bar2' in product 'Bar' contains unsafe build flags"), behavior: .error)
result.checkUnordered(diagnostic: .contains("the target 'Bar2' in product 'TransitiveBar' contains unsafe build flags"), behavior: .error)
result.checkUnordered(diagnostic: .contains("the target 'Bar' in product 'Bar' contains unsafe build flags"), behavior: .error)
result.checkUnordered(diagnostic: .contains("the target 'Bar2' in product 'Bar' contains unsafe build flags"), behavior: .error)
}
}

Expand Down
3 changes: 2 additions & 1 deletion Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3843,7 +3843,8 @@ final class WorkspaceTests: XCTestCase {
// We should only see errors about use of unsafe flag in the version-based dependency.
workspace.checkPackageGraph(roots: ["Foo", "Bar"]) { (graph, diagnostics) in
DiagnosticsEngineTester(diagnostics, ignoreNotes: true) { result in
result.check(diagnostic: .equal("the target 'Baz' in product 'Baz' contains unsafe build flags"), behavior: .error)
result.checkUnordered(diagnostic: .equal("the target 'Baz' in product 'Baz' contains unsafe build flags"), behavior: .error)
result.checkUnordered(diagnostic: .equal("the target 'Bar' in product 'Baz' contains unsafe build flags"), behavior: .error)
}
}
}
Expand Down