swiftlang / vscode-swift

Visual Studio Code Extension for Swift
https://marketplace.visualstudio.com/items?itemName=sswg.swift-lang
Apache License 2.0
755 stars 54 forks source link

Unable to format Swift sources #940

Closed tonyarnold closed 4 months ago

tonyarnold commented 4 months ago

When running the "Format Document" command, I get the following error: "There is no formatter for swift files installed".

CleanShot 2024-06-27 at 16 23 01@2x

I have Xcode 16 beta 2 set as my Swift toolchain.

Environment

adam-fowler commented 4 months ago

Go to the settings and type the following in the filter bar @id:editor.defaultFormatter @lang:swift. The setting that comes up should be set to Swift. VSCode normally comes up with a dialog if no formatter is chosen, don't know why that didn't appear

tonyarnold commented 4 months ago

I just followed those instructions, and it still does the same thing. It added the following lines to my settings.json:

"[swift]": {
  "editor.defaultFormatter": "sswg.swift-lang"
}
adam-fowler commented 4 months ago

ok, working fine for me, but I'm using a nightly build of swift swift-6.0-DEVELOPMENT-SNAPSHOT-2024-06-22-a.xctoolchain/. It should work for Xcode beta builds as well.

If you look in <Xcode install directory>/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin do you see swift-format?

tonyarnold commented 4 months ago

Yeah, it's definitely there:

.rwxr-xr-x@ 21M tonyarnold 26 Jun 07:19  /Applications/Xcode-16.0.0-Beta.2.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/swift-format
tonyarnold commented 4 months ago

Oh hang on, this is an LSP issue:

[Error - 20:28:54] SourceKit Language Server client: couldn't create connection to server.
  Message: Pending response rejected since connection got disposed
  Code: -32097 
Error: Unknown option '--experimental-feature'
Usage: source-kit-lsp <options> <subcommand>
  See 'source-kit-lsp --help' for more information.
[Error - 20:28:54] Server process exited with code 64.
[Error - 20:28:54] Server initialization failed.
  Message: Pending response rejected since connection got disposed
  Code: -32097 

I tried to follow the guide here: https://github.com/apple/sourcekit-lsp/blob/main/Documentation/Enable%20Experimental%20Background%20Indexing.md

adam-fowler commented 4 months ago

Ah if LSP doesn't start then you won't get formatting via the swift extension. You should report those issues setting up background indexing on the SourceKit-LSP repo

tonyarnold commented 4 months ago

On further investigation, the sourcekit-lsp binary included with Xcode 16 betas is quite old, and does not include the flags mentioned in that document you linked from the forums.

It's not an issue for this extension, but it may be worth mentioning in your post?