Skip to content

Return fileHandlingCapability.handled for all files that SwiftPMBuildSystem has build settings for #1232

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
May 8, 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
17 changes: 9 additions & 8 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
public var indexPrefixMappings: [PathPrefixMapping] { return [] }

public func buildSettings(for uri: DocumentURI, language: Language) throws -> FileBuildSettings? {
// SwiftPMBuildSystem doesn't respect the langue specified by the editor.
return try buildSettings(for: uri)
}

private func buildSettings(for uri: DocumentURI) throws -> FileBuildSettings? {
guard let url = uri.fileURL else {
// We can't determine build settings for non-file URIs.
return nil
Expand All @@ -335,7 +340,7 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
}

if path.extension == "h" {
return try settings(forHeader: path, language)
return try settings(forHeader: path)
}

return nil
Expand Down Expand Up @@ -432,14 +437,10 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
}

public func fileHandlingCapability(for uri: DocumentURI) -> FileHandlingCapability {
guard let fileUrl = uri.fileURL else {
return .unhandled
}
if (try? buildTarget(for: AbsolutePath(validating: fileUrl.path))) != nil {
if (try? buildSettings(for: uri)) != nil {
return .handled
} else {
return .unhandled
}
return .unhandled
}

public func sourceFiles() -> [SourceFileInfo] {
Expand Down Expand Up @@ -491,7 +492,7 @@ extension SwiftPMBuildSystem {
/// file.
/// This is safe because all files within one target have the same build settings except for reference to the file
/// itself, which we are replacing.
private func settings(forHeader path: AbsolutePath, _ language: Language) throws -> FileBuildSettings? {
private func settings(forHeader path: AbsolutePath) throws -> FileBuildSettings? {
func impl(_ path: AbsolutePath) throws -> FileBuildSettings? {
var dir = path.parentDirectory
while !dir.isRoot {
Expand Down
33 changes: 33 additions & 0 deletions Tests/SourceKitLSPTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,39 @@ final class WorkspaceTests: XCTestCase {
)
}

func testOpenPackageManifestInMultiSwiftPMWorkspaceSetup() async throws {
let project = try await MultiFileTestProject(
files: [
// PackageA
"PackageA/Sources/MyLibrary/libA.swift": "",
"PackageA/Package.swift": SwiftPMTestProject.defaultPackageManifest,

// PackageB
"PackageB/Sources/MyLibrary/libB.swift": "",
"PackageB/Package.swift": SwiftPMTestProject.defaultPackageManifest,
],
workspaces: { scratchDir in
return [
WorkspaceFolder(uri: DocumentURI(scratchDir)),
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("PackageA"))),
WorkspaceFolder(uri: DocumentURI(scratchDir.appendingPathComponent("PackageB"))),
]
}
)

let bPackageManifestUri = DocumentURI(
project.scratchDirectory.appendingPathComponent("PackageB").appendingPathComponent("Package.swift")
)

project.testClient.openDocument(SwiftPMTestProject.defaultPackageManifest, uri: bPackageManifestUri)

// Ensure that we get proper build settings for Package.swift and no error about `No such module: PackageDescription`
let diags = try await project.testClient.send(
DocumentDiagnosticsRequest(textDocument: TextDocumentIdentifier(bPackageManifestUri))
)
XCTAssertEqual(diags, .full(RelatedFullDocumentDiagnosticReport(items: [])))
}

func testSwiftPMPackageInSubfolder() async throws {
try await SkipUnless.swiftpmStoresModulesInSubdirectory()

Expand Down