Try agent mode in VS Code!
Dismiss this update
Swift is a general-purpose programming language that's approachable for newcomers and powerful for experts. It is fast, modern, safe, and a joy to write. This topic goes into detail about setting up and using Swift within Visual Studio Code, with the swiftlang.swift-vscode extension.
The Swift extension includes:
The Swift extension is designed to support the following projects:
Package.swift
)compile_commands.json
(e.g. using CMake)To create a new Swift project, you can use the Swift: Create New Project...
command in
the Swift extension to guide you through the process. You can find this command by opening
the Command Palette and following the instructions below.
swift.openAfterCreateNewProject
setting.The Swift extension uses SourceKit LSP to power language features. SourceKit LSP provides the following features in the editor. Use these links to see the VS Code documentation for each topic:
SourceKit LSP also provides code actions to automate common tasks. Code actions in VS Code appear as a light bulb near the editor margin (see the below screenshot for an example of this). Clicking on the light bulb will show you the available actions which can include:
Package.swift
Before language features can be used you must perform a swift build
command on your
project either on the command line or using a task in VS Code. This populates the index in sourcekit-lsp.
Visual Studio Code provides tasks as a way to run external tools. See the Integrate with External Tools via Tasks documentation to learn more.
The Swift extension provides some built-in tasks that you can use to build via
Swift Package Manager. You can also configure custom tasks by creating a
tasks.json
file in the root folder of your project. For example, this
tasks.json
builds of your Swift targets in release mode:
{
"version": "2.0.0",
"tasks": [
{
"type": "swift",
"label": "Swift Build All - Release",
"detail": "swift build --build-tests",
"args": ["build", "--build-tests", "-c", "release"],
"env": {},
"cwd": "${workspaceFolder}",
"group": "build"
}
]
}
The above task is configured to be in the build
group. This means it will
appear in the run build tasks
menu that can be opened with CMD+Shift+B
on macOS or Ctrl+Shift+B on other platforms:
Any errors that occur during a build appear in the editor as diagnostics alongside those provided by SourceKit-LSP. Running another build task clears the diagnostics from the previous build task.
Visual Studio Code provides a rich debugging experience. See the Debugging documentation for more information.
The Swift extension relies on the Code-LLDB extension to enable debugging support.
The Swift extension will prompt you to configure settings for LLDB the first time you launch VS Code. You will need to either apply the configuration globally (user settings) or to your workspace (workspace settings) for the debugger to work properly.
By default, the extension creates a launch configuration for each executable
target in your Swift package. You may configure these yourself by adding a
launch.json
file to the root folder of your project. For example, this
launch.json
launches a Swift executable with custom arguments:
{
"configurations": [
{
"type": "lldb",
"name": "Debug swift-executable",
"request": "launch",
"sourceLanguages": ["swift"],
"args": ["--hello", "world"],
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/.build/debug/swift-executable",
"preLaunchTask": "swift: Build Debug swift-executable"
}
]
}
You can launch a debugging session via the Debug view in VS Code.
The executable will be launched and you can set breakpoints in your Swift code that will be hit as code executes.
The screenshot below shows an example of debugging a Hello World program. It is paused on a breakpoint and you can see that the Debug View shows the values of variables in scope. You can also hover over identifiers in the editor to see their variable values:
Visual Studio Code provides a Test Explorer view in the left sidebar which can be used:
The Swift extension supports XCTest as well as Swift Testing. As you write tests they are automatically added to the Test Explorer.
To debug a test:
Debug Test
profile.The Run Test with Coverage
profile instruments the code under test and opens a
code coverage report when the test run completes. As you browse covered files,
line numbers that were executed during a test appear green, and those that were
missed appear red. Hovering over a line number shows how many times covered
lines were executed. Line execution counts can be shown or hidden using the
Test: Show Inline Coverage
command.
Swift Testing tests annotated with
tags
can be filtered in the Test Explorer using @TestTarget:tagName
. You can then
run or debug the filtered list of tests.
The Swift VS Code extension does not support running Swift Testing tests in Swift 5.10 or earlier.
The Swift extension automatically detects your installed Swift toolchain.
However, it also provides a command called Swift: Select Toolchain...
which
can be used to select between toolchains if you have multiple installed.
This is an advanced feature used to configure VS Code with a toolchain other
than the default on your machine. It is recommended to use xcode-select
on
macOS or swiftly
on Linux to switch between toolchains globally.
You may be prompted to select where to configure this new path. Your options are to:
Keep in mind that Workspace Settings take precedence over User Settings:
The Swift extension will then prompt you to reload the extension in order to pick up the new toolchain. You must do so, otherwise the extension will not function correctly: