How to use SourceKit-lsp with iOS projects

I am trying to make SourceKit-LSP work on Neovim.
sourcekitd can successfully auto-complete the functions from the third party frameworks,
but it fails to find UIKit (no such module 'UIKit').

I have added the platform setting to the package.swift, but sourcekitd still fails to find UIKit.
What is the correct setting for iOS projects?

let package = Package(
name: "Hi",
platforms: [
.iOS(.v10)
],
products: [
.library(
name: "Hi",
targets: ["Hi"]),
],

Are you able to build this package using swift build? When I tried this it gives the same error. @Aciid, does the open source swiftpm (or libSwiftPM) support building for iOS?

Command-line SwiftPM only supports building for macOS.

Yes I can. Try this

swift build \
-Xswiftc "-sdk" -Xswiftc "xcrun --sdk iphonesimulator --show-sdk-path"
-Xswiftc "-target" -Xswiftc "x86_64-apple-ios12.1-simulator"

If you can configure your editor to launch sourcekit-lsp with extra command line options, you should be able to pass the same -Xswiftc options to sourcekit-lsp. I haven't tried this, but in principle it should work.

Edit: looking at sourcekit-lsp/Editors at main · apple/sourcekit-lsp · GitHub, I think you could add to the cmd field, although I'm not sure of the syntax.

        \ 'cmd': {server_info->['sourcekit-lsp']},

If this is a common use-case, we could look at more convenient ways to pass these external flags in so they can be per-project instead of global to the server configuration.

Doesn't work. I don't know why. The arguments work for swift build

let g:LanguageClient_serverCommands = {
\ 'swift': ['/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/sourcekit-lsp',
\ '-Xswiftc', '"-sdk"',
\ '-Xswiftc', '"/Applications/Xcode-beta.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator13.0.sdk"',
\ '-Xswiftc', '"-target"',
\ '-Xswiftc', '"x86_64-apple-ios12.1-simulator"']
\ }

let g:LanguageClient_rootMarkers = {
\ 'swift': ['READMD.md', '.git']
\ }
set completefunc=LanguageClient#complete

The SDE of vscode extension can get UIKit to work.
Maybe the problem comes from the LanguageClient-neovim.
I will do more tests

I tried this in Sublime text with the -Xswiftc flags and it worked. I think there must be something wrong in how the command is configured or with the lsp plugin not using those arguments when launching.

True. I think sourcekit-lsp can support iOS projects.
I was trying to make SDE of vscode work on UIKit.
The readme says it will work on UIKit, but it doesn't.
After a little bit of searching, I found SDE doesn't use the arguments from .vscode/settings.json.
Instead, it reads the arguments directly from .build/debug.yaml, which is generated by swift build
If I could apply build arguments for swift build, I can make SDE works on UIKit.

I have made it work. The summary is here (sorry, it's written in Chinese

SourceKit-lsp support BSP protocol to provide compiler options. I have developed a simple BSP server to make sourceKit-lsp use xcode's compile logs. currently it's basicly work like in a xcode. see GitHub - SolaWing/xcode-build-server: a build server protocol implementation for integrate xcode with sourcekit-lsp

4 Likes

Give it a go passing sourcekit-lsp.serverArguments: