Skip to content

Allow ToolsVersionParser to throw missing version tools error #8922

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

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 1 commit
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
4 changes: 0 additions & 4 deletions Sources/PackageLoading/ToolsVersionParser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,7 @@ public struct ToolsVersionParser {
throw ManifestParseError.emptyManifest(path: manifestPath)
}

do {
return try self.parse(utf8String: manifestContentsDecodedWithUTF8)
} catch Error.malformedToolsVersionSpecification(.commentMarker(.isMissing)) {
throw UnsupportedToolsVersion(packageIdentity: .init(path: manifestPath), currentToolsVersion: .current, packageToolsVersion: .v3)
}
}

public static func parse(utf8String: String) throws -> ToolsVersion {
Expand Down
25 changes: 25 additions & 0 deletions Tests/PackageLoadingTests/ToolsVersionParserTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,31 @@ final class ToolsVersionParserTests: XCTestCase {
}
}

/// Verifies that the correct error is thrown for each Swift tools version specification missing entirely..
func testMissingSwiftToolsVersion() throws {
let manifestSnippetsWithoutVersionSpecifier = [
"\n import PackageDescription",
]

// TODO bp update comments here, as this was copied from above test
// seems that this was introduced in https://github.com/swiftlang/swift-package-manager/pull/4302
for manifestSnippet in manifestSnippetsWithoutVersionSpecifier {
XCTAssertThrowsError(
try self.parse(manifestSnippet),
"a 'ToolsVersionLoader.Error' should've been thrown, because the version specifier is missing from the Swift tools version specification"
) { error in
guard let error = error as? ToolsVersionParser.Error, case .malformedToolsVersionSpecification(.commentMarker(.isMissing)) = error else {
XCTFail("'ToolsVersionLoader.Error.malformedToolsVersionSpecification(.versionSpecifier(.isMissing))' should've been thrown, but a different error is thrown")
return
}
XCTAssertEqual(
error.description,
"the manifest is missing a Swift tools version specification; consider prepending to the manifest '\(ToolsVersion.current.specification())' to specify the current Swift toolchain version as the lowest Swift version supported by the project; if such a specification already exists, consider moving it to the top of the manifest, or prepending it with '//' to help Swift Package Manager find it"
)
}
}
}

/// Verifies that the correct error is thrown for each misspelt comment marker in Swift tools version specification.
func testMisspeltSpecificationCommentMarkers() throws {
let manifestSnippetsWithMisspeltSpecificationCommentMarker = [
Expand Down