Skip to content

Commit b88cd88

Browse files
committed
Expanded providedLibrary PackageReference to include origin
This is useful for identity comparisons to enable replacement library to match against original remote package.
1 parent 6d0f18e commit b88cd88

File tree

8 files changed

+64
-27
lines changed

8 files changed

+64
-27
lines changed

Sources/PackageGraph/ModulesGraph.swift

+3-2
Original file line numberDiff line numberDiff line change
@@ -358,11 +358,12 @@ extension PackageGraphError: CustomStringConvertible {
358358
switch $0.manifest.packageKind {
359359
case .root(let path),
360360
.fileSystem(let path),
361-
.localSourceControl(let path),
362-
.providedLibrary(let path):
361+
.localSourceControl(let path):
363362
description += " (at '\(path)')"
364363
case .remoteSourceControl(let url):
365364
description += " (from '\(url)')"
365+
case .providedLibrary(let url, let path):
366+
description += " (from \(url) at \(path))"
366367
case .registry:
367368
break
368369
}

Sources/PackageLoading/ManifestJSONParser.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ enum ManifestJSONParser {
8383
case .localSourceControl(let _packagePath):
8484
// we have a more accurate path than the virtual one
8585
packagePath = _packagePath
86-
case .root(let _packagePath), .fileSystem(let _packagePath), .providedLibrary(let _packagePath):
86+
case .root(let _packagePath), .fileSystem(let _packagePath), .providedLibrary(_, let _packagePath):
8787
// we dont have a more accurate path, and they should be the same
8888
// asserting (debug only) to make sure refactoring is correct 11/2023
8989
assert(packagePath == _packagePath, "expecting package path '\(packagePath)' to be the same as '\(_packagePath)'")

Sources/PackageModel/IdentityResolver.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,8 +46,8 @@ public struct DefaultIdentityResolver: IdentityResolver {
4646
return try self.resolveIdentity(for: url)
4747
case .registry(let identity):
4848
return identity
49-
case .providedLibrary(let path):
50-
return try self.resolveIdentity(for: path)
49+
case .providedLibrary(let url, _):
50+
return try self.resolveIdentity(for: url)
5151
}
5252
}
5353

Sources/PackageModel/PackageReference.swift

+21-11
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ public struct PackageReference {
3434
/// A package from a registry.
3535
case registry(PackageIdentity)
3636

37-
/// A prebuilt library provided by a toolchain
38-
case providedLibrary(AbsolutePath)
37+
/// A prebuilt library provided by a toolchain for a package identified by the given "origin" URL.
38+
case providedLibrary(SourceControlURL, AbsolutePath)
3939

4040
// FIXME: we should not need this once we migrate off URLs
4141
//@available(*, deprecated)
@@ -52,8 +52,8 @@ public struct PackageReference {
5252
case .registry(let identity):
5353
// FIXME: this is a placeholder
5454
return identity.description
55-
case .providedLibrary(let path):
56-
return path.pathString
55+
case .providedLibrary(let url, _):
56+
return url.absoluteString
5757
}
5858
}
5959

@@ -75,8 +75,8 @@ public struct PackageReference {
7575
return "remoteSourceControl \(url)"
7676
case .registry(let identity):
7777
return "registry \(identity)"
78-
case .providedLibrary(let path):
79-
return "library \(path)"
78+
case .providedLibrary(let url, let path):
79+
return "provided library for \(url) @ \(path)"
8080
}
8181
}
8282

@@ -131,8 +131,8 @@ public struct PackageReference {
131131
case .registry(let identity):
132132
// FIXME: this is a placeholder
133133
self.deprecatedName = name ?? identity.description
134-
case .providedLibrary(let path):
135-
self.deprecatedName = name ?? PackageIdentityParser.computeDefaultName(fromPath: path)
134+
case .providedLibrary(let url, _):
135+
self.deprecatedName = name ?? PackageIdentityParser.computeDefaultName(fromURL: url)
136136
}
137137
}
138138

@@ -161,8 +161,12 @@ public struct PackageReference {
161161
PackageReference(identity: identity, kind: .registry(identity))
162162
}
163163

164-
public static func library(identity: PackageIdentity, path: AbsolutePath) -> PackageReference {
165-
PackageReference(identity: identity, kind: .providedLibrary(path))
164+
public static func providedLibrary(
165+
identity: PackageIdentity,
166+
origin: SourceControlURL,
167+
path: AbsolutePath
168+
) -> PackageReference {
169+
PackageReference(identity: identity, kind: .providedLibrary(origin, path))
166170
}
167171
}
168172

@@ -183,6 +187,11 @@ extension PackageReference: Equatable {
183187
switch (self.kind, other.kind) {
184188
case (.remoteSourceControl(let lurl), .remoteSourceControl(let rurl)):
185189
return lurl.canonicalURL == rurl.canonicalURL
190+
case (.remoteSourceControl(let lurl), .providedLibrary(let rurl, _)),
191+
(.providedLibrary(let lurl, _), .remoteSourceControl(let rurl)):
192+
return lurl.canonicalURL == rurl.canonicalURL
193+
case (.providedLibrary(_, let lpath), .providedLibrary(_, let rpath)):
194+
return lpath == rpath
186195
default:
187196
return true
188197
}
@@ -237,8 +246,9 @@ extension PackageReference.Kind: Encodable {
237246
case .registry:
238247
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .registry)
239248
try unkeyedContainer.encode(self.isRoot)
240-
case .providedLibrary(let path):
249+
case .providedLibrary(let url, let path):
241250
var unkeyedContainer = container.nestedUnkeyedContainer(forKey: .providedLibrary)
251+
try unkeyedContainer.encode(url)
242252
try unkeyedContainer.encode(path)
243253
}
244254
}

Sources/SPMTestSupport/MockManifestLoader.swift

+4-4
Original file line numberDiff line numberDiff line change
@@ -109,8 +109,8 @@ extension ManifestLoader {
109109
packageIdentity = identity
110110
// FIXME: placeholder
111111
packageLocation = identity.description
112-
case .providedLibrary(let path):
113-
packageIdentity = try identityResolver.resolveIdentity(for: path)
112+
case .providedLibrary(let url, let path):
113+
packageIdentity = try identityResolver.resolveIdentity(for: url)
114114
packageLocation = path.pathString
115115
}
116116
return try await self.load(
@@ -159,8 +159,8 @@ extension ManifestLoader {
159159
packageIdentity = identity
160160
// FIXME: placeholder
161161
packageLocation = identity.description
162-
case .providedLibrary(let path):
163-
packageIdentity = try identityResolver.resolveIdentity(for: path)
162+
case .providedLibrary(let url, let path):
163+
packageIdentity = try identityResolver.resolveIdentity(for: url)
164164
packageLocation = path.pathString
165165
}
166166
return try await self.load(

Sources/Workspace/Diagnostics.swift

+2-2
Original file line numberDiff line numberDiff line change
@@ -183,9 +183,9 @@ extension Basics.Diagnostic {
183183
switch $0.kind {
184184
case .registry(let identity):
185185
return "'\(identity.description)'"
186-
case .remoteSourceControl(let url):
186+
case .remoteSourceControl(let url), .providedLibrary(let url, _):
187187
return "'\($0.identity)' from \(url)"
188-
case .localSourceControl(let path), .fileSystem(let path), .root(let path), .providedLibrary(let path):
188+
case .localSourceControl(let path), .fileSystem(let path), .root(let path):
189189
return "'\($0.identity)' at \(path)"
190190
}
191191
}

Sources/Workspace/Workspace+State.swift

+30-4
Original file line numberDiff line numberDiff line change
@@ -420,28 +420,35 @@ extension WorkspaceStateStorage {
420420
let kind: Kind
421421
let location: String
422422
let name: String
423+
let originURL: String?
423424

424425
init(_ reference: PackageModel.PackageReference) {
425426
self.identity = reference.identity.description
426427
switch reference.kind {
427428
case .root(let path):
428429
self.kind = .root
429430
self.location = path.pathString
431+
self.originURL = nil
430432
case .fileSystem(let path):
431433
self.kind = .fileSystem
432434
self.location = path.pathString
435+
self.originURL = nil
433436
case .localSourceControl(let path):
434437
self.kind = .localSourceControl
435438
self.location = path.pathString
439+
self.originURL = nil
436440
case .remoteSourceControl(let url):
437441
self.kind = .remoteSourceControl
438442
self.location = url.absoluteString
443+
self.originURL = nil
439444
case .registry:
440445
self.kind = .registry
441446
// FIXME: placeholder
442447
self.location = self.identity.description
443-
case .providedLibrary(let path):
448+
self.originURL = nil
449+
case .providedLibrary(let url, let path):
444450
self.kind = .providedLibrary
451+
self.originURL = url.absoluteString
445452
self.location = path.pathString
446453
}
447454
self.name = reference.deprecatedName
@@ -497,7 +504,13 @@ extension PackageModel.PackageReference {
497504
case .registry:
498505
kind = .registry(identity)
499506
case .providedLibrary:
500-
kind = try .providedLibrary(.init(validating: reference.location))
507+
guard let url = reference.originURL else {
508+
throw InternalError("Cannot form provided library reference without origin: \(reference)")
509+
}
510+
kind = try .providedLibrary(
511+
SourceControlURL(url),
512+
.init(validating: reference.location)
513+
)
501514
}
502515

503516
self.init(
@@ -752,28 +765,35 @@ extension WorkspaceStateStorage {
752765
let kind: Kind
753766
let location: String
754767
let name: String
768+
let originURL: String?
755769

756770
init(_ reference: PackageModel.PackageReference) {
757771
self.identity = reference.identity.description
758772
switch reference.kind {
759773
case .root(let path):
760774
self.kind = .root
761775
self.location = path.pathString
776+
self.originURL = nil
762777
case .fileSystem(let path):
763778
self.kind = .fileSystem
764779
self.location = path.pathString
780+
self.originURL = nil
765781
case .localSourceControl(let path):
766782
self.kind = .localSourceControl
767783
self.location = path.pathString
784+
self.originURL = nil
768785
case .remoteSourceControl(let url):
769786
self.kind = .remoteSourceControl
770787
self.location = url.absoluteString
788+
self.originURL = nil
771789
case .registry:
772790
self.kind = .registry
773791
// FIXME: placeholder
774792
self.location = self.identity.description
775-
case .providedLibrary(let path):
793+
self.originURL = nil
794+
case .providedLibrary(let url, let path):
776795
self.kind = .providedLibrary
796+
self.originURL = url.absoluteString
777797
self.location = path.pathString
778798
}
779799
self.name = reference.deprecatedName
@@ -830,7 +850,13 @@ extension PackageModel.PackageReference {
830850
case .registry:
831851
kind = .registry(identity)
832852
case .providedLibrary:
833-
kind = try .providedLibrary(.init(validating: reference.location))
853+
guard let url = reference.originURL else {
854+
throw InternalError("Cannot form a provided library reference without origin: \(reference)")
855+
}
856+
kind = try .providedLibrary(
857+
SourceControlURL(url),
858+
.init(validating: reference.location)
859+
)
834860
}
835861

836862
self.init(

Tests/PackageLoadingTests/PDLoadingTests.swift

+1-1
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ class PackageDescriptionLoadingTests: XCTestCase, ManifestLoaderDelegate {
9797
packagePath = path
9898
case .remoteSourceControl, .registry:
9999
packagePath = .root
100-
case .providedLibrary(let path):
100+
case .providedLibrary(_, let path):
101101
packagePath = path
102102
}
103103

0 commit comments

Comments
 (0)