56 Commits

Author SHA1 Message Date
Divya Prakash
0ca0660d99 Refactor DocumentManager threading for Swift 6
Replaces DispatchQueue with ThreadSafeBox, relaxes T: Sendable constraints, and removes @unchecked Sendable from Document.
2025-12-21 19:26:02 +05:30
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
Ben Barham
add109d2d8 Avoid crashing when loading client plugins from the same path
When `DYLD_(FRAMEWORK|LIBRARY)_PATH` is set, `dlopen` will first check
if the basename of the provided path is within any of its search paths.
Thus it's possible that only a single library is loaded for each
toolchain, rather than a separate like we expect. The paths should be
equal in this case, since the client plugin is loaded based on the path
of `sourcekitd.framework` (and we should only have one for the same
reason). Allow this case and just avoid re-initializing.
2025-09-26 12:22:59 -07:00
Alex Hoppen
c7ca73aecb Drop support for building with Swift 6.1
Swift 6.2 has been released, so we no longer need to support building and testing SourceKit-LSP using Swift 6.1
2025-09-20 16:37:24 +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
Anthony Latsis
7fb77b6b7a .swift-format: Enable NoVoidReturnOnFunctionSignature 2025-06-23 19:48:20 +01:00
Alex Hoppen
d63419de73 Address review comments 2025-05-12 15:48:45 +02:00
Alex Hoppen
69ab356b36 Log contextual requests that affect sourcekitd’s global state
This way we can log them when a sourcekitd request crashes and we can thus replay these contextual requests when diagnosing the crash.
2025-05-12 15:39:52 +02:00
Alex Hoppen
863c0e2cdf Merge pull request #2137 from ahoppen/terminate-unresponsive-clangd-sourcekitd
If sourcekitd or clangd don’t respond to a request for 5 minutes, terminate them and use crash recovery to restore behavior
2025-05-12 15:13:30 +02:00
Alex Hoppen
14cfd50582 If sourcekitd or clangd don’t respond to a request for 5 minutes, terminate them and use crash recovery to restore behavior
This should be a last stop-gap measure in case sourcekitd or clangd get stuck, don’t respond to any requests anymore and don’t honor cancellation either. In that case we can restore SourceKit-LSP behavior by killing them and using the crash recovery logic to restore functionality.

rdar://149492159
2025-05-07 13:43:28 +02:00
Alex Hoppen
17327e611c Ensure the DLHandle passed to SourceKitD.init(dlhandle:path:pluginPaths:initialize:) gets closed if the initializer fails
Just something I noticed while looking through code. I am not aware of any issues caused by this.

Also relax the `precondition` in `DLHandle.deinit` to a fault. If we do not close a `DLHandle`, that’s a bug but it’s not so bad that we should crash sourcekit-lsp for it.
2025-03-28 14:28:55 -07:00
Alex Hoppen
53407f59af Mark asynchronous blocks in Csourcekitd as Sendable
We don’t have any guarantees which thread these blocks will be called on by sourcekitd, so we shouldn’t make any assumptions about it in Swift. We should thus mark them as Sendable.
2025-03-27 11:06:49 -07:00
Alex Hoppen
5882eae734 Merge SourceKitD.swift and DynamicallyLoadedSourceKitD.swift
With the merge of `SourceKitD` and `DynamicallyLoadedSourceKitD`, there is no longer a need to have two files.
2025-03-27 11:06:48 -07:00
Alex Hoppen
92aa94fa43 Remove the split between SourceKitD and DynamicallyLoadedSourceKitD
There is only one real class that implements the `SourceKitD` protocol, so there really isn’t any need for the protocol + class split at all. Unify them to make code simpler to reason about.
2025-03-27 11:05:24 -07:00
Alex Hoppen
92f3c7740d Wait for request to be sent to sourcekitd before cancelling it in SwiftSourceKitPluginTests.testCancellation
Otherwise, the cancellation could get handled before the request actually gets sent to sourcekitd, which is not what we want to test here. There appears to be a second underlying issue that causes unexpected results when we cancel the fast completion request after sending the slow completion request. I’ll look at that in a follow-up PR.
2025-03-25 14:08:06 -07:00
Alex Hoppen
c68a9892fe Log sourcekitd requests inside withCancellableCheckedThrowingContinuation
Otherwise, we would log sending the sourcekitd request even if the task was already cancelled and we thus took an early exit in `withCancellableCheckedThrowingContinuation`.
2025-03-10 15:56:11 -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
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
947e5269c4 Reduce the number of public imports 2024-09-30 07:50:12 -07:00
Alex Hoppen
8cd831b55d Adopt InternalImportsByDefault 2024-09-27 09:17:13 -07:00
Alex Hoppen
ea9bf37634 Make SourceKit-LSP build without warnings
Fixes all build warnings in SourceKit-LSP so that it builds without any issues using recent Swift development snapshots (`swift-DEVELOPMENT-SNAPSHOT-2024-07-22-a` and `swift-6.0-DEVELOPMENT-SNAPSHOT-2024-07-24-a`)
2024-07-25 15:45:22 -07:00
Alex Hoppen
2877675bd5 Adopt package access level
Change a l public declarations to the `package` access level, accept for:
- The `LanguageServerProtocol` module
- The `BuildServerProtocol` module
- `InProcessClient.InProcessSourceKitLSPClient`
- `LanguageServerProtocolJSONRPC` (I would like to create a more ergonomic API for this like `InProcessSourceKitLSPClient` in the future, but for now, we’ll leave it public)

Unfortunately, our pattern of marking functions as `@_spi(Testing) public` no longer works with the `package` access level because declarations at the `package` access level cannot be marked as SPI. I have decided to just mark these functions as `package`. Alternatives would be:
- Add an underscore to these functions, like we did for functions exposed for testing before the introduction of `SPI`
- Use `@testable` import in the test targets and mark the methods as `internal`

Resolves #1315
rdar://128295618
2024-07-19 09:54:30 -07:00
Alex Hoppen
191d366a2c Add a maximum duration for sourcekitd requests
VS Code does not cancel semantic tokens requests. If a source file gets into a state where an AST build takes very long, this can cause us to wait for the semantic tokens from sourcekitd for a few minutes, effectively blocking all other semantic functionality in that file.

To circumvent this problem (or any other problem where an editor might not be cancelling requests they are no longer interested in) add a maximum request duration for SourceKitD requests, defaulting to 2 minutes.

rdar://130948453
2024-07-02 22:47:27 +02:00
Alex Hoppen
9618df80a0 Add documentation about each module's purpose and move some files between modules
The purpose of the different modules wasn’t clearly defined, which lead to inconsistent responsibilities between the different modules. Define each module’s purpose and move a few files between modules to satisfy these definitions.

There are a few more larger changes that will need to be made for a fully consistent module structure. These are FIXMEs in the new Modules.md document and I’ll address them in follow-up PRs.
2024-06-25 07:47:45 -07:00
Alex Hoppen
1286407762 Remove sourcekitd test hooks
Turns out that sourcekitd test hooks were a bad idea because of the following comment that I wrote:

```
`testHooks` are only considered when an instance is being created. If a sourcekitd instance at the given path already exists, its test hooks will be used.
```

During test execution in Xcode, we generate a bunch of `SourceKitServer` instances in the same process that all call `DynamicallyLoadedSourceKitD.getOrCreate`. Now, if `testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled` is not the first test being executed in the process (which usually it is not), the test hooks in it won’t get used.

Switch back to using the preparation hooks, essentially reverting https://github.com/apple/sourcekit-lsp/pull/1412 and keeping the following snippet to fix the underlying issue

```swift
// Poll until the `CancelRequestNotification` has been propagated to the request handling.
for _ in 0..<Int(defaultTimeout * 100) {
  if Task.isCancelled {
    break
  }
  usleep(10_000)
}
```
2024-06-05 14:24:09 -07:00
Alex Hoppen
5ac5b5685e Merge pull request #1412 from ahoppen/wait-for-prepration-to-propagate
Wait for cancellation to propagate in `testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled`
2024-06-04 15:12:40 -07:00
Alex Hoppen
37e6a8a65a Wait for cancellation to propagate in testDontReturnEmptyDiagnosticsIfDiagnosticRequestIsCancelled
I saw a few non-deterministic test failures. I think the issue was that handling of the `CancelRequestNotification` is done asynchronously, which left a short window in which the sourcekitd diagnostics request could run and return results instead of being cancelled. Wait for the diagnostic request to actually be cancelled before running it for real.

While doing this, also introduce proper sourcekitd test hooks instead of relying on the preparation test hooks, which just got run as a side effect.
2024-06-04 11:34:11 -07:00
Alex Hoppen
b479b2e874 Create a SwiftExtensions module
This allows us to share common Swift utility functions between SourceKit-LSP and LSPLogging.
2024-06-04 07:06:44 -07:00
Alex Hoppen
3b68f15bc4 Merge pull request #1394 from ahoppen/thread-safe-box-sendable
Require `ThreadSafeBox.T` to be Sendable
2024-06-03 13:13:57 -07:00
Alex Hoppen
c7bf59e2ee Require ThreadSafeBox.T to be Sendable
Otherwise, I think `ThreadSafeBox` might still have data races. This also requires us to make `TestSourceKitLSPClient.RequestHandler` sendable.

rdar://128572489
2024-06-03 10:23:13 -07:00
Alex Hoppen
59672e942e Log when a sourcekitd request gets cancelled
I saw a failure in CI where a sourcekitd diagnostic request got cancelled without an LSP cancellation request. I am suspecting that there is some implicit cancellation going on in sourcekitd despite `key.cancel_builds: 0`, which doesn’t make sense to me. Adding some explicit logging to sourcekit-lsp to check if we cancel the request for some reason.
2024-06-03 08:19:58 -07:00
Alex Hoppen
e8d0a0d431 Make SourceKitD build with strict concurrency enabled 2024-03-12 14:46:34 -07:00
Alex Hoppen
d12b258d99 Rename SourceKitDImpl to DynamicallyLoadedSourceKitD
This makes it clearer how exactly `SourceKitDImpl` interacts with sourcekitd.
2024-02-28 09:28:59 -08:00
Alex Hoppen
5f69147bf9 Remove logging dependency from SourceKitD
Make logging protocol requirements on `SourceKitD` so that implementers of the protocol can decide how to log requests. This allows us to add different kinds of sourcekitd connections that behave differently in the future, for example one that dynamically links against sourcekitd instead of loading it it using `dlopen`.
2024-02-24 09:27:02 -08:00
Alex Hoppen
f5fb23ad4c Rename sourcekitd_ to sourcekitd_api_ and add nullability annotations to sourcekit_functions.h
Naming types in sourcekitd_functions.h `sourcekit_api_` instead of `sourcekitd_` indicates that these are types to be used with dynamically loaded sourcekitd libraries. It avoids confusion if sourcekitd is also linked, which adds the `sourcekitd_` symbols.

Adding nullability annotations to it is also just nice.

And some improved formatting never hurts.
2024-02-23 09:13:42 -08:00
Alex Hoppen
1a23153fd5 When sourcekitd crashes, log the file contents with which it crashed and the request
This should make it a lot easier to reproduce sourcekitd crashes.
2023-10-31 08:30:54 -07:00
Alex Hoppen
47e7a13ce6 Emit signposts for request handling
This allows you to trace sourcekit-lsp in Instruments to get a feeling for how long requests take to be handled.
2023-10-30 13:53:59 -07:00
Alex Hoppen
5bdd6f253b Refactor SwiftLanguageServer to be able to call the async version of SourceKitD.send
Resolves #869
rdar://116703689
2023-10-26 21:38:25 -07:00
Alex Hoppen
f7572c4035 Factor withCancellableCheckedThrowingContinuation into a separate function 2023-10-26 11:03:15 -07:00
Alex Hoppen
b41f6af59d Avoid a potential race condition in which a sourcekitd/clangd request wouldn't get cancelled 2023-10-26 11:03:15 -07:00
Alex Hoppen
3d17caded6 Implement request cancellation
When receiving a `CancellationNotification`, we cancel the task that handles the request with that ID.

This will cause `cancel_notification` to be sent to sourcekitd or a `CancellationNotification` to be sent to `clangd`, which ultimately cancels the request.

rdar://117492860
2023-10-26 11:03:15 -07:00
Alex Hoppen
0a18628f04 Simplify sending of sourcekitd requests
Hoist some of the logic out of `withCheckedContinuation` to make the code easier to read.
2023-10-18 21:46:55 -07:00
Alex Hoppen
f960d7ed9b Change logging to use OSLog
OSLog is the suggesting logging solution on Apple platforms and we should be using it there, taking advantage of the different log levels and privacy masking.

Switch sourcekit-lsp to use OSLog on Apple platforms and implement a logger that is API-compatible with OSLog for all uses in sourcekit-lsp and which can be used on non-Darwin platforms.

The goal of this commit is to introduce the new logging API. There are still improvements about what we log and we can display more privacy-insensitive information after masking. Those changes will be in follow-up commits.
2023-10-13 13:46:32 -07:00
Alex Hoppen
d0fc00ce98 Format using swift-format
Add `.swift-format` to the repo and format the repo with `swift-format`.

This commit does not add any automation to enforce formatting of sourcekit-lsp in CI. The goal of this commit is to get the majority of source changes out of the way so that the diff of actually enforcing formatting will have fewer changes or conflicts.
2023-10-10 13:44:47 -07:00
Alex Hoppen
9e07784fb8 Remove the version of SourceKitD.send that took a completion handler 2023-10-06 18:07:19 -07:00
Alex Hoppen
97a6e1c617 Handle notifications from sourcekitd on a serial queue 2023-10-06 10:49:03 -07:00
Alex Hoppen
6e7b5db9a2 Make hover return the request result as a return value 2023-10-03 08:09:00 -07:00
Bart Whiteley
d2f7f2f3c6 Generate textual Swift interfaces for module references 2022-12-12 12:45:21 -07:00