> ## Documentation Index
> Fetch the complete documentation index at: https://openidea.sh/llms.txt
> Use this file to discover all available pages before exploring further.

# Swift

> Swift code intelligence with sourcekit-lsp for SwiftPM and Xcode projects, formatting, single-file runs and debugging with lldb-dap.

OpenIDEA supports Swift through [sourcekit-lsp](https://github.com/swiftlang/sourcekit-lsp), the language server that ships with the Swift toolchain. Highlighting, folding and the outline work without any setup. Completion, navigation, diagnostics, refactorings and formatting come from sourcekit-lsp. For the other languages, see the [support matrix](https://openidea.sh/en/languages/index.md).

| Language | Extensions | Language server | Debugger |
| - | - | - | - |
| Swift | `.swift`, `.swiftinterface` | sourcekit-lsp | lldb-dap |

## Setup

The IDE does not install Swift tools. Install a toolchain, which brings `swift`, `sourcekit-lsp` and `lldb-dap`:

* **macOS**: install Xcode, or only the Command Line Tools (`xcode-select --install`). Xcode is needed for iOS, macOS app and other Apple platform projects.
* **Linux and Windows**: install a toolchain from [swift.org](https://www.swift.org/install).

When `sourcekit-lsp` is not found, opening a Swift file shows an install hint instead of an **Install** button. On macOS, `/usr/bin/sourcekit-lsp` runs the copy from the Xcode chosen with `xcode-select` (or `DEVELOPER_DIR`, see [Several Xcode versions](#several-xcode-versions)). To use another binary, set **Executable** for sourcekit-lsp in **Settings › Languages & Frameworks › Language Servers**.

## Language server

sourcekit-lsp finds the workspace root by looking up from the file for one of these files: `Package.swift`, `buildServer.json`, `compile_commands.json` or `.sourcekit-lsp`. The outermost folder within the project that has one becomes the root; without any, the project folder is the root.

sourcekit-lsp then reads the build settings for that root:

* **SwiftPM**: a root with `Package.swift` works without setup. sourcekit-lsp resolves the package and its dependencies itself.
* **Build server**: a `.bsp/*.json` file or a `buildServer.json` in the root points sourcekit-lsp at a Build Server Protocol server. Xcode projects use this (see [Xcode projects](#xcode-projects)).
* **`compile_commands.json`**: a compilation database, for example from CMake.

### Package dependencies

Right-click `Package.swift` in the Project view or the editor, or use **Find Action** (`⇧⌘A` / `Ctrl+Shift+A`):

* **Resolve Swift Packages** runs `swift package resolve`: it fetches the dependencies and writes `Package.resolved`.
* **Update Swift Packages** runs `swift package update`: it moves the dependencies to the newest versions `Package.swift` allows.

Both run in the **Run** tool window in the folder of `Package.swift`. sourcekit-lsp watches `Package.swift` and `Package.resolved` and reloads the package when either changes, including after `git pull`, so completion for a new dependency works without restarting the server.

`.sourcekit-lsp/config.json` holds sourcekit-lsp's own options; see the [sourcekit-lsp configuration reference](https://github.com/swiftlang/sourcekit-lsp/blob/main/Documentation/Configuration%20File.md).

### Xcode projects

sourcekit-lsp cannot read `.xcodeproj` or `.xcworkspace` files. Without a build server, files in an Xcode project get no build settings, and you see errors such as "No such module". Generate a `buildServer.json` in the project root with one of these tools:

* **[xcode-build-server](https://github.com/SolaWing/xcode-build-server)**: `brew install xcode-build-server`, then in the project root:

  ```bash theme={null}
  xcode-build-server config -workspace App.xcworkspace -scheme App
  ```

  Use `-project App.xcodeproj` instead of `-workspace` for a project without a workspace. xcode-build-server takes the compile flags from Xcode's build logs, so build the scheme once in Xcode or with `xcodebuild` first, and build again after adding files or changing build settings.

* **[sourcekit-xcode-bsp](https://github.com/slime-studio/sourcekit-xcode-bsp)**: needs Xcode 26 or newer. Install it with `brew install slime-studio/tap/sourcekit-xcode-bsp` and run `sourcekit-xcode-bsp init` in the project root; it finds the `.xcworkspace` or `.xcodeproj` itself and targets the iOS Simulator by default (the `platform` field in `buildServer.json`). It is an early release (0.1.x).

Open the folder that contains `buildServer.json` (or `.bsp/`) as the project, or a parent of it with no other root marker between, so that it is the workspace root. After generating the file, reopen the project so that sourcekit-lsp starts with it.

> [!TIP]
>
> `buildServer.json` holds absolute paths for your machine. Add it to `.gitignore` instead of committing it.

**Clean Build Folder** (**Build › Xcode**, Find Action, or right-click the `.xcodeproj` / `.xcworkspace`) runs `xcodebuild clean` for the selected Xcode scheme in the Run tool window, with the same flags a run uses. **Delete DerivedData** moves this project's DerivedData folder (the one `xcodebuild -showBuildSettings` reports, plus the IDE's own run builds) to the Trash after asking, then restarts sourcekit-lsp. Other projects' DerivedData is left alone.

### New files in Xcode projects

`xcodebuild` compiles only the files a target lists, and the IDE does not edit `project.pbxproj`. What happens to a source file you create, rename or delete depends on how the project lists its files:

* **Synchronized folders** (Xcode 16 and newer): files in a folder shown with a blue folder icon in Xcode's navigator belong to the folder's targets as soon as they are on disk. To turn a classic group into one, right-click it in Xcode and choose **Convert to Folder**. New Xcode 16 projects use synchronized folders.
* **Classic groups**: a new file must be added to a target in Xcode (**File › Add Files to…**, or drag it into the group). When a new `.swift`, `.m`, `.h` or other source file isn't named in `project.pbxproj`, the IDE says so, with **Open in Xcode**.
* **[XcodeGen](https://github.com/yonaskolb/XcodeGen) (`project.yml`) and [Tuist](https://tuist.dev) (`Project.swift`)**: the project is generated from the spec, so after files change the IDE offers to regenerate it (`xcodegen generate` or `tuist generate --no-open`) in the Run tool window. **Regenerate Xcode Project** in **Find Action** (`⌘⇧A` / `Ctrl+Shift+A`), or in the context menu of `project.yml` and `Project.swift`, runs it any time.

### Several Xcode versions

To run sourcekit-lsp from a specific Xcode, set `DEVELOPER_DIR` in the server's environment: in **Settings › Languages & Frameworks › Language Servers**, select sourcekit-lsp and add it under **Startup › Environment variables**, or in `settings.toml`:

```toml theme={null}
[languageServers.sourcekit-lsp]
env = { DEVELOPER_DIR = "/Applications/Xcode-beta.app/Contents/Developer" }
```

Set it per project to use a different Xcode in each. It applies to sourcekit-lsp only; to build or run with the same Xcode, set `DEVELOPER_DIR` in the run configuration's environment variables too.

## Formatting

**Reformat Code** (`⌥⌘L` / `Ctrl+Alt+L`) uses sourcekit-lsp, which formats with [swift-format](https://github.com/swiftlang/swift-format) from the same toolchain. swift-format reads its options from a `.swift-format` file in the file's folder or a parent folder.

## Run and debug

### Single files

A `.swift` file can be run with **Run Context Configuration** (`⌃⇧R` / `Ctrl+Shift+F10`). The IDE runs `swift <file>` with the `swift` on your `PATH`.

### SwiftPM packages

There are no SwiftPM run or test configurations yet. Run `swift build`, `swift run` and `swift test` in the terminal, or create a **Shell Script** configuration for them (see [Run configurations](https://openidea.sh/en/guide/run.md)).

### Debugging

Debugging uses lldb-dap. On macOS the IDE finds Xcode's copy through `xcrun -f lldb-dap`; elsewhere it comes with the Swift toolchain on `PATH`. To debug a SwiftPM executable:

**1. Build it**

Run `swift build`. The executable is written to `.build/debug/<product>`.

**2. Create a Program configuration**

In **Run › Edit Configurations…**, add a **Program** configuration with **Executable** set to
`.build/debug/<product>` and **Debug adapter** set to **lldb-dap (C, C++, Swift)**.

**3. Debug**

Start it with **Debug** (`⌃D` / `Shift+F9`).

Rebuild with `swift build` after editing, as the configuration does not build first. **Attach to Process…** also attaches to a running Swift program with lldb-dap when CodeLLDB is not installed. See [Debugging](https://openidea.sh/en/guide/debugging.md) for breakpoints, stepping and the variables view.

## Limitations

* There are no run, test or debug configurations for SwiftPM targets or Xcode schemes, and no test tree for XCTest or Swift Testing.
* Xcode projects need a `buildServer.json` that you generate yourself (see [Xcode projects](#xcode-projects)).
* **Expand Macro** does nothing: sourcekit-lsp shows the expansion in a new document, which the IDE does not open yet.
* There is no SwiftUI Preview, Interface Builder, asset catalog editor or simulator integration. Use Xcode for those.
* sourcekit-lsp and lldb-dap are not installed by the IDE.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.