Skip to content

Commit fb30a2f

Browse files
authored
Merge pull request #1232 from ahoppen/package-swift-handled
Return `fileHandlingCapability.handled` for all files that `SwiftPMBuildSystem` has build settings for
2 parents f0d869f + f3f8860 commit fb30a2f

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,11 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
315315
public var indexPrefixMappings: [PathPrefixMapping] { return [] }
316316

317317
public func buildSettings(for uri: DocumentURI, language: Language) throws -> FileBuildSettings? {
318+
// SwiftPMBuildSystem doesn't respect the langue specified by the editor.
319+
return try buildSettings(for: uri)
320+
}
321+
322+
private func buildSettings(for uri: DocumentURI) throws -> FileBuildSettings? {
318323
guard let url = uri.fileURL else {
319324
// We can't determine build settings for non-file URIs.
320325
return nil
@@ -335,7 +340,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
335340
}
336341

337342
if path.extension == "h" {
338-
return try settings(forHeader: path, language)
343+
return try settings(forHeader: path)
339344
}
340345

341346
return nil
@@ -432,14 +437,10 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
432437
}
433438

434439
public func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
435-
guard let fileUrl = uri.fileURL else {
436-
return .unhandled
437-
}
438-
if (try? buildTarget(for: AbsolutePath(validating: fileUrl.path))) != nil {
440+
if (try? buildSettings(for: uri)) != nil {
439441
return .handled
440-
} else {
441-
return .unhandled
442442
}
443+
return .unhandled
443444
}
444445

445446
public func sourceFiles() -> [SourceFileInfo] {
@@ -491,7 +492,7 @@ extension SwiftPMBuildSystem {
491492
/// file.
492493
/// This is safe because all files within one target have the same build settings except for reference to the file
493494
/// itself, which we are replacing.
494-
private func settings(forHeader path: AbsolutePath, _ language: Language) throws -> FileBuildSettings? {
495+
private func settings(forHeader path: AbsolutePath) throws -> FileBuildSettings? {
495496
func impl(_ path: AbsolutePath) throws -> FileBuildSettings? {
496497
var dir = path.parentDirectory
497498
while !dir.isRoot {

Tests/SourceKitLSPTests/WorkspaceTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,39 @@ final class WorkspaceTests: XCTestCase {
161161
)
162162
}
163163

164+
func testOpenPackageManifestInMultiSwiftPMWorkspaceSetup() async throws {
165+
let project = try await MultiFileTestProject(
166+
files: [
167+
// PackageA
168+
"PackageA/Sources/MyLibrary/libA.swift": "",
169+
"PackageA/Package.swift": SwiftPMTestProject.defaultPackageManifest,
170+
171+
// PackageB
172+
"PackageB/Sources/MyLibrary/libB.swift": "",
173+
"PackageB/Package.swift": SwiftPMTestProject.defaultPackageManifest,
174+
],
175+
workspaces: { scratchDir in
176+
return [
177+
WorkspaceFolder(uri: DocumentURI(scratchDir)),
178+
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("PackageA"))),
179+
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("PackageB"))),
180+
]
181+
}
182+
)
183+
184+
let bPackageManifestUri = DocumentURI(
185+
project.scratchDirectory.appendingPathComponent("PackageB").appendingPathComponent("Package.swift")
186+
)
187+
188+
project.testClient.openDocument(SwiftPMTestProject.defaultPackageManifest, uri: bPackageManifestUri)
189+
190+
// Ensure that we get proper build settings for Package.swift and no error about `No such module: PackageDescription`
191+
let diags = try await project.testClient.send(
192+
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bPackageManifestUri))
193+
)
194+
XCTAssertEqual(diags, .full(RelatedFullDocumentDiagnosticReport(items: [])))
195+
}
196+
164197
func testSwiftPMPackageInSubfolder() async throws {
165198
try await SkipUnless.swiftpmStoresModulesInSubdirectory()
166199

0 commit comments

Comments
 (0)