# Adding explicit module support to sourcekitd

**URL:** <https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784>\
**Category:** SourceKit-LSP\
**Tags:** sourcekit\
**Created:** [February 17, 2026, 2:07pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784 "2026-02-17T14:07:33Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![DavidGoldman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davidgoldman/32/6931_2.png) [@DavidGoldman](https://forums.swift.org/u/DavidGoldman)\
**Post date:** [February 17, 2026, 2:07pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/1 "2026-02-17T14:07:33Z")

</div>

Hi,

I’m interested in contributing support for explicit modules for sourcekitd / Sourcekit-LSP. Currently, sourcekitd uses implicit modules (pcms built when sourcekitd loads an AST) with detailed pre-processing records enabled, which currently in [ClangImporter](https://github.com/swiftlang/swift/blob/fa7e9b34376c4e6a37a9e96daebff50313dc33fa/lib/ClangImporter/ClangImporter.cpp#L772) forces the raw module format and the detailed preprocessing record flag.

Ideally, I’d like sourcekitd to be able to re-use explicit modules from compilation, including obj format, but I’m not sure if there’s an issue without the detailed pre-processing record flag being enabled - should we enable it for our compilations too? Otherwise, if sourcekitd needs this information, I think we’d be forced to have separate explicit module compilations for sourcekitd.

I’m happy to contribute this but could definitely use some pointers. Thanks!

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 12, 2026, 12:34pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/2 "2026-03-12T12:34:40Z")

</div>

Sorry for the late response, this would be wonderful to get done! Happy to help answer any questions.

AFAIK the detailed preprocessing record is enabled in other to allow us to share the same PCMs as indexing, as we enable it there to allow indexing macros. Although I should note that it looks like [we are still able to index macros without it](https://github.com/swiftlang/llvm-project/pull/2799), but we'll only pick up the last definition.

I should note though that even if we changed that, there are other differences between the index and regular build that likely means we'll still end up with a separate set of built module dependencies. For example, we enable `-experimental-allow-module-with-compiler-errors`, `-fallow-pcm-with-compiler-errors`, and `-fallow-pch-with-compiler-errors` to allow modules with errors.

In terms of the work needed to adopt explicit modules, my recollection is that it comes down to the following:

1. Ensuring that preparation works correctly with explicit modules

I don't have a good sense of what all the issues are here, though I believe we have previously encountered issues with the fact that we allow compiler errors here. For example, a PCH built with a missing header wouldn't get rebuilt when the header becomes available since it isn't recorded as an input.

1. Propagating the explicit modules to SourceKit

SourceKit expects driver arguments (and is currently using the old driver [1]), which is a little problematic for explicit modules since we essentially need to split the driver invocation into the dependency scan and explicit module build (which should run during preparation), and the actual frontend invocation for SourceKit (which takes the set of explicit modules from that). It's possible we could hack this together by manually pulling out the `-explicit-swift-module-map-file` obtained from preparation and pass it as an `-Xfrontend` argument, but it might also be worth experimenting to see if SourceKit could take frontend arguments directly instead. The latter might not be entirely trivial to do since a lot of the infrastructure around SourceKit currently expects a single set of build arguments per target.

[1]: @ArtemC started work on adopting the new driver for SourceKit in [Link `swift-ide-test` against the new (early)swift-driver and exercise its `getSingleFrontendInvocationFromDriverArguments` C API by artemcm · Pull Request #75840 · swiftlang/swift · GitHub](https://github.com/swiftlang/swift/pull/75840).

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 12, 2026, 1:08pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/3 "2026-03-12T13:08:42Z")

</div>

> [@hamishknight](#):
>
> SourceKit expects driver arguments (and is currently using the old driver [1]), which is a little problematic for explicit modules since we essentially need to split the driver invocation into the dependency scan and explicit module build (which should run during preparation), and the actual frontend invocation for SourceKit (which takes the set of explicit modules from that).

FWIW, in our use case (Bazel builds), we don't need the dependency scan step at all—we express all of the system/SDK modules as explicit dependencies and so we don't do any of the import-scanning work that produces the the dependency scan JSON.

Totally understand that that likely would be necessary in some fashion for SPM/Xcode use cases, but I want to make sure that we don't end up with an implementation that _requires or assumes_ it either.

> [@](#):
>
> It's possible we could hack this together by manually pulling out the `-explicit-swift-module-map-file` obtained from preparation and pass it as an `-Xfrontend` argument, but it might also be worth experimenting to see if SourceKit could take frontend arguments directly instead. The latter might not be entirely trivial to do since a lot of the infrastructure around SourceKit currently expects a single set of build arguments per target.

In Bazel builds we would love to adopt `-explicit-swift-module-map-file` to express our dependencies for regular compilation actions instead of passing the hundreds of `-Xcc -fmodule-(map-)file` arguments on the command line that we do today, but IIRC the fact that SourceKit is still using the old driver is blocking that. (Blocking in the sense that we can't just take the command line from the Bazel action and drop it into SourceKit; we'd have to manually convert the JSON manifest to the equivalent Clang flags.)

So if that issue were resolved, then that sounds like a great path forward for both of us: Xcode/SPM could do the prescan work to get that file, and for Bazel we would just generate it ourselves based on the BUILD graph and pass it directly to SourceKit as well.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 12, 2026, 2:16pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/4 "2026-03-12T14:16:57Z")

</div>

> [@allevato](#):
>
> but IIRC the fact that SourceKit is still using the old driver is blocking that

I don't actually know if this is true, certainly the old driver won't understand it if you pass it as a driver flag, but I think if you go behind its back and use `-Xfrontend` it _might_ work, I haven't played about with this though. @ArtemC would know better. But in any case we really need to move SourceKit onto the new driver, assuming we want to continue passing driver arguments to it that is.

> [@allevato](#):
>
> So if that issue were resolved, then that sounds like a great path forward for both of us: Xcode/SPM could do the prescan work to get that file, and for Bazel we would just generate it ourselves based on the BUILD graph and pass it directly to SourceKit as well.

Yes that's exactly what I'm thinking, specifically preparation (in BSP this is [`buildTarget/prepare`](https://github.com/swiftlang/sourcekit-lsp/blob/1a182c34266f11cb9c68c2b6e0070b315a115662/Contributor%20Documentation/BSP%20Extensions.md#buildtargetprepare)) is what should do the dependency scanning and explicit module building, and you can skip that for Bazel.

---

<div class="post-metadata">

**Author:** ![ArtemC](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/artemc/32/27369_2.png) [@ArtemC](https://forums.swift.org/u/ArtemC)\
**Post date:** [March 12, 2026, 2:52pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/5 "2026-03-12T14:52:35Z")

</div>

> [@hamishknight](#):
>
> I think if you go behind its back and use `-Xfrontend` it _might_ work, I haven't played about with this though.

I do think this ought to work.

And I broadly second the enthusiasm for this idea and the approach @hamishknight outlined above with 1) teaching the preparation stage to do the dependency scan and building the actual module dependencies, and 2) teaching SourceKit to actually execute compilation in a way that uses preparation results without repeating them, likely with some kind of specialised/massaged `swiftc` invocation.

I am not too familiar with what it would take to have SourceKit take frontend arguments directly though, and where they would be generated in that case.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 12, 2026, 3:11pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/6 "2026-03-12T15:11:12Z")

</div>

> [@ArtemC](#):
>
> I am not too familiar with what it would take to have SourceKit take frontend arguments directly though, and where they would be generated in that case.

So for a swift-build BSP I _think_ we should actually already have the frontend arguments in the build description (and I believe those would already have all the right flags to be able to use the explicit modules from preparation), so that would mostly be a case of refactoring the logic in LSP to not assume that a target has a single set of build arguments I think (or maybe it would be sufficient to teach it to stick `-primary-file` in the right place, perhaps SourceKit itself could even do that).

In cases where the BSP returns driver arguments (e.g for `compile_commands.json`, and I assume Bazel?), I'm thinking we could potentially have LSP link swift-driver and do the transform there. Ideally though it would be part of the BSP itself I think. I haven't fully thought through all of the implications of this though, it's possible we'll need to continue supporting driver arguments in SourceKit regardless (but we could still have a mode for passing direct frontend args). Really my hope here is to just avoid trying to reconstruct the frontend args in SourceKit that swift-build _already has_.

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 12, 2026, 4:01pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/7 "2026-03-12T16:01:12Z")

</div>

The way our Bazel integration works (@DavidGoldman can chime in with more details, he's deeper in it than I am) is that we scrape the actions that Bazel creates to spawn `swiftc`, so we're dealing with driver flags. We don't _directly_ have any way to do fancy things like link to `swift-driver` and extract the frontend jobs from that—the only interface we have to the compiler is the driver executable via standard `swiftc` invocations.

We actually wrap the driver invocation in another process that does some command-line fudging for things that we can't do at the time that we create the action, so theoretically we could have that do something like dump an extra output file that's the moral equivalent of `compile_commands.json` with the individual frontend invocations by running `swiftc -###`, but I believe `swift-driver` today is over-aggressively resolving some relative paths to absolute paths before creating frontend jobs, so writing those paths to a file would produce nondeterministic outputs, harming caching unless we went to the trouble of sanitizing those paths first. (I need to track these down better and try to fix them upstream.)

It would be much easier for us if SourceKit could just take the driver flags expected by `swift-driver` and process them however it needs.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 12, 2026, 4:27pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/8 "2026-03-12T16:27:34Z")

</div>

I see, interesting! Out of curiosity, how are you actually getting those settings into LSP, or are you using something completely different there that directly uses `sourcekitd.framework`? I'm wondering if it would be sufficient to convert the driver arguments in LSP itself.

---

<div class="post-metadata">

**Author:** ![DavidGoldman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davidgoldman/32/6931_2.png) [@DavidGoldman](https://forums.swift.org/u/DavidGoldman)\
**Post date:** [March 12, 2026, 5:38pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/9 "2026-03-12T17:38:12Z")

</div>

Currently in our implementation we have a custom built-in build server to sourcekit-lsp as well as working on a new implementation that just uses a CompilationDatabase but we could also build a simple BSP server. We could definitely try that `-Xfrontend` trick, maybe that will work.

> I should note though that even if we changed that, there are other differences between the index and regular build that likely means we'll still end up with a separate set of built module dependencies. For example, we enable `-experimental-allow-module-with-compiler-errors`, `-fallow-pcm-with-compiler-errors`, and `-fallow-pch-with-compiler-errors` to allow modules with errors.

Hmm yeah this part is more tricky, I believe currently in our custom indexing implementation we don’t use these flags and thus our integration will fail once you have a build error. I think we could trigger an IDE build itself with these flags and then sourcekit wouldn’t need to add them. Having this would mean it no longer shares with a primary build but I do still think this would be beneficial to support for other reasons.

Would we need multiple set of arguments support for sourcekitd/sourcekit-lsp? I would assume sourcekitd/sourcekit-lsp would always want to use the resilient form with `-experimental-allow-module-with-compiler-errors`, `-fallow-pcm-with-compiler-errors`, and `-fallow-pch-with-compiler-errors` if it can, otherwise use the standard flags? Or are there other indexing flags that will break things?

---

<div class="post-metadata">

**Author:** ![allevato](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/allevato/32/385_2.png) [@allevato](https://forums.swift.org/u/allevato)\
**Post date:** [March 12, 2026, 6:32pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/10 "2026-03-12T18:32:07Z")

</div>

`-fallow-pcm-with-compiler-errors` and `-fallow-pch-with-compiler-errors` are Clang frontend flags, not something that's serialized into the actual AST, from what I can tell. So even if SourceKit's invocation uses them, that shouldn't prevent us from using PCMs built with Bazel that didn't use those flags.

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 13, 2026, 11:51am UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/11 "2026-03-13T11:51:17Z")

</div>

> [@DavidGoldman](#):
>
> Currently in our implementation we have a custom built-in build server to sourcekit-lsp as well as working on a new implementation that just uses a CompilationDatabase but we could also build a simple BSP server

I see, any of those should work fine I think. Ultimately I think we'll probably still need to be able to support build servers returning driver arguments, but I would like to see if it's possible to push the translation logic into LSP itself.

> [@DavidGoldman](#):
>
> I think we could trigger an IDE build itself with these flags and then sourcekit wouldn’t need to add them

That would probably work I think, it's somewhat similar to what LSP is doing today [for preparation in the SwiftPMBuildServer](https://github.com/swiftlang/sourcekit-lsp/blob/1a182c34266f11cb9c68c2b6e0070b315a115662/Sources/BuildServerIntegration/SwiftPMBuildServer.swift#L775-L779), it passes `--experimental-prepare-for-indexing` to `swift build`, and [that enables `-experimental-allow-module-with-compiler-errors`](https://github.com/swiftlang/swift-package-manager/blob/cb90a734da843d6ec2f9a7a466e69e65b3944315/Sources/Build/BuildDescription/SwiftModuleBuildDescription.swift#L601-L606) (interestingly we don't appear to be passing the PCM/PCH flags there though, seems like that might be an oversight), although SwiftPM also tries to avoid emitting object files in that case.

> [@DavidGoldman](#):
>
> Would we need multiple set of arguments support for sourcekitd/sourcekit-lsp? I would assume sourcekitd/sourcekit-lsp would always want to use the resilient form with `-experimental-allow-module-with-compiler-errors`, `-fallow-pcm-with-compiler-errors`, and `-fallow-pch-with-compiler-errors` if it can, otherwise use the standard flags? Or are there other indexing flags that will break things?

I'm not sure I fully understand the question here, but I think what we ought to at least do is remove these flag overrides from sourcekitd itself (e.g it shouldn't be hardcoding `-detailed-preprocessing-record` & `-fmodule-format=raw`), and instead leave that up to LSP and the build servers. We could then make this configurable in LSP for build servers that don't want LSP to add additional arguments.

> [@allevato](#):
>
> `-fallow-pcm-with-compiler-errors` and `-fallow-pch-with-compiler-errors` are Clang frontend flags, not something that's serialized into the actual AST, from what I can tell. So even if SourceKit's invocation uses them, that shouldn't prevent us from using PCMs built with Bazel that didn't use those flags

Right, they don't actually prevent you from _using_ the PCH/PCM, but would cause a separate set of modules to be built if you mismatched them in the build vs preparation.

---

<div class="post-metadata">

**Author:** ![DavidGoldman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davidgoldman/32/6931_2.png) [@DavidGoldman](https://forums.swift.org/u/DavidGoldman)\
**Post date:** [March 24, 2026, 12:45am UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/12 "2026-03-24T00:45:40Z")

</div>

> [@hamishknight](#):
>
> I'm not sure I fully understand the question here, but I think what we ought to at least do is remove these flag overrides from sourcekitd itself (e.g it shouldn't be hardcoding `-detailed-preprocessing-record` & `-fmodule-format=raw`), and instead leave that up to LSP and the build servers. We could then make this configurable in LSP for build servers that don't want LSP to add additional arguments.

Okay, this sounds like a great start. Should I make this configurable via a sourcekit request option, or is it ok to remove it and put that logic into sourcekit-lsp in the same PR? Looks like we’d have to forward it all the way to [initCompilerInvocation](https://github.com/swiftlang/swift/blob/594c32a89d9545b2a7eefc6748305f16c2fbfc85/lib/IDETool/CompilerInvocation.cpp#L217)

---

<div class="post-metadata">

**Author:** ![hamishknight](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/hamishknight/32/14226_2.png) [@hamishknight](https://forums.swift.org/u/hamishknight)\
**Post date:** [March 24, 2026, 11:04am UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/13 "2026-03-24T11:04:23Z")

</div>

I would completely remove it from sourcekitd and just handle it in LSP yeah

---

<div class="post-metadata">

**Author:** ![DavidGoldman](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/davidgoldman/32/6931_2.png) [@DavidGoldman](https://forums.swift.org/u/DavidGoldman)\
**Post date:** [April 9, 2026, 9:08pm UTC](https://forums.swift.org/t/adding-explicit-module-support-to-sourcekitd/84784/14 "2026-04-09T21:08:41Z")

</div>

Oh great, thanks! Sorry for the late response, planning to revisit this soon
