emacs-lsp/lsp-sourcekit

No completion on basic SPM project (Xcode 12 beta)

xenodium opened this issue · 10 comments

Completion of Apple frameworks work really well. Thank you! Completion of my own types within a basic SPM project don't seem to be working:

Screenshot 2020-07-06 at 19 05 27

Also no completion in FooBar.swift:

struct FooBar {
  static func main() {
    let foo = Foo()
    foo.sing()
    foo. // <- No completion here
  }
}

Am I misconfiguring things?

(use-package lsp-sourcekit
    :config
    (setq lsp-sourcekit-extra-args (list "--log-level" "debug"))
    (setq lsp-sourcekit-executable "/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"))

Project builds otherwise:

$ swift build
[1/2] Compiling FooBar Foo.swift
[2/2] Compiling FooBar FooBar.swift
[3/3] Merging module FooBar

My Package.swift

// swift-tools-version:5.3
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "FooBar",
  products: [
    .library(
      name: "FooBar",
      targets: ["FooBar"])
  ],
  targets: [
    .target(
      name: "FooBar",
      dependencies: []),
    .testTarget(
      name: "FooBarTests",
      dependencies: ["FooBar"]),
  ]
)

Project structure

$ find FooBar
FooBar
FooBar/Tests
FooBar/Tests/LinuxMain.swift
FooBar/Tests/FooBarTests
FooBar/Tests/FooBarTests/XCTestManifests.swift
FooBar/Tests/FooBarTests/FooBarTests.swift
FooBar/README.md
FooBar/.gitignore
FooBar/Package.swift
FooBar/Sources
FooBar/Sources/FooBar
FooBar/Sources/FooBar/FooBar.swift
FooBar/Sources/FooBar/Foo.swift

Foo.swift content

public struct Foo {
  public func sing() {
    print("singing")
  }
}

Thanks for the detailed report. I can't reproduce your problem, I get good code completion for module Foo and no spurious diagnostic messages:

Screenshot 2020-07-06 at 11 30 41 PM

<<error type>> for type Foo is suspicious. That kind of problems are usually caused by a mismatch in toolchain versions between the SPM version included in the language server and what your SPM project specifies in its manifest. Are you using Xcode 12 Beta? Could you try one of the following things and restart Emacs to see if it solves the problem?

  • Specify Swift 5.2 for your package. You can do that by running this command from inside your project folder: swift package tools-version --set 5.2. That'll modify your Package.swift accordingly.

  • If you really want to use 5.3, install a recent 5.3 toolchain from https://swift.org/download/#snapshots and configure the LSP client to point to it:

(setq lsp-sourcekit-executable "/Library/Developer/Toolchains/swift-latest.xctoolchain/usr/bin/sourcekit-lsp")

I'm suspicious that there may be a Swift version mismatch in the SourceKit-LSP version that shipped in the first beta of Xcode 12. I haven't seen clear reports about this issue yet, though.

Let me know if any of the workarounds fix your issue. If yes, then we may need to report the bug to Apple so that they fix it in a next Xcode 12 beta; if not, we'll enable further LSP and SourceKit logging to see what's going on.

Thank you for this wonderful response. I'm an SPM noob and learned a few things here. Specifying Swift 5.2 for my package did the job for me.

Are you using Xcode 12 Beta?

That's right. Via (setq lsp-sourcekit-executable "/Applications/Xcode-beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp").

Specify Swift 5.2 for your package. You can do that by running this command from inside your project folder: swift package tools-version --set 5.2. That'll modify your Package.swift accordingly.

Thank you 👍

Screenshot 2020-07-07 at 10 16 04

If you really want to use 5.3, install a recent 5.3 toolchain from https://swift.org/download/#snapshots and configure the LSP client to point to it:

I can do with 5.2 for now, but can also try the snapshots.

I'm suspicious that there may be a Swift version mismatch in the SourceKit-LSP version that shipped in the first beta of Xcode 12. I haven't seen clear reports about this issue yet, though.

Let me know if any of the workarounds fix your issue. If yes, then we may need to report the bug to Apple so that they fix it in a next Xcode 12 beta;

Any more useful info I can give?

$ xcode-select -p
/Applications/Xcode-beta.app/Contents/Developer
$ swift package --version
Swift Package Manager - Swift 5.3.0
$ swift --version
Apple Swift version 5.3 (swiftlang-1200.0.16.9 clang-1200.0.22.5)
Target: x86_64-apple-darwin19.4.0

I have checked Xcode 12 Beta 2 (released today), and the problem persists. The most interesting log message I get from the server is this one:

[Trace - 11:29:57 PM] Received notification 'window/logMessage'.
Params: {
"message": "package at '/Users/dmartin/Desktop/FooBar' is using Swift tools version 5.3.0 but the installed version is 5.2.0",
"type": 4
}

I'll prepare a bug report for Apple. For now, the only workarounds that work are:

[Trace - 11:29:57 PM] Received notification 'window/logMessage'.
Params: {
"message": "package at '/Users/dmartin/Desktop/FooBar' is using Swift tools version 5.3.0 but the installed version is 5.2.0",
"type": 4
}

Are you using adding "--log-level" to lsp-sourcekit-extra-args to see these kinds of logs or a is there somewhere else I can look in the future to diagnose things?

I'll prepare a bug report for Apple. For now, the only workarounds that work are:

Thank you. You've been super helpful. We can prolly close this issue.

Are you using adding "--log-level" to lsp-sourcekit-extra-args to see these kinds of logs or a is there somewhere else I can look in the future to diagnose things?

You can check the Emacs <-> Server communication logs by enabling debugging in the client:

  • (setq lsp-print-io t).
  • M-x lsp-workspace-show-log.

Specific for Swift, you can add --log-level to the SourceKit-LSP invocation to get more detailed information. If you need even more logging, you can enable SourceKit logging:

  • (setenv "SOURCEKIT_LOGGING" "3")
  • Restart the Swift language server: M-x lsp-restart-workspace.
  • Try to reproduce the problem and do M-x lsp-workspace-show-log.

This is great. Helps me (and hopefully others) diagnose things in the future.

Really enjoying Swift/LSP on Emacs. Muchas gracias!

Apple confirmed this is a bug in Xcode 12 Beta (https://forums.swift.org/t/should-sourcekit-lsp-from-xcode-12-beta-work-with-swift-5-3/38362/3?u=daniel_martin), so I've created FB7961416 to track it. I'll post here when Apple informs me that the bug is fixed.

Good to see confirmation. Thanks for following up.

I have tested this again with Xcode 12 Beta 5 and things seem to be working fine.

Thank you. BTW, I built the latest sourcekit-lsp and it feels way snappier.