4

当我离开 Mac 时,我仍然希望能够使用 VSCode 在我的 Linux 机器上编写 Swift 代码。但我无法sourcekit-lsp识别来自 iOS SDK 的模块。

我偶尔会使用 Mac,但主要使用我的 Linux 机器。因此,我可以在我的 Mac 上构建应用程序,但我仍然希望能够提前使用 VSCode 在我的 Linux 机器上起草 Swift 代码。

但我无法sourcekit-lsp识别来自 iOS SDK 的模块。与references.d.tsTypeScript 开发人员类似,但使用 swift,并且只有 VSCode/sourcekit-lsp 关心它。

我四处挖掘并Xcode.app发现了各种文件,在文本编辑器中查看后,它们看起来提供了我想要的东西,但我不知道如何查看它们。.modulemap.swiftinterfacesourcekit-lsp

需要强调的是,我不希望也不希望能够在 Linux 机器上编译iOS 代码。在我的故障排除中,我swift build只用来发现更有意义的错误。我不希望发生实际构建——这就是 Mac 的用途。我只是希望能够在 VSCode 中使用来自本机模块(如 UIKit)的自动完成功能,以便稍后将其复制到 Mac 上进行测试。

我知道,这是一个奇怪的工作流程,但这是我现在可以访问的。

设置

我已经下载了 Xcode 11 测试版并将其解压缩,以便我可以访问框架/SDK。但是,我似乎无法找到一种方法来sourcekit-lsp理解各种框架的标题。

此外,swift 安装在/usr/local/bin/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-21-a-ubuntu18.04.

由于sourcekit-lsp没有记录很多错误,我决定swift build一遍又一遍地尝试,尽可能接近“工作”,直到希望sourcekit-lsp能找到一些东西。

试错

到目前为止,我已经尝试了许多不同的命令。但我会遵循我所取得的最佳进展。

我首先使用指向swiftc我的 SDK 和 iOS 目标-sdk Xcode-beta.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk -target arm64-apple-ios13.0

这修复了模块未找到错误(当我运行时swift build

<unknown>:0: error: unable to load standard library for target 'arm64-apple-ios13.0'

我猜这是没有包含的 swift 库,所以我复制Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib/swift/iphoneos/usr/local/bin/swift-5.1-DEVELOPMENT-SNAPSHOT-2019-06-21-a-ubuntu18.04/usr/lib/swift/iphoneos,确实修复了上述错误。

我被困在哪里

现在,swift build给了我一个新的错误:

<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

此外,回到 VSCode,将鼠标悬停在模块导入(如Foundation)上会产生不同的错误:

error response (Request Failed): did not find primary SourceFile

我尝试修复这些错误但没有成功。我不需要swift build工作,但我只想sourcekit-lsp让我编写使用 UIKit/etc 的代码。

包.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"])])
    ]

)
4

0 回答 0