Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
206 lines (159 sloc) 6.6 KB
title author category excerpt status
Swift Development with Visual Studio Code
VSCode is a cross-platform text and source code editor from Microsoft, and among the first tools to support Language Server Protocol. With the previously announced support for LSP for Swift now available in early development, it’s a great time to see how this integration works for yourself.

Visual Studio Code (VSCode) is a cross-platform text and source code editor from Microsoft. It's one of the most exciting open source projects today, with regular updates from hundreds of contributors. VSCode was among the first tools to support Language Server Protocol (LSP), which has played a large part in providing a great developer experience, in a variety of languages and technologies.

With the previously announced support for LSP for Swift now available in early development, it's a great time to see how this integration works for yourself.

So this week, we'll walk through the process of how to get started with Swift's new Language Server Protocol support in Visual Studio Code on macOS. If you haven't tried writing Swift outside Xcode, or are already a VSCode user and new to the language entirely, this article will tell you everything you need to know.

{% warning %}

Swift support for Language Server Protocol is still in early development and doesn't currently offer pre-built packages for the language server (sourcekit-lsp) or the Visual Studio Code extension. For now, downloading, building, and installing these components and their dependencies is a manual process that requires some familiarity with the command line (it should take about 15 minutes on a reasonably fast internet connection).

When LSP support is made generally available, it will be much easier to get everything set up.

{% endwarning %}

{% asset vscode-banner.png %}

Step 0: Install Xcode

If you don't already have Xcode installed on your machine, open the Terminal app and run the following command:

$ xcode-select --install

Running this command presents a system prompt.

{% asset xcode-select-window.png %}

Click the "Get Xcode" button and continue installation on the App Store.

Step 1: Install Visual Studio Code

Download Visual Studio Code and install it to your system Applications folder. Open the app and follow the instructions for launching from the command line. You'll need to have the code command accessible from $PATH in order to install the SourceKit-LSP extension later on.

{% info %}

Electron apps have a reputation for being big and slow, but don't let that stop you from giving VSCode a try --- its performance and memory footprint are comparable to a native app.

{% endinfo %}

Step 2: Install the Latest Swift Toolchain

Go to and download the latest trunk development snapshot (at the time of writing, this was from November 16th, 2018). Once it's finished downloading, run the package to install the Xcode toolchain. To enable it, open Xcode, select the "Xcode > Preferences..." menu item (,), navigate to Components and choose Swift Development Snapshot.

Step 3: Install Node and NPM

VSCode extensions are written in JavaScript / TypeScript. If you're not already set up for JS development, you can download Node (a JavaScript run-time for outside the browser)
and npm (a package manager for Node) with Homebrew using the following commands or manually by following these instructions:

$ brew install node

To verify that you have a working installation, run the following command:

$ npm --version

Step 4: Build and Install SourceKit-LSP

With all of the dependencies taken care of, we're now ready for the main attraction. From the command line, clone the sourcekit-lsp repository, navigate to the resulting directory, and build the Swift project.

$ git clone
$ cd sourcekit-lsp
$ swift build

If successful, the completed binary will be available from of the hidden .build/debug directory. Move that binary to a standard directory in your $PATH, like /usr/local/bin or /usr/bin.

$ mv .build/debug/sourcekit-lsp /usr/local/bin

You can verify that everything is working as expected by running the sourcekit-lsp command:

$ sourcekit-lsp

This command launches a new language server process, but don't worry if it doesn't provide any feedback to STDOUT --- that means it's working as intended. Exit the process with an ETX signal (^C).

Step 5: Build and Install SourceKit-LSP Extension for Visual Studio Code

Now that you have the Swift language server available, the final step is to build and install the extension that allows Visual Studio Code to communicate with it.

From the sourcekit-lsp directory in the previous step, navigate to the Editors/vscode directory, use npm to build the extension and then use the code command to install it:

$ cd Editors/vscode/
$ npm run createDevPackage
$ code --install-extension out/sourcekit-lsp-vscode-dev.vsix

Now launch (or relaunch) VSCode and open a Swift project, such as this one, and enjoy an early preview of the functionality provided by Language Server Protocol support for Swift.

{% asset vscode-swift-lsp-screenshot.png %}

{% info %}

To get the full experience of working with Swift from VSCode, we recommend that you also check out the Swift Language extension (for syntax highlighting until LSP supports this functionality) and the Swift Lint extension (for real-time style and convention diagnostics).

{% endinfo %}

So there you have it --- the makings of a first-class Swift development experience outside of Xcode. For now, Swift support for Language Server Protocol is limited to code completion, quick help, diagnostics, jumping to symbol definitions, and finding references. But we couldn't be more excited for the future of this project and what it means for the prospects of the Swift language outside the Apple ecosystem.