@@ -1716,8 +1716,8 @@ extension Workspace {
1716
1716
needsUpdate = true
1717
1717
} else {
1718
1718
for dependency in dependenciesToPin {
1719
- if let pin = storedPinStore. pins. first ( where : { $0 . value . packageRef . equalsIncludingLocation ( dependency. packageRef) } ) {
1720
- if pin. value . state != PinsStore . Pin ( dependency) ? . state {
1719
+ if let pin = storedPinStore. pins [ comparingLocation : dependency. packageRef] {
1720
+ if pin. state != PinsStore . Pin ( dependency) ? . state {
1721
1721
needsUpdate = true
1722
1722
break
1723
1723
}
@@ -1913,6 +1913,8 @@ extension Workspace {
1913
1913
}
1914
1914
}
1915
1915
1916
+ let topLevelDependencies = root. packages. flatMap { $1. manifest. dependencies. map { $0. packageRef } }
1917
+
1916
1918
var requiredIdentities : OrderedCollections . OrderedSet < PackageReference > = [ ]
1917
1919
_ = transitiveClosure ( inputNodes) { node in
1918
1920
return node. manifest. dependenciesRequired ( for: node. productFilter) . compactMap { dependency in
@@ -1924,17 +1926,26 @@ extension Workspace {
1924
1926
if existing. canonicalLocation == package . canonicalLocation {
1925
1927
// same literal location is fine
1926
1928
if existing. locationString != package . locationString {
1927
- let preferred = [ existing, package ] . sorted ( by: {
1928
- $0. locationString > $1. locationString
1929
- } ) . first! // safe
1930
- observabilityScope. emit ( debug: """
1931
- similar variants of package ' \( package . identity) ' \
1932
- found at ' \( package . locationString) ' and ' \( existing. locationString) '. \
1933
- using preferred variant ' \( preferred. locationString) '
1934
- """ )
1935
- if preferred. locationString != existing. locationString {
1936
- requiredIdentities. remove ( existing)
1937
- requiredIdentities. insert ( preferred, at: index)
1929
+ // we prefer the top level dependencies
1930
+ if topLevelDependencies. contains ( where: { $0. locationString == existing. locationString } ) {
1931
+ observabilityScope. emit ( debug: """
1932
+ similar variants of package ' \( package . identity) ' \
1933
+ found at ' \( package . locationString) ' and ' \( existing. locationString) '. \
1934
+ using preferred root variant ' \( existing. locationString) '
1935
+ """ )
1936
+ } else {
1937
+ let preferred = [ existing, package ] . sorted ( by: {
1938
+ $0. locationString > $1. locationString
1939
+ } ) . first! // safe
1940
+ observabilityScope. emit ( debug: """
1941
+ similar variants of package ' \( package . identity) ' \
1942
+ found at ' \( package . locationString) ' and ' \( existing. locationString) '. \
1943
+ using preferred variant ' \( preferred. locationString) '
1944
+ """ )
1945
+ if preferred. locationString != existing. locationString {
1946
+ requiredIdentities. remove ( existing)
1947
+ requiredIdentities. insert ( preferred, at: index)
1948
+ }
1938
1949
}
1939
1950
}
1940
1951
} else {
@@ -3179,9 +3190,8 @@ extension Workspace {
3179
3190
// a required dependency that is already loaded (managed) should be represented in the pins store.
3180
3191
// also comparing location as it may have changed at this point
3181
3192
if requiredDependencies. contains ( where: { $0. equalsIncludingLocation ( dependency. packageRef) } ) {
3182
- let pin = pinsStore. pins [ dependency. packageRef. identity]
3183
3193
// if pin not found, or location is different (it may have changed at this point) pin it
3184
- if ! ( pin ? . packageRef . equalsIncludingLocation ( dependency. packageRef) ?? false ) {
3194
+ if pinsStore . pins [ comparingLocation : dependency. packageRef] == . none {
3185
3195
pinsStore. pin ( dependency)
3186
3196
}
3187
3197
} else if let pin = pinsStore. pins [ dependency. packageRef. identity] {
@@ -3747,7 +3757,7 @@ extension Workspace {
3747
3757
3748
3758
// only update if necessary
3749
3759
if !workingCopy. exists ( revision: revision) {
3750
- // The fetch operation may update contents of the checkout,
3760
+ // The fetch operation may update contents of the checkout,
3751
3761
// so we need to do mutable-immutable dance.
3752
3762
try self . fileSystem. chmod ( . userWritable, path: checkoutPath, options: [ . recursive, . onlyFiles] )
3753
3763
try workingCopy. fetch ( )
@@ -4632,3 +4642,12 @@ extension Workspace.ManagedDependencies {
4632
4642
} )
4633
4643
}
4634
4644
}
4645
+
4646
+ extension PinsStore . Pins {
4647
+ subscript( comparingLocation package : PackageReference ) -> PinsStore . Pin ? {
4648
+ if let pin = self [ package . identity] , pin. packageRef. equalsIncludingLocation ( package ) {
4649
+ return pin
4650
+ }
4651
+ return . none
4652
+ }
4653
+ }
0 commit comments