Skip to content

Revert "refine location comparison when pre-loading package (#7002)" #7031

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
merged 1 commit into from
Oct 25, 2023
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
13 changes: 2 additions & 11 deletions Sources/PackageGraph/PubGrub/ContainerProvider.swift
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,14 @@ final class ContainerProvider {
completion: @escaping (Result<PubGrubPackageContainer, Error>) -> Void
) {
// Return the cached container, if available.
if let container = self.containersCache[comparingLocation: package] {
if let container = self.containersCache[package], package.equalsIncludingLocation(container.package) {
return completion(.success(container))
}

if let prefetchSync = self.prefetches[package] {
// If this container is already being prefetched, wait for that to complete
prefetchSync.notify(queue: .sharedConcurrent) {
if let container = self.containersCache[comparingLocation: package] {
if let container = self.containersCache[package], package.equalsIncludingLocation(container.package) {
// should be in the cache once prefetch completed
return completion(.success(container))
} else {
Expand Down Expand Up @@ -125,12 +125,3 @@ final class ContainerProvider {
}
}
}

extension ThreadSafeKeyValueStore where Key == PackageReference, Value == PubGrubPackageContainer {
subscript(comparingLocation package: PackageReference) -> PubGrubPackageContainer? {
if let container = self[package], container.package.equalsIncludingLocation(package) {
return container
}
return .none
}
}
2 changes: 1 addition & 1 deletion Sources/PackageModel/PackageIdentity.swift
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ public struct CanonicalPackageLocation: Equatable, CustomStringConvertible {
}

/// Similar to `CanonicalPackageLocation` but differentiates based on the scheme.
public struct CanonicalPackageURL: Equatable, CustomStringConvertible {
public struct CanonicalPackageURL: CustomStringConvertible {
public let description: String
public let scheme: String?

Expand Down
19 changes: 1 addition & 18 deletions Sources/PackageModel/PackageReference.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,24 +161,7 @@ extension PackageReference: Equatable {

// TODO: consider rolling into Equatable
public func equalsIncludingLocation(_ other: PackageReference) -> Bool {
if self.identity != other.identity {
return false
}
if self.canonicalLocation != other.canonicalLocation {
return false
}
switch (self.kind, other.kind) {
case (.remoteSourceControl(let lurl), .remoteSourceControl(let rurl)):
return lurl.canonicalURL == rurl.canonicalURL
default:
return true
}
}
}

extension SourceControlURL {
var canonicalURL: CanonicalPackageURL {
CanonicalPackageURL(self.absoluteString)
return self.identity == other.identity && self.canonicalLocation == other.canonicalLocation
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Workspace/ManagedDependency.swift
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,7 @@ extension Workspace {

// When loading manifests in Workspace, there are cases where we must also compare the location
// as it may attempt to load manifests for dependencies that have the same identity but from a different location
// (e.g. dependency is changed to a fork with the same identity)
// (e.g. dependency is changed to a fork with the same identity)
public subscript(comparingLocation package: PackageReference) -> ManagedDependency? {
if let dependency = self.dependencies[package.identity], dependency.packageRef.equalsIncludingLocation(package) {
return dependency
Expand Down
3 changes: 2 additions & 1 deletion Sources/Workspace/Workspace+Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,8 +859,9 @@ extension Workspace {
// a required dependency that is already loaded (managed) should be represented in the pins store.
// also comparing location as it may have changed at this point
if requiredDependencies.contains(where: { $0.equalsIncludingLocation(dependency.packageRef) }) {
let pin = pinsStore.pins[dependency.packageRef.identity]
// if pin not found, or location is different (it may have changed at this point) pin it
if pinsStore.pins[comparingLocation: dependency.packageRef] == .none {
if !(pin?.packageRef.equalsIncludingLocation(dependency.packageRef) ?? false) {
pinsStore.pin(dependency)
}
} else if let pin = pinsStore.pins[dependency.packageRef.identity] {
Expand Down
35 changes: 11 additions & 24 deletions Sources/Workspace/Workspace+Manifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,6 @@ extension Workspace {
}
}

let topLevelDependencies = root.packages.flatMap { $1.manifest.dependencies.map(\.packageRef) }

var requiredIdentities: OrderedCollections.OrderedSet<PackageReference> = []
_ = transitiveClosure(inputNodes) { node in
node.manifest.dependenciesRequired(for: node.productFilter).compactMap { dependency in
Expand All @@ -211,28 +209,17 @@ extension Workspace {
if existing.canonicalLocation == package.canonicalLocation {
// same literal location is fine
if existing.locationString != package.locationString {
// we prefer the top level dependencies
if topLevelDependencies.contains(where: {
$0.locationString == existing.locationString
}) {
observabilityScope.emit(debug: """
similar variants of package '\(package.identity)' \
found at '\(package.locationString)' and '\(existing.locationString)'. \
using preferred root variant '\(existing.locationString)'
""")
} else {
let preferred = [existing, package].sorted(by: {
$0.locationString > $1.locationString
}).first! // safe
observabilityScope.emit(debug: """
similar variants of package '\(package.identity)' \
found at '\(package.locationString)' and '\(existing.locationString)'. \
using preferred variant '\(preferred.locationString)'
""")
if preferred.locationString != existing.locationString {
requiredIdentities.remove(existing)
requiredIdentities.insert(preferred, at: index)
}
let preferred = [existing, package].sorted(by: {
$0.locationString > $1.locationString
}).first! // safe
observabilityScope.emit(debug: """
similar variants of package '\(package.identity)' \
found at '\(package.locationString)' and '\(existing.locationString)'. \
using preferred variant '\(preferred.locationString)'
""")
if preferred.locationString != existing.locationString {
requiredIdentities.remove(existing)
requiredIdentities.insert(preferred, at: index)
}
}
} else {
Expand Down
13 changes: 2 additions & 11 deletions Sources/Workspace/Workspace+Pinning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ extension Workspace {
needsUpdate = true
} else {
for dependency in dependenciesToPin {
if let pin = storedPinStore.pins[comparingLocation: dependency.packageRef] {
if pin.state != PinsStore.Pin(dependency)?.state {
if let pin = storedPinStore.pins.first(where: { $0.value.packageRef.equalsIncludingLocation(dependency.packageRef) }) {
if pin.value.state != PinsStore.Pin(dependency)?.state {
needsUpdate = true
break
}
Expand Down Expand Up @@ -180,12 +180,3 @@ extension PinsStore.PinState {
}
}
}

extension PinsStore.Pins {
subscript(comparingLocation package: PackageReference) -> PinsStore.Pin? {
if let pin = self[package.identity], pin.packageRef.equalsIncludingLocation(package) {
return pin
}
return .none
}
}
Loading