Skip to content

Commit bb5bcb2

Browse files
committed
Return compiler arguments for invalid package manifests
Currently, when there‘s a syntax error in a package manifest, we don’t get any build settings from it in SourceKit-LSP and thus loose almost all semantic functionality. If we can’t parse the package manifest, fall back to providing build settings by assuming it has the current Swift tools version. Currently, when there‘s a syntax error in a package manifest, we don’t get any build settings from it in SourceKit-LSP and thus loose almost all semantic functionality. If we can’t parse the package manifest, fall back to providing build settings by assuming it has the current Swift tools version. Fixes #1704 rdar://136423767
1 parent 6c5f5c9 commit bb5bcb2

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

Sources/BuildSystemIntegration/SwiftPMBuildSystem.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -756,7 +756,7 @@ package actor SwiftPMBuildSystem: BuiltInBuildSystem {
756756

757757
/// Retrieve settings for a package manifest (Package.swift).
758758
private func settings(forPackageManifest path: AbsolutePath) throws -> TextDocumentSourceKitOptionsResponse? {
759-
let compilerArgs = swiftPMWorkspace.interpreterFlags(for: path.parentDirectory) + [path.pathString]
759+
let compilerArgs = try swiftPMWorkspace.interpreterFlags(for: path) + [path.pathString]
760760
return TextDocumentSourceKitOptionsResponse(compilerArguments: compilerArgs)
761761
}
762762
}

Tests/BuildSystemIntegrationTests/SwiftPMBuildSystemTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1103,6 +1103,39 @@ final class SwiftPMBuildSystemTests: XCTestCase {
11031103
XCTAssertEqual(end.token, begin.token)
11041104
XCTAssertEqual(end.value, .end(WorkDoneProgressEnd()))
11051105
}
1106+
1107+
func testBuildSettingsForInvalidManifest() async throws {
1108+
try await withTestScratchDir { tempDir in
1109+
try FileManager.default.createFiles(
1110+
root: tempDir,
1111+
files: [
1112+
"pkg/Sources/lib/a.swift": "",
1113+
"pkg/Package.swift": """
1114+
// swift-tools-version: 4.2
1115+
import PackageDescription
1116+
""",
1117+
]
1118+
)
1119+
let packageRoot = try tempDir.appendingPathComponent("pkg").realpath
1120+
let manifestURL = packageRoot.appendingPathComponent("Package.swift")
1121+
let buildSystemManager = await BuildSystemManager(
1122+
buildSystemSpec: BuildSystemSpec(kind: .swiftPM, projectRoot: packageRoot),
1123+
toolchainRegistry: .forTesting,
1124+
options: SourceKitLSPOptions(),
1125+
connectionToClient: DummyBuildSystemManagerConnectionToClient(),
1126+
buildSystemTestHooks: BuildSystemTestHooks()
1127+
)
1128+
await buildSystemManager.waitForUpToDateBuildGraph()
1129+
let settings = await buildSystemManager.buildSettingsInferredFromMainFile(
1130+
for: DocumentURI(manifestURL),
1131+
language: .swift,
1132+
fallbackAfterTimeout: false
1133+
)
1134+
let compilerArgs = try XCTUnwrap(settings?.compilerArguments)
1135+
XCTAssert(compilerArgs.contains("-package-description-version"))
1136+
XCTAssert(compilerArgs.contains(try manifestURL.filePath))
1137+
}
1138+
}
11061139
}
11071140

11081141
private func assertArgumentsDoNotContain(

0 commit comments

Comments
 (0)