Skip to content

[ios,macos] Add Swift Sourcekit LSP support#189761

Merged
cbracken merged 6 commits into
flutter:masterfrom
cbracken:add-sourcekit-support
Jul 22, 2026
Merged

[ios,macos] Add Swift Sourcekit LSP support#189761
cbracken merged 6 commits into
flutter:masterfrom
cbracken:add-sourcekit-support

Conversation

@cbracken

Copy link
Copy Markdown
Member

This adds a post-processing pass to complie_commands.json that synthesizes the Swift entries that can be understood by SourceKit LSP and wires up the editor config needed to pick them up.

GN's --export-compile-commands only understands the built-in cc/cxx/objc/objcxx tool types, and doesn't yet have built-in support for injecting swiftc lines, so our custom swift tool (which invokes swiftc.py) never gets an entry there, meaning Swift code in the iOS and macOS embedders isn't indexed by SourceKit LSP and other tooling that reads that file.

Since GN's own compdb export omits the swift targets, _postGn() now shells out (once) to ninja -t compdb for the full compilation database (and falls back to the existing compile_commands.json on disk if that fails), then runs it through expandSwiftcCommands, which turns each swiftc.py-wrapped entry into one or more native swiftc invocations, one per compiled Swift file, with relative paths resolved to absolute against the entry's directory.

We drop -isystem and -Dkey=value flags since that's what swiftc.py does; swiftc only accepts boolean -D defines not key=value flags.

This scans the raw ninja output directly rather than decoding all of compile_commands.json, which can run past 20MB and is too slow to parse wholesale in Dart; only the small handful of matched swiftc.py entries get jsonDecoded -- fewer than 10 lines.

This also wires up .sourcekit-lsp/config.json and the swift.sourcekit-lsp.supported-languages setting in engine.code-workspace/engine-workspace.yaml so that VS Code's Swift extension correctly picks up the generated compdb.

Fixes #185741

Pre-launch Checklist

If you need help, consider asking for advice on the #hackers-new channel on Discord.

If this change needs to override an active code freeze, provide a comment explaining why. The code freeze workflow can be overridden by code reviewers. See pinned issues for any active code freezes with guidance.

Note: The Flutter team is currently trialing the use of Gemini Code Assist for GitHub. Comments from the gemini-code-assist bot should not be taken as authoritative feedback from the Flutter team. If you find its comments useful you can update your code accordingly, but if you are unsure or disagree with the feedback, please feel free to wait for a Flutter team member's review for guidance on which automated comments should be addressed.

@flutter-dashboard flutter-dashboard Bot added the CICD Run CI/CD label Jul 21, 2026
@github-actions github-actions Bot added the engine flutter/engine related. See also e: labels. label Jul 21, 2026
@cbracken
cbracken requested a review from jtmcdole July 21, 2026 04:32
@cbracken
cbracken force-pushed the add-sourcekit-support branch from cdce74c to 66d043c Compare July 21, 2026 04:34

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds support for native swiftc compilation commands in the compilation database by expanding GN swiftc.py wrapper invocations, and configures VS Code workspace settings to support Swift via SourceKit-LSP. Feedback on the changes includes removing accidentally pasted terminal progress output at the top of update_compdb.dart and appending .exe to the ninja executable path on Windows to prevent process execution failures.

I am having trouble creating individual review comments. Click here to see my feedback.

engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart (1-3)

critical

It looks like a terminal command's progress output was accidentally pasted at the top of this file. This will cause compilation errors and should be removed.

engine/src/flutter/tools/pkg/engine_build_configs/lib/src/build_config_runner.dart (394-399)

medium

On Windows, the ninja executable is named ninja.exe. We should append .exe when running on Windows to avoid process execution failures.

    final String ninjaPath = p.join(
      engineSrcDir.parent.parent.path,
      'third_party',
      'ninja',
      io.Platform.isWindows ? 'ninja.exe' : 'ninja',
    );

@cbracken
cbracken force-pushed the add-sourcekit-support branch from 66d043c to 102b862 Compare July 21, 2026 05:04
@cbracken

cbracken commented Jul 21, 2026

Copy link
Copy Markdown
Member Author

Notes for reviewers:

The important things to know:

  • In the 21MB file, there are fewer than 10 lines that do swiftc.py commands (IIRC it's like 4), I actually expect that number to go down over time as we add more swift.
    gn still has poor built-in swift support and doesn't emit the swiftc commands into compile-commands.json. We do JSON-decode the applicable lines but given there are fewer than 10, so the performance hit is negligible, unline for the clang++ commands (hence why @jtmcdole previously went for a regex for handling the RBE case).
  • Our swiftc build goes via a swiftc.py wrapper python script. Each invocation compiles all Swift files in a module (i.e. a gn source_set) together. But SourceKit LSP, like clangd, wants one one swiftc line per file so the point of this code is to translate that one line, all its crappy flags, and the list of all swift source files in a module into one swiftc line per source file (with the same set of crappy compiler flags).
  • Could I have used package:args for the flags parsing? Probably, but when I played with it, it added a dependency, and then doing the one-by-one flags translation ended up actually slightly more code than this. If you have ideas, I'm all ears. Fundamentally if gn ever implemnents full Swift support, this will all go away. This code handles that gracefully since it only operates on swiftc.py lines.

@cbracken
cbracken force-pushed the add-sourcekit-support branch from 102b862 to b6ee5a5 Compare July 21, 2026 12:57

@jtmcdole jtmcdole left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm, but some modern dart changes

Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
Comment thread engine/src/flutter/tools/pkg/engine_build_configs/lib/src/update_compdb.dart Outdated
jtmcdole
jtmcdole previously approved these changes Jul 21, 2026

@jtmcdole jtmcdole left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

printer2

@cbracken cbracken added CICD Run CI/CD and removed CICD Run CI/CD labels Jul 21, 2026
@cbracken
cbracken enabled auto-merge July 22, 2026 01:25
cbracken added 3 commits July 22, 2026 10:28
This adds a post-processing pass to `complie_commands.json` that
synthesizes the Swift entries that can be understood by SourceKit LSP
and wires up the editor config needed to pick them up.

GN's `--export-compile-commands` only understands the built-in
`cc`/`cxx`/`objc`/`objcxx` tool types, and doesn't yet have built-in
support for injecting `swiftc` lines, so our custom `swift` tool (which
invokes `swiftc.py`) never gets an entry there, meaning Swift code in
the iOS and macOS embedders isn't indexed by SourceKit LSP and other
tooling that reads that file.

Since GN's own compdb export omits the swift targets, `_postGn()` now
shells out (once) to `ninja -t compdb` for the full compilation database
(and falls back to the existing `compile_commands.json` on disk if that
fails), then runs it through `expandSwiftcCommands`, which turns each
`swiftc.py`-wrapped entry into one or more native `swiftc` invocations,
one per compiled Swift file, with relative paths resolved to absolute
against the entry's `directory`.

We drop `-isystem` and `-Dkey=value` flags since that's what `swiftc.py`
does; swiftc only accepts boolean -D defines not key=value flags.

This scans the raw ninja output directly rather than decoding all of
`compile_commands.json`, which can run past 20MB and is too slow to
parse wholesale in Dart; only the small handful of matched `swiftc.py`
entries get `jsonDecode`d -- fewer than 10 lines.

This also wires up `.sourcekit-lsp/config.json` and the
`swift.sourcekit-lsp.supported-languages` setting in
`engine.code-workspace`/`engine-workspace.yaml` so that VS Code's Swift
extension correctly picks up the generated compdb.

Fixes flutter#185741
jtmcdole
jtmcdole previously approved these changes Jul 22, 2026

@jtmcdole jtmcdole left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm-seinfeld

jtmcdole
jtmcdole previously approved these changes Jul 22, 2026

@jtmcdole jtmcdole left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

infrastructure bridge sunset outrun 2 lgtm_640_24

@jtmcdole

Copy link
Copy Markdown
Member

re-running flakey tests.

@cbracken cbracken added the autosubmit Merge PR when tree becomes green via auto submit App label Jul 22, 2026
@cbracken
cbracken enabled auto-merge July 22, 2026 21:40
@cbracken
cbracken added this pull request to the merge queue Jul 22, 2026
Merged via the queue into flutter:master with commit b322c11 Jul 22, 2026
19 of 20 checks passed
@cbracken
cbracken deleted the add-sourcekit-support branch July 22, 2026 23:07
@flutter-dashboard flutter-dashboard Bot removed the autosubmit Merge PR when tree becomes green via auto submit App label Jul 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CICD Run CI/CD engine flutter/engine related. See also e: labels.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[iOS][macOS] Get Swift language server protocol (LSP) support working

2 participants