Jump to definition not working very well in VSCode

I just started using the Swift extension in VSCode and noticed if I CMD click or hover the definition is not found. Which is weird since that is listed as one of the features of the extension...
I found some information about adding server arguments to fix the situation, which does improve it a lot but still many definitions of methods within the project for some reason cannot be found. I'm yet to see a clear pattern and what works and what doesn't even. Are there any other server arguments that can be added to fix it even more?

"swift.sourcekit-lsp.serverArguments": [

Jump to definition only works once you have a built project. It also has to have been built with the same arguments as are passed to the LSP server. There is some support for doing this in the extension already. There is a command Swift: Select Target Platform.. which allows you to select an iOS platform. This builds with the correct SDK and setups the LSP server with this SDK as well.

There is one gotcha with this though, Apple do not support building iOS projects with SwiftPM. There are multiple issues, you have to clean the project when switching between platforms because they use the same folders, building tests doesn't appear to work.

On top of that LSP functionality does sometimes fail when using SourceKit-LSP from Swift 5.9. You can use the command Swift: Restart LSP Server in those situations to get it running again.

Thanks for the reply. So basically what you're saying is you need to build a project first for it to work but there isn't really any way to do that? So in fact jump to definition doesn't in fact work?

EDIT: It seems the project I'm testing is an xcode project when it should be a swift package. That's the main reason the jump to definition does not work well? As I said before, I'm new to swift.

How are you opening the project in VSCode? Do you have a Package.swift file?

Swift LSP requires building to work correctly. That has some implementation reasoning behind this, but I do not remember which exactly. Hopefully it could be fixed at some point, yet currently that is the state.

With Xcode projects (and basically all SDK dependant code) it is a lot worse situation. Swift LSP is concerned only about Swift, obviously :) Some iOS/macOS/etc app has set of its own dependencies and build artifacts. I have tried in light of NeoVim some solutions, that try to be-friend Xcode build artifacts, but it wasn't the best experience. So if you want to write iOS app, better to use Xcode. If not, all you need is to run swift test.

1 Like

No, it's an xcode project and that seems to be part of the problem. I didn't realise vscode couldn't deal with regular xcode projects.

Yeah that will cause issues. It's possible to build Xcode projects with some of the configuration that Adam spoke about, but the extension is very much focused on Swift packages

VSCode and SourceKit-LSP do not recognise Xcode projects. You have to create a Package.swift that includes all the files in your project if you want LSP features to work.