Skip to content

Return compiler arguments for invalid package manifests #8138

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
Dec 9, 2024
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
38 changes: 14 additions & 24 deletions Sources/Workspace/Workspace+Manifests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -384,33 +384,23 @@ extension Workspace {
}

/// Returns manifest interpreter flags for a package.
// TODO: should this be throwing instead?
public func interpreterFlags(for packagePath: AbsolutePath) -> [String] {
do {
guard let manifestLoader = self.manifestLoader as? ManifestLoader else {
throw StringError("unexpected manifest loader kind")
}
public func interpreterFlags(for manifestPath: AbsolutePath) throws -> [String] {
guard let manifestLoader = self.manifestLoader as? ManifestLoader else {
throw StringError("unexpected manifest loader kind")
}

let manifestPath = try ManifestLoader.findManifest(
packagePath: packagePath,
fileSystem: self.fileSystem,
currentToolsVersion: self.currentToolsVersion
)
let manifestToolsVersion = try ToolsVersionParser.parse(
manifestPath: manifestPath,
fileSystem: self.fileSystem
)
let manifestToolsVersion = (try? ToolsVersionParser.parse(
manifestPath: manifestPath,
fileSystem: self.fileSystem
)) ?? self.currentToolsVersion

guard self.currentToolsVersion >= manifestToolsVersion || SwiftVersion.current.isDevelopment,
manifestToolsVersion >= ToolsVersion.minimumRequired
else {
throw StringError("invalid tools version")
}
return manifestLoader.interpreterFlags(for: manifestToolsVersion)
} catch {
// We ignore all failures here and return empty array.
return []

guard self.currentToolsVersion >= manifestToolsVersion || SwiftVersion.current.isDevelopment,
manifestToolsVersion >= ToolsVersion.minimumRequired
else {
throw StringError("invalid tools version")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can you include the invalid tools version in the string to make the issue easier to debug?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this error path also have corresponding test coverage here?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just shuffled code around and I don’t know how I would trigger this error scenario.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Addressed in #8172

}
return manifestLoader.interpreterFlags(for: manifestToolsVersion)
}

/// Load the manifests for the current dependency tree.
Expand Down
21 changes: 17 additions & 4 deletions Tests/WorkspaceTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -159,9 +159,10 @@ final class WorkspaceTests: XCTestCase {

try testWithTemporaryDirectory { path in
let foo = path.appending("foo")
let packageManifest = foo.appending("Package.swift")

func createWorkspace(_ content: String) throws -> Workspace {
try fs.writeFileContents(foo.appending("Package.swift"), string: content)
try fs.writeFileContents(packageManifest, string: content)

let manifestLoader = ManifestLoader(toolchain: try UserToolchain.default)

Expand All @@ -185,7 +186,7 @@ final class WorkspaceTests: XCTestCase {
"""
)

XCTAssertMatch(ws.interpreterFlags(for: foo), [.equal("-swift-version"), .equal("4")])
XCTAssertMatch(try ws.interpreterFlags(for: packageManifest), [.equal("-swift-version"), .equal("4")])
}

do {
Expand All @@ -199,7 +200,7 @@ final class WorkspaceTests: XCTestCase {
"""
)

XCTAssertEqual(ws.interpreterFlags(for: foo), [])
XCTAssertThrowsError(try ws.interpreterFlags(for: packageManifest))
}

do {
Expand All @@ -213,7 +214,19 @@ final class WorkspaceTests: XCTestCase {
"""
)

XCTAssertMatch(ws.interpreterFlags(for: foo), [.equal("-swift-version"), .equal("6")])
XCTAssertMatch(try ws.interpreterFlags(for: packageManifest), [.equal("-swift-version"), .equal("6")])
}

do {
// Invalid package manifest should still produce build settings.
let ws = try createWorkspace(
"""
// swift-tools-version:999.0
import PackageDescription
"""
)

XCTAssertMatch(try ws.interpreterFlags(for: packageManifest), [.equal("-package-description-version")])
}
}
}
Expand Down