Open-Source Wikis

/

Swift

/

Tools

/

SourceKit

apple/swift

SourceKit

Active contributors: akyrtzi, ahoppen, bnbarham

Purpose

tools/SourceKit/ is the IDE service that backs Xcode's editor experience and Apple's sourcekit-lsp server. It runs as an out-of-process daemon (or as an in-process framework on Apple platforms) and answers requests like "complete at this offset," "what's the type of the symbol under the cursor," "rename this symbol," and "format this region."

Internally it is a thin shell around lib/IDE/, lib/Index/, lib/Refactoring/, and lib/Markup/. The C++-IDE pages cover the inner workings; this page covers the service binary.

Directory layout

tools/SourceKit/
├── README.txt
├── lib/                       # the C++ implementation
│   ├── SwiftLang/             # request → handler mapping (Swift-specific)
│   ├── SourceKit/             # framework-style facade
│   └── ...
├── tools/                     # the actual binaries
│   ├── sourcekit/             # the daemon
│   ├── sourcekitd-test/       # CLI test driver
│   ├── sourcekitd-repl/       # interactive request console
│   └── complete-test/         # completion-specific tester
├── bindings/                  # Python and Swift client bindings
├── cmake/
└── docs/                      # request reference

Key abstractions

Type File Description
sourcekitd_request_t tools/SourceKit/include/sourcekitd/sourcekitd.h Opaque dictionary representing one client request.
sourcekitd_response_t tools/SourceKit/include/sourcekitd/sourcekitd.h Opaque dictionary returned to the client.
SourceKit::SwiftLangSupport tools/SourceKit/lib/SwiftLang/SwiftLangSupport.h The Swift-language request dispatcher.
SourceKit::EditorEditingDoc tools/SourceKit/lib/SwiftLang/SwiftEditor.cpp An open file's parsing/typecheck state.

How it works

sequenceDiagram
    participant Editor as Xcode / sourcekit-lsp
    participant SK as sourcekitd
    participant Lang as SwiftLangSupport
    participant Compiler as in-process Swift compiler
    Editor->>SK: request (XPC dict / framed JSON-like)
    SK->>Lang: dispatch by request kind
    Lang->>Compiler: setup CompilerInstance, run partial compile
    Compiler-->>Lang: results (completions, types, edits, ...)
    Lang-->>SK: response dict
    SK-->>Editor: response

Request format

Requests and responses are dictionary-based. Encoded as XPC dictionaries on Apple platforms or framed binary blobs elsewhere. sourcekitd-test accepts a textual format (tools/SourceKit/tools/sourcekitd-test/) for invoking requests from CI.

A typical completion request looks like:

{
  key.request:    source.request.codecomplete,
  key.sourcefile: "/path/to/Foo.swift",
  key.offset:     128,
  key.compilerargs: ["-sdk", "/path/to/SDK", ...]
}

The response is a list of key.results, each carrying a key.name, key.typename, key.kind, etc.

Long-running compiler instances

SourceKit keeps CompilerInstance objects alive between requests for the same file. This is critical for performance: re-parsing and re-type-checking a 5,000-line file on every keystroke would be untenable. The instance maintains a cache, and incremental requests reuse it via IDEInspectionCallbacks (see IDE and SourceKit).

Macros

When a request opens a file containing macros, SourceKit launches swift-plugin-server (or reuses an existing one) and forwards macro expansions to it. Failures in the plugin server become sourcekitd errors rather than crashes.

Integration points

  • sourcekit-lsp (in apple/sourcekit-lsp) is the most prominent consumer. It is a separate process and translates LSP requests into sourcekitd requests.
  • Xcode embeds sourcekitd-inproc.framework for in-process requests.
  • Tests -- test/SourceKit/ and the unit tests in tools/SourceKit/tools/sourcekitd-test/ exercise individual requests.

Entry points for modification

  • Adding a new request: extend sourcekitd-request-set in tools/SourceKit/lib/SwiftLang/, route to the appropriate lib/IDE/ or lib/Refactoring/ API, document in tools/SourceKit/docs/.
  • Improving completion: live in lib/IDE/ (see IDE and SourceKit). The SourceKit layer is mostly request marshalling.

Built by Factory AutoWiki from public repository content. It is a generated preview for codebase exploration, not source-maintained documentation.

SourceKit – Swift wiki | Factory