Commit Graph

452 Commits

Author SHA1 Message Date
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
a26b0b7b3a Resolve swiftly when referenced in compile commands
When the compiler in `compile_commands.json` references a `swift` executable that’s a symlink to `swiftly`, SourceKit-LSP got confused because the `swift` executable doesn’t reside in a real toolchain, causing us to not provide any semantic functionality.

When we discover that the `swift` executable reference in compile commands references a `swiftly` executable, use `swiftly use -p` to resolve the binary in the real toolchain and continue operating based on that.

Fixes #2128
rdar://150301344
2025-05-09 09:12:21 +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
6a8ea4cd97 Merge pull request #2105 from ahoppen/test-timeout-cancellation
Fix issue that caused request cancellation after a timeout in tests to not work
2025-04-09 15:23:28 -07:00
Alex Hoppen
705ae0d699 Require Swift 6.1 to build SourceKit-LSP
Now that Swift 6.1 has been released, we no longer need to support building SourceKit-LSP using Swift 6.1.
2025-04-08 16:09:16 -07: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
24f361cda2 Merge pull request #2093 from ahoppen/remove-dynamically-loaded-sourcekitd
Remove the split between `SourceKitD` and `DynamicallyLoadedSourceKitD`
2025-03-28 14:17:27 -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
374554bde7 Merge pull request #2092 from ahoppen/official-synchronization
Make `workspace/synchronize` a non-experimental request
2025-03-27 15:05:22 -07:00
Alex Hoppen
93d59cbe18 Merge pull request #2091 from ahoppen/time-imprecise
Add delay before writing file to disk in tests
2025-03-27 15:03:51 -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
4883ee7088 Merge pull request #2088 from ahoppen/cancellation-issue
Wait for request to be sent to sourcekitd before cancelling it in `SwiftSourceKitPluginTests.testCancellation`
2025-03-26 14:39:25 -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
0f5727f4f0 Introduce assertContains to check that a sequence/string contains an element/substring
This also prints the sequence and expected element on failure, which is useful for debugging.
2025-03-25 19:12:51 -07:00
Alex Hoppen
3528e52bcb Add delay before writing file to disk in tests
Depending on the system, mtime resolution might not be perfectly accurate. Particularly containers appear to have imprecise mtimes.
Wait a short time period before writing the new file to avoid situations like the following:
 - We index a source file and the unit receives a time stamp and wait for indexing to finish
 - We modify the source file but so quickly after the unit has been modified that the updated source file
   receives the same mtime as the unit file
 - We now assume that the we have an up-to-date index for this source file even though we do not.

Waiting 10ms appears to be enough to avoid this situation on the systems we care about.

rdar://147811044
2025-03-25 18:59:01 -07:00
Alex Hoppen
3bb4690db4 Terminate pending background indexing and preparation tasks when shutting down SourceKit-LSP
When SourceKit-LSP is shut down, we should make sure that we don’t leave behind child processes, which will become orphans after SourceKit-LSP has terminated. What’s worse, when SourceKit-LSP has exited, these processes might not have any process to read their stdout/stderr, which can lead to them running indefinitely.

This change does not cover the termination of subprocess trees. For example, if we launch `swift build` and need to kill it because it doesn’t honor SIGINT, its child processes will still live on. Similarly, if we kill a BSP server, its child processes might live on. Fixing this is a drastically bigger endeavor, likely requiring changes to Foundation and/or TSC. I filed https://github.com/swiftlang/sourcekit-lsp/issues/2080 for it.
2025-03-25 14:10:38 -07:00
Alex Hoppen
d5e254137c Make fulfillmentOfOrThrow take a variadic list of expectations
In almost all cases, we pass a single expectation and that looks a lot nicer with a variadic parameter.
2025-03-25 09:16:17 -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
87dd406af8 Merge pull request #2077 from ahoppen/shutdown-semaphore
Add a timeout to the semaphore we use for shutting down SourceKit-LSP
2025-03-21 14:30:30 -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
Saleem Abdulrasool
684bfe419a SKTestSupport: remove hardcoded version for XCTest
The XCTest and Testing version information is encoded into the
`PlatformInfo.plist` that is at the root of the platform. Use this to
determine the path for XCTest. This allows us to migrate the XCTest
location into an appropriate versioned directory.
2025-03-19 20:24:52 -07:00
Alex Hoppen
71e74045f2 If the compiler arguments only contain -o, add the output path as -index-unit-output-path to the adjusted options
Otherwise, we would strip away the `-o`, leaving the command line without any output path option and thus not able to generate a unit file (which requires an output path).
2025-03-18 21:54:10 -07:00
Alex Hoppen
cef212f03b Merge pull request #2038 from ahoppen/multi-target-support
Support indexing a file in the context of multiple targets
2025-03-18 13:56:06 -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
7734c3d16e Merge pull request #2067 from ahoppen/trailing-comma
Remove trailing comma
2025-03-17 15:27:46 -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
3cc39df8a6 Merge pull request #2066 from ahoppen/expected-formatting
Fix expected output in swift-format `SkipUnless` check
2025-03-14 19:42:19 -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
88b40f3077 Use Windows file paths for fake output paths 2025-03-14 15:50:12 -07:00
Alex Hoppen
4991e1560c Re-index header files when they are modified 2025-03-14 15:50:11 -07:00
Alex Hoppen
d10c868497 Support indexing a file in the context of multiple targets
If a source file is part of multiple targets, we should index it in the context of all of those targets because the different targets may produce different USRs since they might use different build settings to interpret the file.
2025-03-14 15:49:59 -07:00
Alex Hoppen
c771daee1e Add standard initialization response for custom build server that supports background indexing 2025-03-14 15:12:38 -07:00
Alex Hoppen
cb17643752 Make a couple parameters in the BuildTarget initializer defaulted 2025-03-14 15:12:38 -07:00
Alex Hoppen
23780ca7f1 Merge pull request #2061 from ahoppen/normalized-path
Do not apply file path normalization in `mainFiles(containing:)` to the file itself
2025-03-14 15:10:23 -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
35b6c1236c Fix expected output in swift-format SkipUnless check
We weren’t hitting this in Swift CI because we only test against Swift 6.2 toolchains, which take an earlier exit based on the Swift version.
2025-03-14 14:43:29 -07:00
Alex Hoppen
6eb800625a Merge pull request #2062 from ahoppen/skip-format-tests
Skip formatting tests if the `swift-format` executable in the host toolchain doesn’t support `-` to indicate that it’s reading the source file from stdin
2025-03-13 20:17:00 -07:00
Alex Hoppen
7f1e90f194 Skip formatting tests if the swift-format executable in the host toolchain doesn’t support - to indicate that it’s reading the source file from stdin 2025-03-13 14:12:03 -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
e198b4b4d7 Do not apply file path normalization in mainFiles(containing:) to the file itself
Do not apply the standardized file normalization to the source file itself. Otherwise we would get the following behavior:
 - We have a build system that uses standardized file paths and index a file as /tmp/test.c
 - We are asking for the main files of /private/tmp/test.c
 - Since indexstore-db uses realpath for everything, we find the unit for /tmp/test.c as a unit containing /private/tmp/test.c, which has /private/tmp/test.c as the main file.
 - If we applied the path normalization, we would normalize /private/tmp/test.c to /tmp/test.c, thus reporting that /tmp/test.c is a main file containing /private/tmp/test.c,
But that doesn't make sense (it would, in fact cause us to treat /private/tmp/test.c as a header file that we should index using /tmp/test.c as a main file.
2025-03-12 18:06:17 -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
d645f44735 Merge pull request #2054 from ahoppen/file-change-freestanding
Handle file did change notifications as freestanding messages
2025-03-12 14:21:42 -07:00
Alex Hoppen
ca960bc9c2 Introduce a MultiEntrySemaphore for testing
Compare to the `while !AtomicBool.value { sleep }` pattern, this eventually times out, avoiding a timeout of the entire `swift test` invocation.
2025-03-11 09:20:44 -07:00
Alex Hoppen
572660ae26 Add timeouts to most subprocess calls in tests
Just a speculative fix to avoid timeouts during the `swift test` invocation
2025-03-11 09:20:44 -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
956742d34f Merge pull request #2049 from ahoppen/cancellation-custom-build-server
Support cancellation in `CustomBuildServer`
2025-03-11 08:22:43 -07:00
Alex Hoppen
ce75053555 Handle file did change notifications as freestanding messages
Technically, the watched files notification can change the response of any other request (eg. because a target needs to be re-prepared). But treating it as a `globalConfiguration` inserts a lot of barriers in request  handling and significantly prevents parallelism. Since many editors batch file change notifications already, they might have delayed the file change notification even more, which is equivalent to handling the  notification a little later inside SourceKit-LSP. Thus, treating it as `freestanding` should be acceptable.

Also, simplify the logic needed in tests to write modified files to disk because we now need to run a barrier request in tests to ensure that the watched file notification has been handled.
2025-03-10 21:44:35 -07:00
Alex Hoppen
64147963d0 Support cancellation in CustomBuildServer
I developed this for a test case that didn’t end up being useful. But supporting request cancellation here might come in useful in the future.
2025-03-10 15:25:50 -07:00