Commit Graph

115 Commits

Author SHA1 Message Date
Rintaro Ishizaki a6c926fa34 Relax atomic memory orderings; switch semaphore to release/acquire
For ID counters and standalone flags (most sites), drop to .relaxed
since nothing reads other memory based on the atomic's value.

MultiEntrySemaphore.signaled is the exception: it is used as a
signal/wait primitive where waiters proceed to use state set up
before signal(). Use .releasing on store and .acquiring on load so
that pre-signal writes are visible to waiters that observe `true`.
.relaxed there would be incorrect on weakly-ordered architectures.
2026-05-21 11:16:17 -07:00
Rintaro Ishizaki 83de30d29f Migrate atomic call sites to Synchronization.Atomic and ThreadSafeBox
Replace AtomicBool/UInt8/UInt32/Int32 from
ToolsProtocolsSwiftExtensions with Synchronization.Atomic<T> where
the storage is a static, module-level let, or class stored property.
For local lets captured by @Sendable closures (where Atomic's
~Copyable nature prevents capture), use ThreadSafeBox<T> instead.
2026-05-21 10:58:34 -07:00
Rintaro Ishizaki a39c92022f Add forOpenDocument label to language service lookup methods
Rename languageServices(for:), primaryLanguageService(for:), and their
internal counterparts to use the `forOpenDocument` label, so the
precondition that the document must already be open is visible at call
sites.

Also make primaryLanguageService(forOpenDocument:) throw instead of
returning an optional, and switch several resolve-style handlers from
the find-or-create primaryLanguageService(for:_:) to
primaryLanguageService(forOpenDocument:), since those call sites already
have an open document in hand.
2026-05-20 10:31:44 -07:00
Rintaro Ishizaki f2a121453d Scope language service instances per workspace
Previously, language services were held in a global registry on
SourceKitLSPServer and shared across workspaces, requiring complex
lifetime tracking (isImmortal, shutdownOrphanedLanguageServices) to
decide when to tear them down. In practice, every language service
already stored workspace-specific properties (buildServerManager,
semanticIndexManagerTask), so sharing them across workspaces was never
truly safe. Giving each Workspace its own service instances simplifies
lifetime management: services are created when needed and shut down
with their workspace.

Remove LanguageService.isImmortal, the workspace parameter from
canHandle(toolchain:), and the initialize/clientInitialized protocol
requirements.
2026-05-18 09:21:01 -07:00
Rintaro Ishizaki 64aa354caf [TestDiscovery] "/refresh" style tests/playgrounds requests
When the client opts in to `workspace/tests/refresh` or
`workspace/playgrounds/refresh` via experimental client capabilities,
SourceKit-LSP now maintains a proactive cache of the current test and
playground lists and sends the corresponding `workspace/.../refresh`
notification whenever the cache changes. `workspaceTests()` /
`workspacePlaygrounds()` then serve subsequent fetch requests directly
from the cache.

Add `EntryPointManager`: runs background scans, stores the results,
fires callbacks on changes:
 - Start scanning when build targets are updated including initial
   updates, any watched files are changed, and index is updated.
 - Send '/refresh' server initiated requests when the cache has changed.
 - Coalesces rapid invalidations by cancelling any in-flight refresh task.

Also:
- Simplify `SourceKitIndexDelegate` from an `actor` with `AtomicInt32`
  to a plain `class`, since it is only called from IndexStoreDB's
  internal serial dispatch queue.
2026-03-27 09:32:59 -07:00
Alex Hoppen e59d5e8bd3 Always wait for real build settings in testExperimentalFeaturesPassedToSyntaxTreeManager
If the SwiftPM build server is slow to respond, we may get document symbols based on a `SyntaxTreeManager` with fallback build settings, which would not contain the `_test_EverythingUnexpected` experimental feature and which would thus fail.

Similar issues existed in other tests as well.
2026-03-20 16:01:42 +01:00
Alex Hoppen d13471cc74 Merge pull request #2485 from Steffeeen/semantic-tokens
Only create semantic tokens in cases where they are actually needed
2026-03-15 13:33:15 +01:00
Steffeeen dc2da5cc58 Add reportSyntacticHighlightInSemanticTokens config option
When this option is set to `true` the results for
`textDocument/semanticTokens` include semantic tokens for syntactic
highlighting obtained from swift-syntax. If this option is set to
`false` only the semantic tokens obtained from SourceKit are included.
This option defaults to `false`.
2026-03-13 17:02:47 +01:00
Hamish Knight b2f42a1c48 Test to make sure sourcekitd doesn't change working directory
Ensure that the working directory remains consistent across the
lifetime of `TestSourceKitLSPClient`.
2026-02-09 10:05:13 +00:00
Karan Lokchandani 473088afe6 Add position markers in the expected ranges (#2449)
this should make tests more readable, follow up to this pr
https://github.com/swiftlang/sourcekit-lsp/pull/2435
reference comment here
https://github.com/swiftlang/sourcekit-lsp/pull/2435#discussion_r2670161613

---------

Signed-off-by: Karan <karanlokchandani@protonmail.com>
2026-01-13 23:06:06 +01:00
Alex Hoppen 219428328c Address my own review comments
Apply the following changes:
- Check for the presence of `#Playgrounds` textually before getting the module name in `SwiftPlaygroundsScanner`. This is important because getting the module name requires us to get build settings for the file, which can be expensive. Do the cheaper check first
- Make `syntacticTests` and `syntacticPlaygrounds` closures capture the workspace instead of passing the workspace from the `SwiftSyntacticIndex` back out. I like this better because now we can’t accidentally pass the wrong workspace to a `SwiftSyntacticIndex`, eg. to `buildTargetsChanges`.
- Capture the initialize result in `TestSourceKitLSPClient` instead of using `postInitialization` to capture the result
- Minor cleanup of unnecessary abstractions, likely artifacts of earlier iterations
- Restructure tests so that every test has its own list of source files, allowing for easier local reasoning – turns out some of these tests didn’t even need to open a workspace, just to check the initialize response
2025-12-08 15:16:36 -05:00
Adam Ward 60289cdfd6 Add tests 2025-12-08 15:16:36 -05:00
Anthony Latsis 6b19657739 Enable ExistentialAny 2025-12-02 12:27:27 +00:00
Owen Voorhees f04b971726 Adopt swift-tools-protocols 2025-10-31 14:11:11 -07:00
Alex Hoppen 6b494ddf95 Flush logs at the end of test execution
Otherwise it sometimes happens that shutdown and other messages are logged during the next test’s execution, which makes it harder to diagnose issues.
2025-09-29 13:51:09 +02:00
Alex Hoppen 882c990cae Add support for copied header files to SourceKit-LSP
If a build server copies files (eg. header) to the build directory during preparation and those copied files are referenced for semantic functionality, we would currently jump to the file in the build directory. Teach SourceKit-LSP about files that are copied during preparation and if we detect that we are jumping to such a file, jump to the original file instead.

So far only the definition request checks the copied file paths. Adding support for copied file paths in the other requests will be a follow-up change.
2025-08-30 23:35:14 +02:00
Alex Hoppen d887490990 Merge pull request #2231 from ahoppen/exhaustive-swift-format
Apply exhaustive swift-format configuration from swift-syntax
2025-08-11 21:02:32 +02:00
Alex Hoppen 98cd30bb1c Apply exhaustive swift-format configuration from swift-syntax
Apply the exhaustive swift-format configuration from https://github.com/swiftlang/swift-syntax/pull/3117 to sourcekit-lsp. Also apply all automatic formattings.
2025-08-11 09:23:39 +02:00
Alex Hoppen c692a83f43 Construct LanguageServiceRegistry outside of SourceKitLSP module 2025-08-04 11:44:04 +02:00
Alex Hoppen 4f792e8781 Register language services in a dynamic registry 2025-08-04 11:44:04 +02:00
Alex Hoppen c04518241d Fix issue that caused request cancellation after a timeout in tests to not work
We were calling `server.handle(notification:)`, which called directly into `SourceKitLSPServer` but request handling is implemented in `QueueBasedMessageHandler.handle(_:)`. Call that to ensure that we actually cancel requests after the test timeout.
2025-04-07 18:28:41 -07:00
Alex Hoppen a151ca413c Fix a race condition leading to out-of-order notifications in tests
`TestSourceKitLSPClient.handle` created a new `Task`. This means that we could swap the order of notifications received from SourceKit-LSP. In most cases this doesn’t matter but `BackgroundIndexingTests.testProduceIndexLogWithTaskID` checks that the notification to start a structured log is received before the first report, which might not be the case because of the `Task` here.

Change `PendingNotifications` to a class with a `ThreadSafeBox` to remove the need for a `Task`.

rdar://147814254
2025-03-27 15:15:11 -07:00
Alex Hoppen 73016af24e Make workspace/synchronize a non-experimental request
This request is generally useful, not only for tests within SourceKit-LSP but also:
- In editor tests that want to test the integration with SourceKit-LSP
- In code analysis tools that want to gather project information using SourceKit-LSP and need an up-to-date index for that.

Remove the experimental feature guard from `workspace/_synchronize`, consequently rename it to `workspace/synchronize` and only guard the `buildServerUpdates` option on the synchronize request by an experimental feature because its long-term usefulness is still not fully understood yet.
2025-03-26 10:08:20 -07:00
Alex Hoppen ea1f1879d2 Merge pull request #2082 from ahoppen/cancel
Send CancelRequestNotification if we time out a request in `TestSourceKitLSPClient`
2025-03-24 13:27:58 -07:00
Alex Hoppen 37b8b06732 Send CancelRequestNotification if we time out a request in TestSourceKitLSPClient 2025-03-21 15:22:38 -07:00
Alex Hoppen 6feab14ee2 Add a timeout to the semaphore we use for shutting down SourceKit-LSP 2025-03-20 10:30:34 -07:00
Alex Hoppen b757fb9806 Merge pull request #2069 from ahoppen/test-request-timeout
Add a timeout for requests sent from `TestSourceKitLSPClient`
2025-03-17 17:07:32 -07:00
Alex Hoppen 1f5cb45b32 Add a timeout for requests sent from TestSourceKitLSPClient
Otherwise, if a SourceKit-LSP request hangs, we would hang the test execution.
2025-03-17 11:16:24 -07:00
Alex Hoppen 6a3afeaaa0 Remove trailing comma
This restores the ability to build SourceKit-LSP using a Swift 6.0 compiler
2025-03-14 15:52:12 -07:00
Alex Hoppen 1f1e440dd8 Merge pull request #2060 from ahoppen/synchronize-request
Generalize `PollIndexRequest` and `BarrierRequest` into a single `SynchronizeRequest`
2025-03-14 15:10:13 -07:00
Alex Hoppen 9c867df103 Merge pull request #2057 from ahoppen/test-timeout
Add a few timeout checks to tests
2025-03-13 13:46:06 -07:00
Alex Hoppen 5fba1699bd Generalize PollIndexRequest and BarrierRequest into a single SynchronizeRequest
This new request also allows us to wait for build system updates without waiting for background indexing to finish.
2025-03-12 14:25:50 -07:00
Alex Hoppen 1cd1102d89 Introduce defaultTimeoutDuration to get the default timeout as a Duration 2025-03-11 09:20:44 -07:00
Alex Hoppen 776d019ec9 Do not send DiagnosticsRefreshRequest to the client if it doesn't support it
Check if the client's capabilities to see if it supports `workspace/diagnostic/refresh`.
2025-03-10 14:11:23 -07:00
Alex Hoppen 1cfa8db1d8 Require Swift 6 to build SourceKit-LSP
This significantly cleans up our `import` statements
2025-03-07 08:05:49 -08:00
Alex Hoppen bd6fb5d04c Do not block initialization of the build server when build server is unresponsive in returning the list of test files
We were blocking the initialization response on `self.buildSystemManager.testFiles`, which requires the list of test files to be determined. Make that operation asynchronous so that a slow build server can’t take down all of SourceKit-LSP.
2025-02-26 16:31:29 -08:00
Alex Hoppen b33a42f0ab When using SOURCEKIT_LSP_TEST_PLUGIN_PATHS=RELATIVE_TO_SOURCEKITD infer plugin paths from default testing toolchain
Otherwise, we infer the SourceKit plugin paths from the toolchain when creating a `SourceKitLSPServer` during testing because we don’t override the plugin paths in the SourceKitLSPOptions. But when running `SourceKitDTests`, we pass `pluginPaths: nil`, which would not load any plugins. If both of the tests run in the same process, this causes a fault to get logged because sourcekitd can only loaded once per process and we can’t modify which plugins are loaded after the fact.
2025-02-05 08:23:04 -08:00
Alex Hoppen 8787787e4f Allow loading the SourceKit plugin from the toolchain during testing 2025-01-27 09:18:45 -08:00
Alex Hoppen ed098f58cb Merge pull request #1934 from ahoppen/multi-toolchain-support
Support opening documents within the same workspace with sourcekitd/clangd from different toolchains
2025-01-25 08:52:52 -08:00
Alex Hoppen 98b1294ad9 Support opening documents within the same workspace with sourcekitd/clangd from different toolchains
rdar://142909783
2025-01-24 22:32:05 -08:00
Alex Hoppen aee36fdf6c Remove SkipUnless checks that always evaluate to true
We don’t support testing SourceKit-LSP using toolchains that don’t contain these features anymore.
2025-01-23 21:11:08 -08:00
Matthew Bastien 6675d275fe address review comments 2025-01-17 11:11:56 -05:00
Matthew Bastien 5c3136694f use built-in LSP ResponseError with code RequestFailed for errors 2025-01-17 11:11:55 -05:00
Matthew Bastien af02df9af7 revert changes to TestSourceKitLSPClient 2025-01-17 11:11:54 -05:00
Matthew Bastien d4b63a2af5 add textDocument/convertDocumentation request to SourceKit-LSP 2025-01-17 11:11:54 -05:00
Alex Hoppen 31b1909ce1 Allow injection of build systems into SourceKitLSP
This allows us to clean up the creation of `TestBuildSystem` a little bit because the tests can create `TestBuildSystem` instead of retrieving it from the `BuildSystemManager`.

rdar://142906050
2025-01-15 15:57:21 -08:00
Alex Hoppen e5f1bbfc03 Set plugin paths in all tests
Otherwise, we were trying to load sourcekitd without the plugins for the normal SourceKIt-LSP tests and with the plugins for the plugin tests.
2025-01-09 14:09:01 +01:00
Alex Hoppen 5709e1a864 Add a SourceKit plugin to handle code completion requests
This adds a sourcekitd plugin that drives the code completion requests. It also includes a `CompletionScoring` module that’s used to rank code completion results based on their contextual match, allowing us to show more relevant code completion results at the top.
2025-01-03 14:21:54 +01:00
Alex Hoppen c0c0f1f0a6 Skip tests that use background indexing when running tests with a Swift 5.10 toolchain
https://github.com/swiftlang/sourcekit-lsp/pull/1714 changed the background preparation mode to `enabled` but a Swift 5.10 toolchain does not support `--experimental-prepare-for-indexing`. Thus, these tests fail. Skip tests that rely on background indexing when testing SourceKit-LSP with a Swift 5.10 host toolchain.
2024-11-18 15:28:34 -08:00
Alex Hoppen 78f9968eec Merge pull request #1819 from ahoppen/notification-wait 2024-11-14 08:09:33 -08:00