Skip to content

Commit 493ca23

Browse files
authored
The warning "dependency 'baz' is not used by any target" should be associated with a package (#6271)
Like other diagnostics, the warning "dependency 'baz' is not used by any target" should be associated with a package. This allows it to show up in the right place in a package resolution log in IDEs that show such things. rdar://106678793
1 parent 016448a commit 493ca23

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

Sources/PackageGraph/PackageGraph+Loading.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,11 +193,14 @@ private func checkAllDependenciesAreUsed(_ rootPackages: [ResolvedPackage], obse
193193
if dependency.products.contains(where: \.isCommandPlugin) {
194194
continue
195195
}
196+
197+
// Make sure that any diagnostics we emit below are associated with the package.
198+
let packageDiagnosticsScope = observabilityScope.makeChildScope(description: "Package Dependency Validation", metadata: package.underlyingPackage.diagnosticsMetadata)
196199

197200
// Otherwise emit a warning if none of the dependency package's products are used.
198201
let dependencyIsUsed = dependency.products.contains(where: productDependencies.contains)
199202
if !dependencyIsUsed && !observabilityScope.errorsReportedInAnyScope {
200-
observabilityScope.emit(.unusedDependency(dependency.identity.description))
203+
packageDiagnosticsScope.emit(.unusedDependency(dependency.identity.description))
201204
}
202205
}
203206
}

Tests/PackageGraphTests/PackageGraphTests.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,9 @@ class PackageGraphTests: XCTestCase {
892892
)
893893

894894
testDiagnostics(observability.diagnostics) { result in
895-
result.check(diagnostic: "dependency 'baz' is not used by any target", severity: .warning)
895+
let diagnostic = result.check(diagnostic: "dependency 'baz' is not used by any target", severity: .warning)
896+
XCTAssertEqual(diagnostic?.metadata?.packageIdentity, "foo")
897+
XCTAssertEqual(diagnostic?.metadata?.packageKind?.isRoot, true)
896898
#if ENABLE_TARGET_BASED_DEPENDENCY_RESOLUTION
897899
result.check(diagnostic: "dependency 'biz' is not used by any target", severity: .warning)
898900
#endif

0 commit comments

Comments
 (0)