sourcekit-lsp icon indicating copy to clipboard operation
sourcekit-lsp copied to clipboard

[Question] iOS support

Open wojciech-kulik opened this issue 3 years ago • 12 comments

Is there any way to set up the LSP to work with iOS apps? Specifically with iOS SPM packages? I have a project which is based on SPM modularized architecture.

I have found a way to compile packages for iOS using the following command:

 xcodebuild -scheme PackageName -destination 'platform=iOS Simulator,OS=16.4,name=iPhone 14'

However, when I try to develop some classes in my editor I get these errors in LSP logs:

[ERROR][2023-09-14 12:02:28] .../vim/lsp/rpc.lua:734	"rpc"	"/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"	"stderr"	"[2023-09-14 12:02:28.905] [error]: the library 'SomDependency' requires macos 10.13, but depends on the product 'Kingfisher' which requires macos 10.14; consider changing the library 'SomDependency' to require macos 10.14 or later, or the product 'Kingfisher' to require macos 10.13 or earlier.\n"

It looks like the LSP is trying to build the project for macOS. I tried configuring LSP like this:

    lspconfig["sourcekit"].setup({
      capabilities = capabilities,
      on_attach = on_attach,
      cmd = { "/Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp",
        "-Xswiftc", "-sdk",
        "-Xswiftc", "/Users/xyz/Downloads/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator.sdk",
        "-Xswiftc", "-target",
        "-Xswiftc", "arm64-apple-ios16.4-simulator" },
    })

but I get the same errors when I try to work with my Swift files. I would like to be able to work on my project in Neovim or VSCode.

wojciech-kulik avatar Sep 14 '23 10:09 wojciech-kulik

There's currently no way to make LSP work with cross-compilation.

MaxDesiatov avatar Sep 14 '23 10:09 MaxDesiatov

😢 so it looks like iOS developers are doomed to use Xcode forever as the only IDE 😭

wojciech-kulik avatar Sep 14 '23 10:09 wojciech-kulik

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

ahoppen avatar Sep 14 '23 10:09 ahoppen

@wojciech-kulik you can try my tool xcode-build-server to bind xcode project compile info to sourcekit-lsp.

SolaWing avatar Sep 16 '23 17:09 SolaWing

There's currently no way to make LSP work with cross-compilation.

So -sdk and -target do not work? I've had UIKit, SwiftUI completion with these parameters. It becomes problematic when you need to build a full project. There are a number of hurdles there, but breaking everything off into libraries and compiling them using swift CLI using same parameters generally works (as long as you aren't building tests).

adam-fowler avatar Sep 16 '23 18:09 adam-fowler

These issues might be helpful! (Signed, a stubborn Sublime user)

#730 - xcodebuild support for sourcekit-lsp https://github.com/apple/swift-package-manager/issues/4328 - SPM multiple targets https://github.com/apple/swift-package-manager/issues/5619 - SPM unknownOS in -triple

peterkos avatar Sep 19 '23 04:09 peterkos

@wojciech-kulik you can try my tool xcode-build-server to bind xcode project compile info to sourcekit-lsp.

Thank you! It seems to work! I'm only struggling with root_dir detection by lspconfig. It doesn't work properly when my xcodeproj is not where .git is located. Any advice on how to find it?

I tried:

      root_dir = function(filename, _)
        local git_root
        git_root = lspconfig.util.find_git_ancestor(filename)

        local handle = io.popen("find \"" .. git_root .. "\" -type d -name \"*.xcodeproj\" | tail -n 1 | xargs dirname")
        local result = handle:read("*a")
        handle:close()

        return result
      end,

but for some reasons, it doesn't work.

wojciech-kulik avatar Sep 25 '23 18:09 wojciech-kulik

@wojciech-kulik may be you should use handle:read("*l")? "*a" return all output, which end with a invalid "\n".

anyway, the generated buildServer.json can be any dir, just ensure it same with your lsp root, it not have to in same dir as xcodeproj.

SolaWing avatar Sep 26 '23 06:09 SolaWing

@SolaWing it helped, thank you!

wojciech-kulik avatar Sep 27 '23 21:09 wojciech-kulik

I’m closing this since it’s the same underlying issue that we’re already tracking in #730.

ahoppen avatar Oct 04 '23 17:10 ahoppen

I described how to set up Neovim and VS Code here: https://wojciechkulik.pl/ios/how-to-develop-ios-and-macos-apps-in-other-ides-like-neovim-or-vs-code

wojciech-kulik avatar Oct 15 '23 15:10 wojciech-kulik

@wojciech-kulik may be you should use handle:read("*l")? "*a" return all output, which end with a invalid "\n".

anyway, the generated buildServer.json can be any dir, just ensure it same with your lsp root, it not have to in same dir as xcodeproj.

Had the same problem, was keeping buildServer.json file in the same dir as the workspace, then moved it to the .git directory, and resolved the issue. Thank you

zeyrie avatar Oct 20 '24 11:10 zeyrie