Skip to content

Commit f79acf3

Browse files
authored
Add package metadata to observability scopes used by PackageContainers (#5969)
PackageContainer is a protocol that is implemented by various types involved in SwiftPM package resolution. They can emit messages but those messages don't get associated with the diagnostics. There are currently three implementors of this protocol: FileSystemPackageContainer, SourceControlPackageContainer, and RegistryPackageContainer. All three just assign a given ObservabilityScope, and setting a package location metadata on that scope is a good way to make sure that all the diagnostics emitted to that scope get associated with the package. This fix makes sure that diagnostics emitted through these scopes get properly associated with the package. This is particularly useful for manifest-related diagnostics, which inherit the scope used by the various kinds of PackageContainer. rdar://103229985
1 parent 420ef0a commit f79acf3

File tree

5 files changed

+17
-5
lines changed

5 files changed

+17
-5
lines changed

Sources/PackageModel/PackageReference.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,12 @@ extension PackageReference: Hashable {
173173
}
174174
}
175175

176+
extension PackageReference {
177+
public var diagnosticsMetadata: ObservabilityMetadata {
178+
return .packageMetadata(identity: self.identity, kind: self.kind)
179+
}
180+
}
181+
176182
extension PackageReference: CustomStringConvertible {
177183
public var description: String {
178184
return "\(self.identity) \(self.kind)"

Sources/Workspace/FileSystemPackageContainer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,9 @@ public struct FileSystemPackageContainer: PackageContainer {
5757
self.manifestLoader = manifestLoader
5858
self.currentToolsVersion = currentToolsVersion
5959
self.fileSystem = fileSystem
60-
self.observabilityScope = observabilityScope
60+
self.observabilityScope = observabilityScope.makeChildScope(
61+
description: "FileSystemPackageContainer",
62+
metadata: package.diagnosticsMetadata)
6163
}
6264

6365
private func loadManifest() throws -> Manifest {

Sources/Workspace/RegistryPackageContainer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,9 @@ public class RegistryPackageContainer: PackageContainer {
4646
self.registryClient = registryClient
4747
self.manifestLoader = manifestLoader
4848
self.currentToolsVersion = currentToolsVersion
49-
self.observabilityScope = observabilityScope
49+
self.observabilityScope = observabilityScope.makeChildScope(
50+
description: "RegistryPackageContainer",
51+
metadata: package.diagnosticsMetadata)
5052
}
5153

5254
// MARK: - PackageContainer

Sources/Workspace/SourceControlPackageContainer.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,9 @@ internal final class SourceControlPackageContainer: PackageContainer, CustomStri
9292
self.currentToolsVersion = currentToolsVersion
9393
self.fingerprintStorage = fingerprintStorage
9494
self.fingerprintCheckingMode = fingerprintCheckingMode
95-
self.observabilityScope = observabilityScope
95+
self.observabilityScope = observabilityScope.makeChildScope(
96+
description: "SourceControlPackageContainer",
97+
metadata: package.diagnosticsMetadata)
9698
}
9799

98100
// Compute the map of known versions.

Sources/Workspace/Workspace.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3129,7 +3129,7 @@ extension Workspace {
31293129
try? fileSystem.chmod(.userUnWritable, path: checkoutPath, options: [.recursive, .onlyFiles])
31303130

31313131
// Record the new state.
3132-
observabilityScope.emit(debug: "adding '\(package.identity)' (\(package.locationString)) to managed dependencies")
3132+
observabilityScope.emit(debug: "adding '\(package.identity)' (\(package.locationString)) to managed dependencies", metadata: package.diagnosticsMetadata)
31333133
self.state.dependencies.add(
31343134
try .sourceControlCheckout(
31353135
packageRef: package,
@@ -3280,7 +3280,7 @@ extension Workspace {
32803280
}
32813281

32823282
// Record the new state.
3283-
observabilityScope.emit(debug: "adding '\(package.identity)' (\(package.locationString)) to managed dependencies")
3283+
observabilityScope.emit(debug: "adding '\(package.identity)' (\(package.locationString)) to managed dependencies", metadata: package.diagnosticsMetadata)
32843284
self.state.dependencies.add(
32853285
try .registryDownload(
32863286
packageRef: package,

0 commit comments

Comments
 (0)