1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
|
//===----------------------------------------------------------------------===//
//
// This source file is part of the Swift open source project
//
// Copyright (c) 2019-2023 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
import Basics
import OrderedCollections
import PackageModel
import struct TSCUtility.Version
/// A container for an individual package. This enhances PackageContainer to add PubGrub specific
/// logic which is mostly related to computing incompatibilities at a particular version.
internal final class PubGrubPackageContainer {
/// The underlying package container.
let underlying: PackageContainer
/// Reference to the pins map.
private let pins: PinsStore.Pins
init(underlying: PackageContainer, pins: PinsStore.Pins) {
self.underlying = underlying
self.pins = pins
}
var package: PackageReference {
self.underlying.package
}
/// Returns the pinned version for this package, if any.
var pinnedVersion: Version? {
switch self.pins[self.underlying.package.identity]?.state {
case .version(let version, _):
return version
default:
return .none
}
}
/// Returns the numbers of versions that are satisfied by the given version requirement.
func versionCount(_ requirement: VersionSetSpecifier) throws -> Int {
if let pinnedVersion, requirement.contains(pinnedVersion) {
return 1
}
return try self.underlying.versionsDescending().filter(requirement.contains).count
}
/// Computes the bounds of the given range against the versions available in the package.
///
/// `includesLowerBound` is `false` if range's lower bound is less than or equal to the lowest available version.
/// Similarly, `includesUpperBound` is `false` if range's upper bound is greater than or equal to the highest available version.
func computeBounds(for range: Range<Version>) throws -> (includesLowerBound: Bool, includesUpperBound: Bool) {
var includeLowerBound = true
var includeUpperBound = true
let versions = try self.underlying.versionsDescending()
if let last = versions.last, range.lowerBound < last {
includeLowerBound = false
}
if let first = versions.first, range.upperBound > first {
includeUpperBound = false
}
return (includeLowerBound, includeUpperBound)
}
/// Returns the best available version for a given term.
func getBestAvailableVersion(for term: Term) throws -> Version? {
assert(term.isPositive, "Expected term to be positive")
var versionSet = term.requirement
// Restrict the selection to the pinned version if is allowed by the current requirements.
if let pinnedVersion = self.pinnedVersion {
if versionSet.contains(pinnedVersion) {
if !self.underlying.shouldInvalidatePinnedVersions {
versionSet = .exact(pinnedVersion)
} else {
// Make sure the pinned version is still available
let version = try self.underlying.versionsDescending().first { pinnedVersion == $0 }
if version != nil {
return version
}
}
}
}
// Return the highest version that is allowed by the input requirement.
return try self.underlying.versionsDescending().first { versionSet.contains($0) }
}
/// Compute the bounds of incompatible tools version starting from the given version.
private func computeIncompatibleToolsVersionBounds(fromVersion: Version) throws -> VersionSetSpecifier {
assert(!self.underlying.isToolsVersionCompatible(at: fromVersion))
let versions: [Version] = try self.underlying.versionsAscending()
// This is guaranteed to be present.
let idx = versions.firstIndex(of: fromVersion)!
var lowerBound = fromVersion
var upperBound = fromVersion
for version in versions.dropFirst(idx + 1) {
let isToolsVersionCompatible = self.underlying.isToolsVersionCompatible(at: version)
if isToolsVersionCompatible {
break
}
upperBound = version
}
for version in versions.dropLast(versions.count - idx).reversed() {
let isToolsVersionCompatible = self.underlying.isToolsVersionCompatible(at: version)
if isToolsVersionCompatible {
break
}
lowerBound = version
}
// If lower and upper bounds didn't change then this is the sole incompatible version.
if lowerBound == upperBound {
return .exact(lowerBound)
}
// If lower bound is the first version then we can use 0 as the sentinel. This
// will end up producing a better diagnostic since we can omit the lower bound.
if lowerBound == versions.first {
lowerBound = "0.0.0"
}
if upperBound == versions.last {
// If upper bound is the last version then we can use the next major version as the sentinel.
// This will end up producing a better diagnostic since we can omit the upper bound.
upperBound = Version(upperBound.major + 1, 0, 0)
} else {
// Use the next patch since the upper bound needs to be inclusive here.
upperBound = upperBound.nextPatch()
}
return .range(lowerBound ..< upperBound.nextPatch())
}
/// Returns the incompatibilities of a package at the given version.
func incompatibilites(
at version: Version,
node: DependencyResolutionNode,
overriddenPackages: [PackageReference: (version: BoundVersion, products: ProductFilter)],
root: DependencyResolutionNode
) throws -> [Incompatibility] {
// FIXME: It would be nice to compute bounds for this as well.
if !self.underlying.isToolsVersionCompatible(at: version) {
let requirement = try self.computeIncompatibleToolsVersionBounds(fromVersion: version)
let toolsVersion = try self.underlying.toolsVersion(for: version)
return [try Incompatibility(Term(node, requirement), root: root, cause: .incompatibleToolsVersion(toolsVersion))]
}
var unprocessedDependencies = try self.underlying.getDependencies(at: version, productFilter: node.productFilter)
if let sharedVersion = node.versionLock(version: version) {
unprocessedDependencies.append(sharedVersion)
}
var constraints: [PackageContainerConstraint] = []
for dep in unprocessedDependencies {
// Version-based packages are not allowed to contain unversioned dependencies.
guard case .versionSet = dep.requirement else {
let cause: Incompatibility.Cause = .versionBasedDependencyContainsUnversionedDependency(
versionedDependency: self.package,
unversionedDependency: dep.package
)
return [try Incompatibility(Term(node, .exact(version)), root: root, cause: cause)]
}
// Skip if this package is overridden.
if overriddenPackages.keys.contains(dep.package) {
continue
}
for node in dep.nodes() {
constraints.append(
PackageContainerConstraint(
package: node.package,
requirement: dep.requirement,
products: node.productFilter
)
)
}
}
return try constraints.flatMap { constraint -> [Incompatibility] in
// We only have version-based requirements at this point.
guard case .versionSet(let constraintRequirement) = constraint.requirement else {
throw InternalError("Unexpected unversioned requirement: \(constraint)")
}
return try constraint.nodes().compactMap { constraintNode in
// cycle
guard node != constraintNode else {
return nil
}
var terms: OrderedCollections.OrderedSet<Term> = []
// the package version requirement
terms.append(Term(node, .exact(version)))
// the dependency's version requirement
terms.append(Term(not: constraintNode, constraintRequirement))
return try Incompatibility(terms, root: root, cause: .dependency(node: node))
}
}
}
}
|