swiftlang / sourcekit-lsp

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

Phantom errors, caching issues. #1495

Closed adamwych closed 1 week ago

adamwych commented 2 weeks ago

Swift version

swiftlang-5.10.0.13, 6.0-dev (490cf64aee23f13)

Platform

macOS 14.3.1

Editor

VSCode with Swift extension 1.10.2

Does the issue reproduce with Swift 6?

Yes

Description

LSP (in my case running in VSCode) very often reports errors, that do not in fact exist in the code.

The context here is an empty project, just a "Hello, world":

image

Here you can see that it complains about identifier v10_14 not being in scope, when .v13 is used (I did put v10_14 there before, the issue here is that it didn't update).

image

After multiple removals of .build, restarts and re-builds, it finally got it - but then I changed swift-tools-version to 5.6 and it started reporting that v13 is not available yet - okay, I bumped the version to 5.8 and it keeps reporting the same error:

image

I also encountered issues in another project where the LSP was reporting that a class member did not exist, while at the same time showing me the definition of that field right below the error...

Note that after I add a product and target, the package will build just fine when running swift build, there are no compilation errors, but the LSP will continue to report those kinds of errors.

For me, it happens very often when working on Swift code and it makes it a chore rather than a pleasure. There is no way to get rid of those phantom errors other than by removing the .build, restarting the editor, reloading LSP and hoping that finally it will work - in case of bigger projects, fully re-building them all the time is obviously terrible and sometimes it doesn't even work! Sometimes I need to fully re-build them multiple times, which makes no sense.

Steps to Reproduce

  1. Create a folder and a Package.swift file.
  2. Put this into Package.swift:
    
    // swift-tools-version:5.9
    import PackageDescription

let package = Package( name: "MyPackage", platforms: [ .macOS(.v13) ] )


3. Change version on the top to 5.6 and save the file.
4. Change version back to 5.9 and save the file.
5. **Change the tab to another file** and the error will pop up.

### Logging

_No response_
ahoppen commented 2 weeks ago

Synced to Apple’s issue tracker as rdar://130144910

adamwych commented 2 weeks ago

FYI, I enabled logging in the VSCode extension to look at what requests are sent to the LSP and it looks like LSP does not report any error there, so (in this case) it might actually be a bug in the extension and I also reported this issue there.

[Trace - 1:43:15 PM] Sending request 'textDocument/hover - (123)'.
Params: {
    "textDocument": {
        "uri": "<< path to project >>/Package.swift"
    },
    "position": {
        "line": 7,
        "character": 6
    }
}

[Trace - 1:43:15 PM] Received response 'textDocument/hover - (123)' in 23ms.
No result returned.
ahoppen commented 1 week ago

It looks like the diagnostics you see lingering around are from compilation, not SourceKit-LSP (it shows swiftc after the error message, not SourceKit or sourcekitd) and those diagnostics are managed by the Swift extension for VS Code. I can’t move this issue to the VS Code extension repo at the moment, because it’s a different organization, so I filed an equivalent issue over there: https://github.com/swiftlang/vscode-swift/issues/920.