Editing Swift Packages in Visual Studio Code

Nearly two(!) years ago, I wrote about editing Swift packages in Xcode. In this post I'll show you how to edit packages in Visual Studio Code using the awesome Swift for VSCode extension. This extension provides the ability to solve the same issues described in the Xcode post, and arguably in a nicer way.

Swift for VSCode

The Swift for VSCode extension is a community extension maintained by the Swift Server Work Group. It provides all of the functionality you'd expect for a language extension in VSCode, including autocompletion, syntax highlighting, debugging support, test support and many more features. It's an incredible extension that provides a first class development experience when using Visual Studio Code. You really should try it.

When you open a project in VSCode that contains a Swift Package, you'll see something like:

A Swift Package in VSCode

This is just a basic app built from the Vapor template, but has a number of dependencies. You can see the dependencies in the PACKAGE DEPENDENCIES section in the explorer, much in the same way as Xcode displays them. As in the post on Xcode, there are many occasions where you may want to work on packages side by side or edit dependencies.

Editing your dependencies

In Xcode you need to do a bit of a dance by dragging directories into a workspace. It also has a few quirks such as problems caused by adding the dependency to the wrong level (which is an easy mistake to do), or accidentally clicking the Move To Trash button instead of Remove Reference when you're done and having to go rescue the dependency from the trash.

In VSCode, editing packages is much simpler. Right-click on the package you want to edit in the PACKAGE DEPENDENCIES and choose Use Local Version:

A Swift Package in VSCode

Then find the directory containing the package you want to edit and click Select in the dialog. You'll see the package change to your local one:

A Swift Package in VSCode

You can make changes in the local version and use then straight in your project!

Multiple Packages in a Workspace

One really neat feature of the Swift for VSCode extension is being able to have multiple packages in a workspace. You can add as many packages as you want and the extension will automatically generate tasks for building, running and testing each package.

Whilst you may just want to try running your package with a local dependency, the chances are you want to actually work on the local package at the same time. The extension makes this really easy. Right click on the local dependency in the PACKAGE DEPENDENCIES view and click Add To Workspace. The extension will automatically convert your project to a workspace with the two packages in:

Multiple packages in a workspace

You can run the tests for each package in the Test Explorer:

Multiple packages in a workspace test explorer

And you can build the different packages in the build menu (CMD+SHIFT+B):

Multiple packages in a workspace build menu

This does require any configuration, the extension does it all for you.

Tidying Up

Once you're done and want to switch back to the remote dependency, you can right click on the package directory and click Remove Folder from Workspace. Then in the package dependency view, right click on the local package and click Revert To Original Version:

Revert to original version menu

And that's it! It's really great to see how well the extension is now working and I'm using it more and more as my daily driver. If you have any ideas about what other features should be added, feel free to open an issue on GitHub.