@@ -583,8 +583,6 @@ extension ManifestLoader {
583583 }
584584 }
585585
586- // Otherwise, check if there is a version-specific manifest that has
587- // a higher tools version than the main Package.swift file.
588586 let contents : [ String ]
589587 do { contents = try fileSystem. getDirectoryContents ( packagePath) } catch {
590588 throw ToolsVersionParser . Error. inaccessiblePackage ( path: packagePath, reason: String ( describing: error) )
@@ -607,39 +605,47 @@ extension ManifestLoader {
607605
608606 let regularManifest = packagePath. appending ( component: Manifest . filename)
609607
610- // Find the newest version-specific manifest that is compatible with the the current tools version.
611- if let versionSpecificCandidate = versionSpecificManifests. keys. sorted ( by: > ) . first ( where: { $0 <= currentToolsVersion } ) {
608+ // Try to get the tools version of the regular manifest. As the comment marker is missing, we default to
609+ // tools version 3.1.0 (as documented).
610+ let regularManifestToolsVersion : ToolsVersion
611+ do {
612+ regularManifestToolsVersion = try ToolsVersionParser . parse ( manifestPath: regularManifest, fileSystem: fileSystem)
613+ }
614+ catch let error as UnsupportedToolsVersion where error. packageToolsVersion == . v3 {
615+ regularManifestToolsVersion = . v3
616+ }
612617
613- let versionSpecificManifest = packagePath. appending ( component: versionSpecificManifests [ versionSpecificCandidate] !)
618+ // Find the newest version-specific manifest that is compatible with the current tools version.
619+ guard let versionSpecificCandidate = versionSpecificManifests. keys. sorted ( by: > ) . first ( where: { $0 <= currentToolsVersion } ) else {
620+ // Otherwise, return the regular manifest.
621+ return regularManifest
622+ }
614623
615- // SwiftPM 4 introduced tools-version designations; earlier packages default to tools version 3.1.0.
616- // See https://swift.org/blog/swift-package-manager-manifest-api-redesign.
617- let versionSpecificManifestToolsVersion : ToolsVersion
618- if versionSpecificCandidate < . v4 {
619- versionSpecificManifestToolsVersion = . v3
620- }
621- else {
622- versionSpecificManifestToolsVersion = try ToolsVersionParser . parse ( manifestPath: versionSpecificManifest, fileSystem: fileSystem)
623- }
624+ let versionSpecificManifest = packagePath. appending ( component: versionSpecificManifests [ versionSpecificCandidate] !)
624625
625- // Try to get the tools version of the regular manifest. At the comment marker is missing, we default to
626- // tools version 3.1.0 (as documented) .
627- let regularManifestToolsVersion : ToolsVersion
628- do {
629- regularManifestToolsVersion = try ToolsVersionParser . parse ( manifestPath : regularManifest , fileSystem : fileSystem )
630- }
631- catch let error as UnsupportedToolsVersion where error . packageToolsVersion == . v3 {
632- regularManifestToolsVersion = . v3
633- }
626+ // SwiftPM 4 introduced tools- version designations; earlier packages default to tools version 3.1.0.
627+ // See https://swift.org/blog/swift-package-manager-manifest-api-redesign .
628+ let versionSpecificManifestToolsVersion : ToolsVersion
629+ if versionSpecificCandidate < . v4 {
630+ versionSpecificManifestToolsVersion = . v3
631+ }
632+ else {
633+ versionSpecificManifestToolsVersion = try ToolsVersionParser . parse ( manifestPath : versionSpecificManifest , fileSystem : fileSystem )
634+ }
634635
635- // Compare the tools version of this manifest with the regular
636- // manifest and use the version-specific manifest if it has
637- // a greater tools version.
638- if versionSpecificManifestToolsVersion > regularManifestToolsVersion {
636+ // Compare the tools version of this manifest with the regular
637+ // manifest and use the version-specific manifest if it has
638+ // a greater tools version.
639+ if versionSpecificManifestToolsVersion > regularManifestToolsVersion {
640+ return versionSpecificManifest
641+ } else {
642+ // If there's no primary candidate, validate the regular manifest.
643+ if regularManifestToolsVersion. validateToolsVersion ( currentToolsVersion) {
644+ return regularManifest
645+ } else {
646+ // If that's incompatible, use the closest version-specific manifest we got.
639647 return versionSpecificManifest
640648 }
641649 }
642-
643- return regularManifest
644650 }
645651}
0 commit comments