Skip to content

Lazy prepare-for-indexing produces incomplete swiftmodules when types receive macro-synthesized extension conformances #2696

Description

@ktoso

Swift version

swiftlang-6.4.0.19.4

Platform

all platforms

Editor

VSCode

Description

When background indexing runs with the default mode, modules that contain types decorated with macros that use @attached(extension, conformances: ...) appear to produce swiftmodules whose conformance information is incomplete or missing. Cross-module consumers then see spurious "type does not conform to protocol" diagnostics, broken completion, and broken jump-to-definition for APIs that depend on those conformances.

Switching to backgroundPreparationMode: "noLazy" seems to resolve the issues.

Previously thought to just expose this setting swiftlang/vscode-swift#2291 but instead we should fix the indexing.

Steps to Reproduce

swiftlang/swift-java editing experience is broken due to this and reproduces easily.

Additional context

swift-java's @JavaClass / @JavaInterface macros are declared as:

  @attached(member, names: …)
  @attached(extension, conformances: AnyJavaObject)
  public macro JavaClass(_ fullClassName: String, …)

Their ExtensionMacro expansion emits:

  extension <Type>: AnyJavaObject { }

These macros are applied to many types across many modules in the project, and downstream modules use those types where an AnyJavaObject conformance is required.

With backgroundPreparationMode: "enabled" (default), opening the project in VS Code with background indexing enabled produces sporadic but consistent failures (false errors, missing completion, broken jump-to-def) on code that should bevalid. With backgroundPreparationMode: "noLazy", everything works correctly.

The workaround currently committed to swift-java is .sourcekit-lsp/config.json: {"backgroundPreparationMode": "noLazy"}

Steps to reproduce

  1. git clone https://github.com/swiftlang/swift-java && cd swift-java
  2. rm .sourcekit-lsp/config.json to remove the workaround setting the noLazy mode
  3. Open in VS Code with the Swift extension and background indexing enabled
  4. Open files that import modules containing @JavaClass / @JavaInterface types, any file containing them diagnostics / broken IDE features on uses of those types

Logging

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions