I would still like to be able to use VSCode to draft Swift code on my Linux machine while I'm away from my Mac. But I can't get the sourcekit-lsp
to recognize the modules from the iOS SDK.
I have occasional access to a Mac but mostly use my Linux machine. As such, I can build apps on my Mac, but I would still like to be able to use VSCode to draft Swift code on my Linux machine ahead of time.
But I can't get the sourcekit-lsp
to recognize the modules from the iOS SDK. Similar to references.d.ts
for TypeScript devs, but with swift, and only VSCode/sourcekit-lsp cares about it.
I've dug around Xcode.app
and found various .modulemap
and .swiftinterface
files that, after looking in a text editor, look like they provide what I want, but I don't know how to get sourcekit-lsp
to look at them.
To emphasize, I do not expect or want to be able to compile iOS code on a Linux machine. In my troubleshooting, I have used
swift build
only to find more meaningful errors. I don't expect actual builds to happen - that's what the Mac is for. I simply want to be able to use autocompletions from native modules, like UIKit, in VSCode, so I can later copy it over to the Mac to test it.
I know, it's a weird workflow but it's what I have access to right now.
Setup
I've downloaded the Xcode 11 beta and extracted it so I can access the frameworks/SDKs. However, I can't seem to find a way to get sourcekit-lsp
to understand the headers for the various frameworks.
Also, swift is installed under /usr/local/bin/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-21-a-ubuntu18.04
.
Since sourcekit-lsp
doesn't log many errors, I decided to try swift build
over and over again, getting as close to "working" as possible until hopefully sourcekit-lsp
would pick something up.
Trial and error
So far I have tried many different commands. But I'll follow the best line of progress I have gotten.
I started by pointing swiftc
to my SDK and iOS target using -sdk Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target arm64-apple-ios13.0
This fixed the module not found errors (when I ran swift build
)
<unknown>:0: error: unable to load standard library for target 'arm64-apple-ios13.0'
I guessed that this was the swift libraries not being included, so I copied Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos
to /usr/local/bin/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-21-a-ubuntu18.04/usr/lib/swift/iphoneos
, which does indeed fix the above error.
Where I'm stuck
Now, swift build
gives me a new error:
<unknown>:0: error: compiled module was created by an older version of the compiler; rebuild 'Swift' and try again: /usr/local/bin/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-21-a-ubuntu18.04/usr/lib/swift/iphoneos/Swift.swiftmodule/arm64.swiftmodule
Additionally, going back to VSCode, hovering over a module import (like Foundation
) yields a different error:
error response (Request Failed): did not find primary SourceFile
I've tried without success to fix these errors. I don't need swift build
to work but I just want sourcekit-lsp
to let me write code that uses UIKit/etc.
Package.swift
// swift-tools-version:5.0
// The swift-tools-version declares the minimum version of Swift required to build this package.
import PackageDescription
let package = Package(
name: "Example",
platforms: [
.iOS("13.0")
],
products: [
// Products define the executables and libraries produced by a package, and make them visible to other packages.
.library(
name: "Example",
targets: ["Example"]),
],
dependencies: [
// Dependencies declare other packages that this package depends on.
// .package(url: /* package url */, from: "1.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
// Targets can depend on other targets in this package, and on products in packages which this package depends on.
.target(
name: "Example",
dependencies: [], swiftSettings: [SwiftSetting.unsafeFlags(["-sdk", "/why/doesnt/this/work/Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk", "-target", "arm64-apple-ios13.0"])])
]
)