Skip to content

[Tests] Add coverage for ReferencesContext.includeDeclaration#2644

Open
YooGyeongMo wants to merge 3 commits into
swiftlang:mainfrom
YooGyeongMo:fix-references-includes-definition
Open

[Tests] Add coverage for ReferencesContext.includeDeclaration#2644
YooGyeongMo wants to merge 3 commits into
swiftlang:mainfrom
YooGyeongMo:fix-references-includes-definition

Conversation

@YooGyeongMo
Copy link
Copy Markdown

@YooGyeongMo YooGyeongMo commented May 12, 2026

Description

Following the discussion in #2638, SourceKit-LSP's textDocument/references behavior appears to already comply with the LSP specification. When includeDeclaration is true, the declaration is included. When includeDeclaration is false, the declaration is excluded.

The behavior reported in #2638, where the definition appears in "Find All References" results in VS Code, appears to be caused by the VS Code Swift extension sending includeDeclaration: true for that command, rather than by a server-side bug (verified here).

This PR reverts the earlier behavior change and instead adds test coverage for the includeDeclaration: false case, which wasn't previously covered. The includeDeclaration: true case is already covered by existing tests testReferencesInMacro, CopyDestinationTests, IndexTests, SwiftPMIntegrationTests.

Tests added

  • testReferencesWithoutDeclaration: verifies that includeDeclaration: false returns only use sites

The test passes against the current behavior.

Note on the original issue

The UX concern, where the definition appears in the results, is best addressed on the client side. For example, the VS Code Swift extension could send includeDeclaration: false for "Find All References". I would be happy to follow up on vscode-swift after this PR is reviewed.

Related to #2638

References should return use sites, not the definition itself.
The textDocument/references request was including the symbol's own definition, adding noise when navigating from a definition.

This change removes .definition from the roles set when includeDeclaration is true. The existing testReferencesInMacro test was also updated to apply the same principle consistently across the macro case.

Fixes swiftlang#2638
[
Location(uri: project.fileURI, range: Range(project.positions["2️⃣"])),
Location(uri: project.fileURI, range: Range(project.positions["3️⃣"])),
]
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If I understood the LSP specification correctly, the declarartion should be part of this response if and only if includeDeclaration is true. https://microsoft.github.io/language-server-protocol/specifications/lsp/3.17/specification/#textDocument_references

You could check if this is already the behavior (write tests if not yet covered). Maybe the VSCode extension is sending the wrong request with includeDeclaration: true.

Revert the earlier behavior change and add regression test coverage for both modes of `includeDeclaration`. sourcekit-lsp already handles this per the LSP spec.

These tests pin the behavior so it cannot regress.

Related to swiftlang#2638
@ahoppen
Copy link
Copy Markdown
Member

ahoppen commented May 12, 2026

The changes in your PR do not seem to match your PR description. In particular, it currently only contains test changes. Could you please make sure that they match?

@YooGyeongMo YooGyeongMo changed the title Exclude the symbol's own definition from the references response [Tests] Add coverage for ReferencesContext.includeDeclaration May 12, 2026
@YooGyeongMo
Copy link
Copy Markdown
Author

YooGyeongMo commented May 12, 2026

Hi @ahoppen @tothambrus11 ,

Thanks for the feedback. Apologies for the mismatch — I've updated the description to match the current changes: test coverage only, with no server behavior change.

For context on how I arrived here:

I initially tried removing .definition from the roles set so that references would always exclude the symbol's own definition. After @tothambrus11 pointed out the LSP spec — that the declaration should be excluded only when includeDeclaration is false — I wrote tests for both modes to verify SourceKit-LSP's actual behavior:

  • includeDeclaration: false excludes the declaration from the references result
  • includeDeclaration: true includes the declaration in the references result

The includeDeclaration: true test failed with my earlier change, which made it clear that my original direction was breaking the spec rather than fixing a bug. So I reverted that change and turned the verification tests into this PR.

Based on this, SourceKit-LSP appears to already be following the LSP behavior for textDocument/references. The behavior reported in #2638 may be caused by the VS Code Swift extension sending includeDeclaration: true for "Find All References", which would make this more of a client-side issue.

Does this direction make sense to you?

@YooGyeongMo
Copy link
Copy Markdown
Author

@ahoppen Thanks for the update in #2638

I saw that this now appears to be related to the VS Code extension sending includeDeclaration: true, while SourceKit-LSP is handling the request according to the LSP behavior.

Given that, would it still be useful to keep this PR as test-only coverage for the existing includeDeclaration behavior?

@ahoppen
Copy link
Copy Markdown
Member

ahoppen commented May 13, 2026

Yeah, a test would be good here but one of the tests you added covers the default behavior, which should already be tested by other tests. Could you reduce this to only add a test that covers new behavior?

@YooGyeongMo
Copy link
Copy Markdown
Author

YooGyeongMo commented May 13, 2026

Done, thanks @ahoppen !

I removed the redundant testReferencesWithDeclaration test and kept only the includeDeclaration: false coverage.

The includeDeclaration: true behavior is already covered by existing tests, including testReferencesInMacro.

Please let me know if this looks good or if anything else is needed.

Copy link
Copy Markdown
Member

@ahoppen ahoppen left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks.

@ahoppen
Copy link
Copy Markdown
Member

ahoppen commented May 14, 2026

@swift-ci Please test

@ahoppen
Copy link
Copy Markdown
Member

ahoppen commented May 14, 2026

@swift-ci Please test Windows

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants