Skip to content
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

Report whether a target is part of the root package in the sourcekit-lsp API #7492

Merged
merged 1 commit into from May 1, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
42 changes: 33 additions & 9 deletions Sources/SourceKitLSPAPI/BuildDescription.swift
Expand Up @@ -22,31 +22,45 @@ import class Build.BuildPlan
import class Build.ClangTargetBuildDescription
import class Build.SwiftTargetBuildDescription
import struct PackageGraph.ResolvedModule
import struct PackageGraph.ModulesGraph
Copy link
Member

Choose a reason for hiding this comment

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

Is there a way to implement this with private import? All of the types above without private import should not leak outside of SourceKitLSPAPI and we should work on making this list smaller instead of adding more to it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I viewed it similarly to how we use ResolvedModule in the API as well.

I agree that we should not expose these types in SourceKitLSPAPI, but completely wrapping ModuleGraph will be a fairly significant undertaking (we use it in other places in SourceKit-LSP as well) and I think that should be done when we switch all of SourceKitLSP to only use the API module. Doing that for 6.0 is too much work and risk though, so I'd prefer to keep this as-is for now and schedule time for 6.1 to extend the SourceKitLSPAPI module so that we don't need to import any other modules. WDYT?

Copy link
Member

Choose a reason for hiding this comment

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

That's fair, although I would like ModulesGraph (and Resolved* types) to go through a significant refactoring for performance improvements in the near future, which very likely is going to be a breaking change. I'm not sure of the timeline yet, it might need to be included in 6.0 if we see performance regressing too much due to other changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's fair. If an abstraction layer for LSP falls out of that, that's great. Otherwise, I'm happy to assist with any changes that need to be done in SourceKit-LSP due to the refactoring.


public protocol BuildTarget {
var sources: [URL] { get }

/// Whether the target is part of the root package that the user opened or if it's part of a package dependency.
var isPartOfRootPackage: Bool { get }

func compileArguments(for fileURL: URL) throws -> [String]
}
}

private struct WrappedClangTargetBuildDescription: BuildTarget {
private let description: ClangTargetBuildDescription
let isPartOfRootPackage: Bool

init(description: ClangTargetBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
}

extension ClangTargetBuildDescription: BuildTarget {
public var sources: [URL] {
return (try? compilePaths().map { URL(fileURLWithPath: $0.source.pathString) }) ?? []
return (try? description.compilePaths().map { URL(fileURLWithPath: $0.source.pathString) }) ?? []
}

public func compileArguments(for fileURL: URL) throws -> [String] {
let filePath = try resolveSymlinks(try AbsolutePath(validating: fileURL.path))
let commandLine = try self.emitCommandLine(for: filePath)
let commandLine = try description.emitCommandLine(for: filePath)
// First element on the command line is the compiler itself, not an argument.
return Array(commandLine.dropFirst())
}
}

private struct WrappedSwiftTargetBuildDescription: BuildTarget {
private let description: SwiftTargetBuildDescription
let isPartOfRootPackage: Bool

init(description: SwiftTargetBuildDescription) {
init(description: SwiftTargetBuildDescription, isPartOfRootPackage: Bool) {
self.description = description
self.isPartOfRootPackage = isPartOfRootPackage
}

var sources: [URL] {
Expand All @@ -71,17 +85,27 @@ public struct BuildDescription {
}

// FIXME: should not use `ResolvedTarget` in the public interface
public func getBuildTarget(for target: ResolvedModule) -> BuildTarget? {
public func getBuildTarget(for target: ResolvedModule, in modulesGraph: ModulesGraph) -> BuildTarget? {
if let description = buildPlan.targetMap[target.id] {
switch description {
case .clang(let description):
return description
return WrappedClangTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
case .swift(let description):
return WrappedSwiftTargetBuildDescription(description: description)
return WrappedSwiftTargetBuildDescription(
description: description,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(description.package.id)
)
}
} else {
if target.type == .plugin, let package = self.buildPlan.graph.package(for: target) {
return PluginTargetBuildDescription(target: target, toolsVersion: package.manifest.toolsVersion)
return PluginTargetBuildDescription(
target: target,
toolsVersion: package.manifest.toolsVersion,
isPartOfRootPackage: modulesGraph.rootPackages.map(\.id).contains(package.id)
)
}
return nil
}
Expand Down
Expand Up @@ -21,11 +21,13 @@ private import class PackageModel.UserToolchain
struct PluginTargetBuildDescription: BuildTarget {
private let target: ResolvedModule
private let toolsVersion: ToolsVersion
let isPartOfRootPackage: Bool

init(target: ResolvedModule, toolsVersion: ToolsVersion) {
init(target: ResolvedModule, toolsVersion: ToolsVersion, isPartOfRootPackage: Bool) {
assert(target.type == .plugin)
self.target = target
self.toolsVersion = toolsVersion
self.isPartOfRootPackage = isPartOfRootPackage
}

var sources: [URL] {
Expand Down
12 changes: 8 additions & 4 deletions Tests/SourceKitLSPAPITests/SourceKitLSPAPITests.swift
Expand Up @@ -63,7 +63,8 @@ class SourceKitLSPAPITests: XCTestCase {
"-emit-dependencies",
"-emit-module",
"-emit-module-path", "/path/to/build/\(buildParameters.triple)/debug/exe.build/exe.swiftmodule"
]
],
isPartOfRootPackage: true
)
try description.checkArguments(
for: "lib",
Expand All @@ -73,7 +74,8 @@ class SourceKitLSPAPITests: XCTestCase {
"-emit-dependencies",
"-emit-module",
"-emit-module-path", "/path/to/build/\(buildParameters.triple)/debug/Modules/lib.swiftmodule"
]
],
isPartOfRootPackage: true
)
}
}
Expand All @@ -82,10 +84,11 @@ extension SourceKitLSPAPI.BuildDescription {
@discardableResult func checkArguments(
for targetName: String,
graph: ModulesGraph,
partialArguments: [String]
partialArguments: [String],
isPartOfRootPackage: Bool
) throws -> Bool {
let target = try XCTUnwrap(graph.allTargets.first(where: { $0.name == targetName }))
let buildTarget = try XCTUnwrap(self.getBuildTarget(for: target))
let buildTarget = try XCTUnwrap(self.getBuildTarget(for: target, in: graph))

guard let file = buildTarget.sources.first else {
XCTFail("build target \(targetName) contains no files")
Expand All @@ -96,6 +99,7 @@ extension SourceKitLSPAPI.BuildDescription {
let result = arguments.contains(partialArguments)

XCTAssertTrue(result, "could not match \(partialArguments) to actual arguments \(arguments)")
XCTAssertEqual(buildTarget.isPartOfRootPackage, isPartOfRootPackage)
return result
}
}