Skip to content

[6.0] Fix incorrect ConfiguredTargets for Package.swift w/ multiple workspaces #1566

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
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
8 changes: 5 additions & 3 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ extension SwiftPMBuildSystem {
)

self.fileToTargets = [DocumentURI: [SwiftBuildTarget]](
modulesGraph.allTargets.flatMap { target in
modulesGraph.allModules.flatMap { target in
return target.sources.paths.compactMap { (filePath) -> (key: DocumentURI, value: [SwiftBuildTarget])? in
guard let buildTarget = buildDescription.getBuildTarget(for: target, in: modulesGraph) else {
return nil
Expand All @@ -412,7 +412,7 @@ extension SwiftPMBuildSystem {
)

self.sourceDirToTargets = [DocumentURI: [SwiftBuildTarget]](
modulesGraph.allTargets.compactMap { (target) -> (DocumentURI, [SwiftBuildTarget])? in
modulesGraph.allModules.compactMap { (target) -> (DocumentURI, [SwiftBuildTarget])? in
guard let buildTarget = buildDescription.getBuildTarget(for: target, in: modulesGraph) else {
return nil
}
Expand Down Expand Up @@ -545,7 +545,9 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
return targets.map(ConfiguredTarget.init)
}

if path.basename == "Package.swift" {
if path.basename == "Package.swift"
&& projectRoot == (try? TSCBasic.resolveSymlinks(TSCBasic.AbsolutePath(path.parentDirectory)))
{
// We use an empty target name to represent the package manifest since an empty target name is not valid for any
// user-defined target.
return [ConfiguredTarget.forPackageManifest]
Expand Down
43 changes: 43 additions & 0 deletions Tests/SourceKitLSPTests/WorkspaceTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,49 @@ final class WorkspaceTests: XCTestCase {
XCTAssertEqual(diags, .full(RelatedFullDocumentDiagnosticReport(items: [])))
}

func testCorrectWorkspaceForPackageSwiftInMultiSwiftPMWorkspaceSetup() 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 pkgA = DocumentURI(
project.scratchDirectory
.appendingPathComponent("PackageA")
.appendingPathComponent("Package.swift")
)

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

assertEqual(
await project.testClient.server.workspaceForDocument(uri: pkgA)?.rootUri,
DocumentURI(project.scratchDirectory.appendingPathComponent("PackageA"))
)

assertEqual(
await project.testClient.server.workspaceForDocument(uri: pkgB)?.rootUri,
DocumentURI(project.scratchDirectory.appendingPathComponent("PackageB"))
)
}

func testSwiftPMPackageInSubfolder() async throws {
let packageManifest = """
// swift-tools-version: 5.7
Expand Down