How to make sourcekit-lsp work with more than one local source file?

I'm using a Mac, but I'd rather use an editor other than Xcode because of having to resign it to use the XVim2 plugin. I've tried VS Code with the Maintained Swift Development Environment plugin, and vim, but they both have the same error. sourcekit-lsp does seem to work, but it only seems to be aware of one local source file at a time. One of my source files defines a protocol and another source file defines a class which implements it. Xcode itself is OK with that, but Code and vim both highlight the point where the class tries to implement the protocol, with the error "cannot find type 'TextureManager' in scope" (the protocol is named TextureManager). Even if I put both files in the same folder it doesn't fix it. It seems like I need to set up some sort of config file so that sourcekit-lsp can find all my sources, but I can't find any documentation about how to do that.

1 Like

As far as I understand, you'll have to create a Package.swift manifest, as SourceKit LSP uses swift build (or en equivalent call to libSwiftPM directly) to produce an index for these files.

As far as I understand, you'll have to create a Package.swift manifest, as SourceKit LSP uses swift build to produce an index for these files.

More importantly than the index, sourcekit-lsp needs to be able to figure out the compiler arguments for your code, which is what tells us what files belong in which modules, etc. Using a swiftpm package with a Package.swift file is the best way to do that today. We also support clang-compatible compilation databases (compile_commands.json), but I'm not sure if anyone is using that in production for Swift code - it's more commonly used in C/C++ projects, and I'm not aware of any tools that will produce one automatically for swift code.

1 Like

Thanks. I did see one or two articles or search results that vaguely hinted that the lsp might work if there was a Package.swift present, but nothing definitive. I wanted to be sure before I invested the time in learning how to set that up.