Make Intellisense Work for a Swift Project in VSCode

Learn how to use the official Swift VSCode plugin and a dev server to get proper Swift intellisense for your XCode project.

SwiftVSCode

In this tutorial, you will:

  1. Install the official Swift plugin for VSCode.
  2. Install and run xcode-build-server.
  3. Use the build-server to communicate with the Swift Language Server Protocol and get Intellisense working properly.

Meaning you don’t have to leave lovely VSCode for crappy XCode when you’re building macOS apps.

Step 1 — Install the Official Swift Plugin

Install the following plugin in VSCode:

Swift - Visual Studio Marketplace

When you search the marketplace, you’ll likely see two. Search this exact string to get the correct one:

swiftlang.swift-vscode

Step 2 — Install xcode-build-server

You can install xcode-build-server via brew with:

bash
brew install xcode-build-server

This will install it as a CLI command. Check it’s worked with:

bash
where xcode-build-server

Step 3 — Build Your Project with XCode

To build your project, run the following:

bash
xcodebuild -scheme YourScheme -destination 'platform=macOS' clean build

The -scheme flag tells XCode which set of build instructions you want to run. You can see the available schemes by running the following from the root of your project:

bash
xcodebuild -list

Step 4 — Run xcode-build-server

Finally, you can run the xcode-build-server to produce a buildServer.json file that tells the LSP how to interact with the xcode-build-server:

bash
xcode-build-server config -project YourProject.xcodeproj -scheme YourScheme

This will create a buildServer.json file that looks like this:

json
{
  "name": "xcode build server",
  "version": "1.3.0",
  "bspVersion": "2.2.0",
  "languages": ["c", "cpp", "objective-c", "objective-cpp", "swift"],
  "argv": ["/opt/homebrew/bin/xcode-build-server"],
  "workspace": "/Users/your-username/path/to/your-project/YourProject.xcodeproj/project.xcworkspace",
  "build_root": "/Users/your-username/Library/Developer/Xcode/DerivedData/YourProject-dkymfthsnwjywxatlrxebnqpfpav",
  "scheme": "YourScheme",
  "kind": "xcode"
}

This configuration tells the Swift LSP to call the xcode-build-server program we’ve installed via brew with the workspace, build_root, and scheme provided. xcode-build-server looks at the build logs and finds out what files XCode built with what. These are returned to the Swift LSP to allow proper intellisense.

Multiple Schemes

If you have multiple Schemes in your project definition, you’ll have to run the xcode-build-server command on the different Schemes in turn. You will then get Intellisense on the built Scheme, but not on the unbuilt ones. Change the scheme option in the buildServer.json to alter what Scheme you want Intellisense on.

Final Words

I’m brand new to Swift development, so I apologise if I’m overlooking anything in this tutorial. However, I couldn’t find a decent tutorial explaining how to set up Intellisense in VSCode properly, so I had to make one.

I hope it saves you the 2.5 hours I spent searching and hacking for a working solution! Swift away.