86 Commits

Author SHA1 Message Date
Karan Lokchandani
55d75954e5 Add if let to guard transform (#2420)
fixes: https://github.com/swiftlang/sourcekit-lsp/issues/1569

mostly works but not sure what to do with many edge cases and has a todo
for switch statements, also this will probably have conflicts with
https://github.com/swiftlang/sourcekit-lsp/pull/2406 marking as draft
till that merges and i can resolve the conflicts.


https://github.com/user-attachments/assets/a6d07f9d-6f09-4330-8cd0-2d24bd6973fb

---------

Signed-off-by: Karan <karanlokchandani@protonmail.com>
2026-01-13 22:46:54 +01:00
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
Alex Hoppen
4c957506e8 Add a code action to remove unused imports in a source file
The idea is pretty simple: When `MemberImportVisibility` is enabled, we know that imports can only affect the current source file. So, we can just try and remove every single `import` declaration in the file, check if a new error occurred and if not, we can safely remove it.
2025-10-22 23:56:09 +02:00
Alex Hoppen
76847dbf27 Do not try to infer build settings from a sibling file if determining the target timed out 2025-10-16 07:44:54 +02:00
Alex Hoppen
9dff775010 Enable InferIsolatedConformances and NonisolatedNonsendingByDefault
This allows us to easily get rid of some `@_inheritActorContext`. The others seem to be a little more tricky and I haven’t spent too much time at trying to figure out how to remove the attribute from those.
2025-10-09 00:46:48 +02:00
Alex Hoppen
868e218e6f Merge pull request #2303 from ahoppen/active-processor-count
Use `activeProcessorCount` instead of `processorCount` in short-lived use-cases
2025-09-23 16:53:34 +02:00
Ben Barham
d4047382c7 Merge pull request #2289 from bnbarham/infinite-root-2
Standardize file paths when attempting to find toolchains
2025-09-22 13:29:17 -07:00
Alex Hoppen
4fe68eebfb Use activeProcessorCount instead of processorCount in short-lived use-cases
According to https://developer.apple.com/documentation/foundation/processinfo/activeprocessorcount

> Whereas the processorCount property reports the number of advertised processing cores, the activeProcessorCount property reflects the actual number of active processing cores on the system. There are a number of different factors that may cause a core to not be active, including boot arguments, thermal throttling, or a manufacturing defect.

For short-lived workloads like `concurrentMap` we want to parallelize across the number of cores that are currently active, so use `activeProcessorCount` instead. The only case where we want to continue using `processorCount` is the computation of concurrent tasks for `TaskScheduler` because the value is stored for the lifetime of the SourceKit-LSP process and we don’t want to limit parallelism if SourceKit-LSP was launched during a time of thermal throttling.

I stumbled across this while working on #2302
2025-09-22 09:51:41 +02:00
Ben Barham
4e870214bf Standardize file paths when attempting to find toolchains
`containingXCToolchain` loops infinitely when given eg. `C:\foo\..\bar`.
The underlying cause is that `deletingLastPathComponent` does not remove
the `..` on Windows. On macOS it's potentially worse - it *adds* `..`.
We don't see the infinite loop on macOS/Linux though because
`AbsolutePath` already removes them (which is not the case on Windows).

Resolves #2174.
2025-09-20 19:09:18 -07:00
Alex Hoppen
fa67c22235 Merge pull request #2277 from ahoppen/bsp-timeouts
Add a timeout for `workspace/buildTargets` and `buildTarget/sources` requests
2025-09-20 13:13:40 +02:00
Ben Barham
ad69881000 Use native root check on Windows
Not sure exactly what paths are triggering this on Windows, as all the
logs in #2174 are within `C:\`. But the stacktrace seems to strongly
imply that we have an infinite loop here, so let's see if switching to
the native check fixes this now that
https://github.com/swiftlang/swift-foundation/issues/976 is in.

Resolves #2174
2025-09-08 19:45:28 -07:00
Alex Hoppen
c142b601ff Add a timeout for workspace/buildTargets and buildTarget/sources requests
This allows us to provide functionality based on fallback settings for unresponsive BSP servers.

Fixes #2252
2025-09-01 16:12:41 +02:00
Alex Hoppen
fbaa7ce75e When PipeAsStringHandler is destroyed, ensure that we flush any pending data
Otherwise, we would not handle the last line if it didn’t end with a newline character.
2025-08-28 09:05:14 +02:00
Alex Hoppen
65a3402ee1 Merge pull request #2253 from ahoppen/fix-warnings 2025-08-26 07:28:09 +02:00
Alex Hoppen
509ac6f9be Replace a couple of #if conditions by runtime checks
This ensures that we build all branches on all platforms, reducing the likelihood of build failures on other platforms.
2025-08-25 23:33:17 +02:00
Alex Hoppen
3762e7a2a4 Fix all warnings
Fixes all warnings when building on macOS and Linux using Swift 6.1, Swift 6.2 and SDKs from Xcode 16.2 and Xcode 16.3
2025-08-25 14:40:16 +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
0b3db5e49b Fix a couple build warnings 2025-08-04 12:32:39 +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
250081ec58 Fix theoretical issue in withTaskPriorityChangedHandler that could always report a priority change to high
We were launching the task that watches for the priority change with `high` priority. If the base priority was `medium`, this should immediately report a priority change to `high`. It appears the only reason why this doesn’t happen right now is due to rdar://147868544.
2025-03-27 15:07:45 -07:00
Alex Hoppen
3fb72cddb6 Merge pull request #2089 from ahoppen/async-fixes
Be a little more pedantic about making sure that we cancel  `bodyTask` and `timeoutTask` in `withTimeout` and `withTaskPriorityChangedHandler`
2025-03-27 15:04:03 -07:00
Alex Hoppen
a691ee3440 Be a little more pedantic about making sure that we cancel bodyTask and timeoutTask in withTimeout and withTaskPriorityChangedHandler
I have not been able to reason out any concrete issues that this might fix but these are my best guesses to fix the underlying issue of https://github.com/swiftlang/sourcekit-lsp/pull/2088
2025-03-25 14:13:52 -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
dd2d2aff49 Fix some more quadratic performance issues in AsyncQueue
We would hit quadratic behavior in `AsyncQueue` when the build system floods us with `build/logMessage` or `build/task(Start|Progress|Finish)` notifications because we record a dependency on all of the pending log message handling tasks.

We can extend the improvement made in https://github.com/swiftlang/sourcekit-lsp/pull/1840 to fix this quadratic problem: If the current task depends on a task with metadata that depends on itself (ie. all tasks of metadata that needs to be executed in-order), we only need to depend on the last task with that metadata.

This covers many message types and we can now only get into quadratic behavior if we get flooded with two different kinds of messages: One that does not have a self-dependency and one that depends on the message without a self-dependency. In terms of LSP messages, this could be a document read followed by a document update, but I think this is a lot more unlikely than getting spammed with one type of message.
2025-03-21 17:33:55 -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
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
58bce8225f Add a concurrentForEach method to start a concurrent task for each element in a collection
This is simpler to reason about than creating a `TaskGroup`.
2025-02-26 16:27:56 -08:00
Alex Hoppen
c67c06e7b1 Merge pull request #1950 from ahoppen/gardening
Gardening
2025-01-24 22:33:51 -08:00
Alex Hoppen
64f2aef446 Fix build warnings
Quite a few of these were reminders to clean things up once we no longer need to support testing using compilers and sourcekitd from older toolchains.
2025-01-23 21:11:08 -08:00
Alex Hoppen
15b9670888 Split build systems for JSON compilation database and fixed compilation database
I feel like the implementations are actually simpler if we split them. This will also allow us to add more advanced logic to the JSON compilation database build system in the future, such as inferring the toolchain from the compile command.
2025-01-17 14:38:37 -08:00
Alex Hoppen
69121eed95 Merge pull request #1906 from ahoppen/add-sourcekit-plugin
Add a SourceKit plugin to handle code completion requests
2025-01-15 12:42:18 -08:00
Alex Hoppen
16f897aefa Make the SourceKit plugins work on Windows 2025-01-11 06:57:03 +01:00
Alex Hoppen
718c0ee809 Remove imports of SwiftPM modules that are not strictly necessary 2025-01-08 12:43:43 +01:00
Alex Hoppen
03d58e8415 Normalize Windows drive letter to be uppercase
VS Code spells file paths with a lowercase drive letter, while the rest of Windows APIs use an uppercase drive letter. Normalize the drive letter spelling to be uppercase.

Fixes #1855
rdar://141001203
2024-12-10 16:16:27 -08:00
Alex Hoppen
87af5b06f1 Merge pull request #1857 from ahoppen/no-implicit-completion-cancellation 2024-12-07 08:56:38 -08:00
Alex Hoppen
1c1a1cf5f6 Cached transformed results in Cache
The transform to get the transformed result might be expensive, so we should cache its result.
2024-12-06 11:59:25 -08:00
Alex Hoppen
990ec7fc2a Don’t implicitly cancel code completion requests on document edits
As the user types, we filter the code completion results. Cancelling the completion request on every keystroke means that we will never build the initial list of completion results for this code completion session if building that list takes longer than the user's typing cadence (eg. for global completions) and we will thus not show any completions.
2024-12-05 14:43:56 -08:00
Alex Hoppen
eb982d5b1e Fix quadratic performance issue in AsyncQueue<Serial>
Adding an item to `AsyncQueue` was linear in the number of pending queue items, thus adding n items to an `AsyncQueue` before any can execute is in O(n^2). This decision was made intentionally because the primary use case for `AsyncQueue` was to track pending LSP requests, of which we don’t expect to have too many pending requests at any given time.

While we can't fix the quadratic performance issue in general, we can resolve the quadratic issue of `AsyncQueue<Serial>` by making a new task only depend on the last item in the queue, which then transitively depends on all the previous items. `AsyncQueue<Serial>` are the queues that are most likely to contain many items.

Fixes #1725
rdar://137886469
2024-11-22 15:33:57 +01:00
Alex Hoppen
be546308ca Use URL in many cases where we used AbsolutePath
We made quite a few fixes recently to make sure that path handling works correctly using `URL` on Windows. Use `URL` in most places to have a single type that represents file paths instead of sometimes using `AbsolutePath`.

While doing so, also remove usages of `TSCBasic.FileSystem` an `InMemoryFileSystem`. The pattern of using `InMemoryFileSystem` for tests was never consistently used and it was a little confusing that some types took a `FileSystem` parameter while other always assumed to work on the local file system.
2024-11-18 18:19:48 -08:00
Alex Hoppen
9c84a344c8 Merge pull request #1817 from ahoppen/bsp-review 2024-11-14 08:10:02 -08:00
Alex Hoppen
bb6f4b4a47 Allow waiting for the next notification from SourceKit-LSP when the previous wait timed out
When `TestSourceKitLSPClient.nextNotification` timed out, it would cancel `AsyncSequence.Iterator.next()` and thus also cancel the underlying `AsyncStream` (which I only now found out). This means that calling `nextNotification` again would never return any notification. Hence, if we didn’t receive a `PublishDiagnosticsNotification` within the first timeout, we would never get one.
2024-11-13 16:53:08 -08:00
Alex Hoppen
c1e090ef33 Address minor review comments 2024-11-13 10:09:31 -08:00
Alex Hoppen
820b454994 Merge pull request #1780 from ahoppen/withtimeout-priority-escalation
Fix a race condition that caused `withTimeout` to not escalate the priority of the body
2024-10-29 13:02:18 -07:00
Alex Hoppen
0b7b565783 Fix a race condition that caused withTimeout to not escalate the priority of the body
rdar://137678566
2024-10-24 16:24:07 -07:00
Alex Hoppen
5d47358236 Merge pull request #1762 from ahoppen/build-settings-timeout 2024-10-23 23:50:58 -07:00
Alex Hoppen
cbc8aed404 Guard access-level import by compiler(>=6)
This allows us to build SourceKit-LSP using Swift 5.10 again.
2024-10-23 09:34:40 -07:00
Alex Hoppen
951e923245 Use withUnsafeFileSystemRepresentation to get the path of a URL on disk
`URL.path` returns forward slashes in the path on Windows (https://github.com/swiftlang/swift-foundation/issues/973) where we expect backslashes. Work around that by defining our own `filePath` property that is backed by `withUnsafeFileSystemRepresentation`, which produces backslashes.

rdar://137963660
2024-10-21 11:12:30 -07:00