Skip to content

bring back #7002 #7039

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 26, 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: 11 additions & 2 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[package], package.equalsIncludingLocation(container.package) {
if let container = self.containersCache[comparingLocation: 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[package], package.equalsIncludingLocation(container.package) {
if let container = self.containersCache[comparingLocation: package] {
// should be in the cache once prefetch completed
return completion(.success(container))
} else {
Expand Down Expand Up @@ -125,3 +125,12 @@ 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: CustomStringConvertible {
public struct CanonicalPackageURL: Equatable, CustomStringConvertible {
public let description: String
public let scheme: String?

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

// TODO: consider rolling into Equatable
public func equalsIncludingLocation(_ other: PackageReference) -> Bool {
return self.identity == other.identity && self.canonicalLocation == other.canonicalLocation
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)
}
}

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: 1 addition & 2 deletions Sources/Workspace/Workspace+Dependencies.swift
Original file line number Diff line number Diff line change
Expand Up @@ -859,9 +859,8 @@ 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 !(pin?.packageRef.equalsIncludingLocation(dependency.packageRef) ?? false) {
if pinsStore.pins[comparingLocation: dependency.packageRef] == .none {
pinsStore.pin(dependency)
}
} else if let pin = pinsStore.pins[dependency.packageRef.identity] {
Expand Down
35 changes: 24 additions & 11 deletions Sources/Workspace/Workspace+Manifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,8 @@ 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 @@ -209,17 +211,28 @@ extension Workspace {
if existing.canonicalLocation == package.canonicalLocation {
// same literal location is fine
if existing.locationString != package.locationString {
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)
// 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)
}
}
}
} else {
Expand Down
21 changes: 18 additions & 3 deletions Sources/Workspace/Workspace+Pinning.swift
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,16 @@ extension Workspace {
for dependency in requiredDependencies {
if let managedDependency = self.state.dependencies[comparingLocation: dependency] {
dependenciesToPin.append(managedDependency)
} else if let managedDependency = self.state.dependencies[dependency.identity] {
observabilityScope
.emit(
info: "required dependency '\(dependency.identity)' from '\(dependency.locationString)' was not found in managed dependencies, using alternative location '\(managedDependency.packageRef.locationString)' instead"
)
dependenciesToPin.append(ManagedDependency(packageRef: dependency, state: managedDependency.state, subpath: managedDependency.subpath))
} else {
observabilityScope
.emit(
warning: "required dependency \(dependency.identity) (\(dependency.locationString)) was not found in managed dependencies and will not be recorded in resolved file"
warning: "required dependency '\(dependency.identity)' from '\(dependency.locationString)' was not found in managed dependencies and will not be recorded in resolved file"
)
}
}
Expand All @@ -49,8 +55,8 @@ extension Workspace {
needsUpdate = true
} else {
for dependency in dependenciesToPin {
if let pin = storedPinStore.pins.first(where: { $0.value.packageRef.equalsIncludingLocation(dependency.packageRef) }) {
if pin.value.state != PinsStore.Pin(dependency)?.state {
if let pin = storedPinStore.pins[comparingLocation: dependency.packageRef] {
if pin.state != PinsStore.Pin(dependency)?.state {
needsUpdate = true
break
}
Expand Down Expand Up @@ -180,3 +186,12 @@ 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