swiftlang / sourcekit-lsp

Language Server Protocol implementation for Swift and C-based languages
Apache License 2.0
3.26k stars 271 forks source link

"No such module" when importing foreign package even though build succeeds #1040

Closed benjiwolff closed 6 months ago

benjiwolff commented 7 months ago

Steps to reproduce:

mkdir LspTest
cd LspTest
swift package init

Add a package to Package.swift e.g.:

// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
        name: "LspTest",
        platforms: [.macOS(.v12)],
        products: [
        // Products define the executables and libraries a package produces, making them visible to other packages.
        .library(
            name: "LspTest",
            targets: ["LspTest"]),
        ],
        dependencies: [
        .package(url: "git@github.com:stevengharris/SplitView.git", from: "3.5.0"),
        ],
        targets: [
        // Targets are the basic building blocks of a package, defining a module or a test suite.
        // Targets can depend on other targets in this package and products from dependencies.
        .target(
            name: "LspTest",
            dependencies: [.product(name: "SplitView", package: "SplitView")]
            ),
        .testTarget(
            name: "LspTestTests",
            dependencies: ["LspTest"]),
        ]
        )

Import the module in a .swift file e.g. in Sources/LspTest/LspTest.swift:

// The Swift Programming Language
// https://docs.swift.org/swift-book

import SplitView

sourcekit-lsp returns "No such module" e.g. in Neovim:

image

Hint

This seems to be a new bug in the development version of sourcekit-lsp. Using the release version, I do not encounter this problem.

ahoppen commented 7 months ago

Tracked in Apple’s issue tracker as rdar://121696265

ahoppen commented 7 months ago

Do you still see the issue after building the package? sourcekit-lsp requires a build to find the modules to import.

benjiwolff commented 7 months ago

Yes, the problem persists after deleting the .build folder and building both the debug and release configurations.

ahoppen commented 7 months ago

Hmm, that’s very odd. Maybe we aren’t finding the build folder. Could you check if the problem persists when you:

  1. Run swift build and wait for it to finish
  2. Make an edit in the file that contains import SplitView
  3. Restart sourcekit-lsp
benjiwolff commented 7 months ago

I followed the steps. I simply added an "x" on a new lined and saved the file before restarting Neovim (restarts lsp too). The issue persists.

image
ahoppen commented 7 months ago

Ah, I think I know what might be going on: Do you use sourcekit-lsp from the same toolchain snapshot that contains your swift build or are they from different toolchain version, eg. because you built sourcekit-lsp from source on main? SwiftPM moved the folder that contains the .swiftmodules, changing the compiler arguments that are required to build the file. Since SourceKit-LSP contains its own copy of SwiftPM, the compiler arguments it gets might not be correct to correctly pick up the Swift modules. https://github.com/apple/sourcekit-lsp/pull/1046/files#diff-d58fae5b483fc1623905094d04af6418aa40c2a77652889be608feb158d15a7dR142-R147

Generally speaking, sourcekit-lsp should be used from the same toolchain that the Swift compiler is from. While mixing and matching sourcekit-lsp with different compiler versions often works, there’s no guarantee that it does.

benjiwolff commented 6 months ago

Spot on! I did use the release version of swift (5.9) to build. Now that I build the module with the dev version that my sourcekit-lsp uses, I no longer get the missing module errors :)

image