How I Got Zed Editor Working with Swift Projects

J@y
2 min readAug 22, 2024

--

As a Swift developer, I’m always on the lookout for tools that can enhance my workflow. Recently, I decided to try out Zed, an editor from Zed Industries that has been gaining traction in the developer community. Given my background, it was only natural to see how well it works with Swift. I ran into a few hiccups along the way, and I’d like to share how they got resolved.

Setting Up Swift in Zed

The first step is to install the Swift extension for Zed. This extension is essential as it relies on sourcekit-lsp to provide features like code completion and diagnostics, making your development experience much smoother.

However, I quickly hit a roadblock: Zed wasn’t recognizing my local Swift packages or external Swift Package Manager (SPM) dependencies. I spent a good amount of time troubleshooting, combing through documentation, and trying different solutions, but nothing seemed to work. It was frustrating because, aside from this issue, Zed was shaping up to be an incredibly fast and responsive editor — potentially what the editor in Xcode should be.

The Solution: buildServer.json

After some digging, I discovered that to get sourcekit-lsp to properly navigate your project, you need a buildServer.json file in your project’s root directory. Here’s how you can generate it:

  1. Set up Xcode Build Server: Follow the instructions on this GitHub repository to set up the Xcode build server.
  2. Generate buildServer.json:
  • If you’re working with an Xcode workspace, run:
xcode-build-server config -scheme A_SCHEME_NAME -workspace WORKSPACE_NAME.xcworkspace
  • If you’re working with a standard Xcode project, run:
xcode-build-server config -scheme A_SCHEME_NAME -project PROJECT_NAME.xcodeproj

One of these commands will create a buildServer.json file at the root of your project, which is key to making sourcekit-lsp work with Zed.

Configuring Zed

Next, you’ll need to tweak your Zed settings. Here’s what I added to my ~/.config/zed/settings.json file:

{
"languages": {
"Swift": {
"enable_language_server": true,
"language_servers": ["sourcekit-lsp"],
"formatter": "language_server",
"format_on_save": "on"
}
}
}

If you want to load the LSP from a specific path, say a beta version of Xcode, you can modify the configuration like this:

{
"lsp": {
"sourcekit-lsp": {
"binary": {
"path": "/Applications/Xcode-16.1.0-Beta.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/sourcekit-lsp"
}
}
},
"languages": {
"Swift": {
"enable_language_server": true,
"language_servers": ["sourcekit-lsp"],
"formatter": "language_server",
"format_on_save": "on"
}
}
}

With these settings in place, all you need to do is restart Zed, load your project, and you should be good to go!

Final Thoughts

Zed is a promising editor, and once you get past the initial setup hurdles, it can be a powerful tool for Swift development. If you run into any issues, I recommend checking out the Usage section of the xcode_build_server GitHub repo for more details.

--

--

J@y
J@y

Written by J@y

Technology, lifestyle, travel and food enthusiast.

Responses (1)