Commit Graph

49 Commits

Author SHA1 Message Date
Alex Hoppen
0f8b6a5d2a Add a request to re-index all files in SourceKit-LSP
Users should not need to rely on this request. The index should always be updated automatically in the background. Having to invoke this request manes there is a bug in SourceKit-LSP's automatic re-indexing. It does, however, offer a workaround to re-index files when such a bug occurs where otherwise there would be no workaround.

rdar://127476221
Resolves #1263
2024-06-27 05:14:58 +02:00
Alex Hoppen
2bcd14581e Send SIGKILL to swift-frontend indexing processes
We were sending `SIGINT` to `swift-frontend` processes if they didn’t terminate after 2 minutes. However, `swift-frontend` doesn’t listen to `SIGINT`.

If a task running `waitUntilExitStoppingProcessOnTaskCancellation` is cancelled and the process doesn’t terminate on a `SIGINT` after 2 seconds, kill it.

rdar://130103147
2024-06-26 22:29:15 +02:00
Alex Hoppen
55f6dee5c8 Update links for repositories moved to the swiftlang org on GitHub 2024-06-25 06:57:00 -07:00
Alex Hoppen
c43cffd044 Change all variables that are Atomic* types to not be nonisolated(unsafe)
Since the `Atomic*` types can not be marked as `Sendable` (because they aren’t C structs), we can change the variables to constants and can remove `nonisolated(unsafe)`.
2024-06-07 08:49:10 -07:00
Alex Hoppen
2692d5f7fb Log the exit status of prepare and index processes to the index log 2024-06-06 13:22:07 -07:00
Alex Hoppen
aa176644f6 Merge pull request #1434 from ahoppen/process-priority
Set the priority of processes launched for background indexing
2024-06-06 13:07:24 -07:00
Alex Hoppen
556fd333b5 Heap allocate our atomics
We used C atomics but these were allocated as Swift variables. Even thought they were atomic, concurrent accesses to them could violate Swift’s exclusivity laws, raising thread sanitizer errors.

Allocate the C atomics using malloc to fix this problem.

rdar://129170128
2024-06-05 23:27:47 -07:00
Alex Hoppen
db9662bcc3 Set the priority of processes launched for background indexing
Unfortunately, `setpriority` only allows reduction of a process’s priority and doesn’t support priority elevation (unless you are a super user). I still think that it’s valuable to set the process’s priority based on the task priority when it is launched because many indexing processes never get their priority escalated and should thus run in the background.

On Windows, we can elevate the process’s priority.

rdar://127474245
2024-06-05 23:13:21 -07:00
Alex Hoppen
6eaa4331da Merge pull request #1418 from ahoppen/dont-index-fallback-args
Don’t index files with fallback build settings
2024-06-04 11:20:41 -07:00
Alex Hoppen
1553c9c17a Don’t index files with fallback build settings
Fallback build settings don’t even have an indexstore path set, so we would never pick up any index data generated from them. Also, indexing with fallback args has some other problems:
- If it did generate a unit file, we would consider the file’s index up-to-date even if the compiler arguments change, which basically means that we wouldn’t get any up-to-date-index even when we do get build settings for the file.
- It’s unlikely that the index from a single file with fallback arguments will be very useful as it can’t tie into the rest of the project.
2024-06-04 07:27:37 -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
fff9eb569e Merge pull request #1382 from ahoppen/stream-index-log
Instead of sending a message to the index log when an indexing task finishes, stream results as they come in
2024-06-03 19:33:29 -07:00
Alex Hoppen
09ad77ba8d Instead of sending a message to the index log when an indexing task finishes, stream results as they come in
This also means that you can use the index log to view which tasks are currently being executed.

Since we only have a single log stream we can write to, I decided to prefix every line in the index log with two colored emojis that an easy visual association of every log line to the task that generated them.
2024-06-03 13:21:54 -07:00
Alex Hoppen
80c214b008 Change CheckedIndex to use DocumentURI instead of URL 2024-06-01 12:54:07 -07:00
Alex Hoppen
b08895b82c Add a few more arguments to allow errors during indexing
I noticed that we were missing a bunch more arguments (in particular for clang index invocations), while diagnosing rdar://129071600, which is fixed by passing `-experimental-allow-module-with-compiler-errors` to the Swift index invocations.

rdar://129071600
2024-05-31 15:38:39 -07:00
Alex Hoppen
0f86e01cab Merge pull request #1349 from ahoppen/address-review-comments-1322-1329
Address review comments from #1322 and #1329
2024-05-28 17:30:15 -07:00
Alex Hoppen
a703b72c2f Merge pull request #1354 from ahoppen/timeout-indexstore-update
Time out updating the index store after 2 minutes
2024-05-28 07:47:38 -07:00
Alex Hoppen
3ae6501c25 Time out updating the index store after 2 minutes
Time out updating of the index store after 2 minutes. We don't expect any single file compilation to take longer than 2 minutes in practice, so this indicates that the compiler has entered some kind of loop. We will try indexing the file again when it is edited or when the project is re-opened.

rdar://128732571
2024-05-26 18:50:43 -07:00
Alex Hoppen
9098e09287 Address review comments from #1322 2024-05-25 13:42:46 -07:00
Alex Hoppen
5d75e142d4 Merge pull request #1352 from ahoppen/dont-reindex
Don’t re-index files in languages that cannot be indexed
2024-05-25 13:41:04 -07:00
Alex Hoppen
193ab77dfd Don’t re-index files in languages that cannot be indexed
Swift packages can have source files that we can’t index (like assembly files). When re-opening, we schedule indexing for those files, which requires the targets to be re-prepared. We skip them earlier.

rdar://128711633
2024-05-24 17:13:53 -07:00
Alex Hoppen
eb784ebdd7 Log when the build graph starts being generated and when it finishes
The signposts aren’t easily visible in the log (you need to add `--signpost` to `log show`) and don’t get logged on non-Darwin platforms at all. Add logging for it.
2024-05-24 11:24:32 -07:00
Alex Hoppen
950a74a260 Fix a negation issue that prevented parallel indexing 2024-05-22 13:31:58 -07:00
Alex Hoppen
82637937f6 Add signposts for background preparation and indexing
This allows nice visualization of the indexing progress in Instruments.
2024-05-22 06:46:06 -07:00
Alex Hoppen
3e6319c3b9 Produce an index log for the client
This allows a user of SourceKit-LSP to inspect the result of background indexing. This allows a user of SourceKit-LSP to inspect the result of background indexing. I think this gives useful insights into what SourceKit-LSP is indexing and why/how it fails, if it fails, also for users of SourceKit-LSP.

rdar://127474136
Fixes #1265
2024-05-21 22:18:06 -07:00
Alex Hoppen
41b810b80c Cancel preparation tasks for editor functionality if the preparation task hasn't been started yet and the document is no longer active
When the user opens documents from three targets A, B, and C in quick succession, then we don’t want to schedule preparation of wait until A *and* B are finished preparing before preparing C.

Instead, we want to
- Finish for preparation of A to finish if it has already started by the time the file in C is opened. This is done so we always make progress during preparation and don’t get into a scenario where preparation is always cancelled if a user switches between two targets more quickly than it takes to prepare those targets.
- Not prepare B because it is no longer relevant and we haven’t started any progress here. Essentially, we pretend that the hop to B never happened.
2024-05-21 18:12:24 -07:00
Alex Hoppen
e295a4e95a Split up-to-date status tracking and index progress tracking
We were mixing the up-to-date status and in-progress status of an index task in `SemanticIndexManager`. This meant that a single `QueuedTask` in the task scheduler could be needed for eg. both preparation for editor functionality in a file of that target and to re-index a file in that target. This dual ownership made it unclear, which caller would be entitled to cancel the task. Furthermore, we needed to duplicate some logic from the preparation task dependencies in `SemanticIndexManager.prepare`.

To simplify things:
- Split the up-to-date status and the in-progress status into two different data structures
- Make the caller of `prepare` and `scheduleIndex` responsible for cancellation of the task it has scheduled. `TaskScheduler` might receive more scheduled tasks this way but the additional tasks should all be no-ops because the status is known to be up-to-date when they execute.
2024-05-20 21:01:40 -07:00
Alex Hoppen
d12c946988 Merge pull request #1321 from ahoppen/target-preparation-testing 2024-05-20 20:02:01 -07:00
Alex Hoppen
3f9ff29ee3 Merge pull request #1309 from ahoppen/review-comments-1306
Address review comments to #1306
2024-05-20 13:40:58 -07:00
Alex Hoppen
815fea827e Add infrastructure to test which targets are being prepared 2024-05-20 09:49:24 -07:00
Alex Hoppen
9524753826 Introduce test hooks that can be used to monitor when preparation and index tasks finish 2024-05-17 16:46:14 -07:00
Alex Hoppen
6695859c4b Improve handling of main file vs header file during indexing
Essentially fix two issues in updating the index store:
1. If there was one task to index `HeaderA.h` through `main.c` and one to index `HeaderB.h` through `main.c`, we would not declare a dependency between them in the task scheduler, which meant that we could have two concurrent and racing index tasks for `main.c`. Declare a dependency between any two files that have the same main file
2. `UpdateIndexStoreTaskDescription` was computing the target to index a file in independently of `SemanticIndexManager`. While they currently always line up, we should pass the target in which to index a file to the `UpdateIndexStoreTaskDescription`. Only this way can we guarantee that we actually prepared the target that the file will be indexed in.
2024-05-17 15:28:42 -07:00
Alex Hoppen
5bad2c540f Generalize the removal of compiler argument options during indexing
The existing ad-hoc logic was not quite correct because it didn’t eg. remove `-MT/depfile` because it assumed that `-MT` was followed by a space. It also didn’t take into account that `serialize-diagnostics` can be spelled with a single dash or two dashes.

Create a `CompilerCommandLineOption` type that forces decisions to be made about the dash spelling and argument styles, which should help avoid problems like this in the future.
2024-05-17 14:16:47 -07:00
Alex Hoppen
c58fa70011 Don’t re-index all files that include a header when a header is modified
When a header is modified, we don’t we want to re-index all main files that include it. Instead, we just want to index one main to effectively re-index the header itself.

I originally implemented re-indexing of all files that include the header but on second thought, headers are like Swift modules, where we also don’t re-index all dependencies either. And if you change a low-level header that’s included by the entire project, you probably don’t want the indexer to go off and re-index the entire project.
2024-05-15 20:10:07 -07:00
Alex Hoppen
597932c0b7 Merge pull request #1303 from ahoppen/review-comments-1216
Address review comment to #1216
2024-05-15 15:46:58 -07:00
Alex Hoppen
f10eac3ace Address review comment to #1216 2024-05-15 12:36:27 -07:00
Alex Hoppen
546bb3230e Update index as files are modified on disk
Fixes #1251
rdar://127476161
2024-05-15 11:00:13 -07:00
Alex Hoppen
c9b51f9b34 Allow client request handlers specified on TestSourceKitLSPClient to be executed in any order 2024-05-14 06:38:22 -07:00
Alex Hoppen
0dc5cdd7c7 Call indexTaskDidFinish from SemanticIndexManager
This fixes a bug where `indexTaskDidFinish` would also get called when a task is cancelled to be rescheduled.
2024-05-14 06:38:22 -07:00
Alex Hoppen
5cce99b920 Make IndexTaskDescription protocol-based instead of enum-based
This simplifies the implementation.
2024-05-14 06:38:21 -07:00
Alex Hoppen
0f097e61f0 Merge pull request #1289 from ahoppen/index-c-files
Index clang files
2024-05-14 06:14:00 -07:00
Alex Hoppen
61a21347da Index clang files
Fixes #1253
rdar://127474135
2024-05-13 17:51:15 -07:00
Alex Hoppen
70e373c1fa Make all non-test modules except for SourceKitLSP build in Swift 6 mode
This would have caught a race condition in background indexing that was caused by accessing `CheckedIndex` from multiple threads despite it not being thread-safe.
2024-05-13 10:05:25 -07:00
Alex Hoppen
5e83d7d904 Support background preparation of targets 2024-05-10 11:58:04 -07:00
Alex Hoppen
7b4bbeb39d Address review comments to #1216 2024-05-09 15:19:31 -07:00
Alex Hoppen
a1d10d8bed Fall back to launching subprocesses without a working directory
Details from https://github.com/apple/sourcekit-lsp/issues/1271

> Amazon Linux 2 and CentOS 7 have a glibc that doesn’t support `posix_spawn_file_actions_addchdir_np` and thus `TSCBasic.Process` can’t launch a process on these platforms with the working directory set. We currently fall back to launching the index tasks without a working directory on these platforms, which I think is fine because SwiftPM gives us compiler arguments with absolute paths. But we should figure something out.
>
> Using `Foundation.Process` is not an option because it runs `chdir` on the current process for Posix platforms, which is racy if there are multiple subprocesses being spawned simultaneously. On Windows `TSCBasic.Processs` uses `Foundation.Process` and `Foundation.Process` properly set the working directory of the subprocesses on Windows, so Windows is not a problem.

rdar://127797048
2024-05-09 10:26:23 -07:00
Alex Hoppen
0ae3d80c5c Fix Windows build failure
Windows has `ProcessResult.ExitStatus.abnormal` instead of ` `ProcessResult.ExitStatus.signalled`
2024-05-05 14:53:42 -07:00
Alex Hoppen
a6a76c66cc Conjure up an output file path so that indexing of two files produces different unit files 2024-05-05 14:26:52 -07:00
Alex Hoppen
1a5d1321a1 Implement initial background indexing of a project
Implements an initial background index when the project is opened.

The following will be implemented in follow-up PRs:
- Resolving package dependencies
- Preparing dependent modules
- Watching for file updates
2024-05-05 14:26:52 -07:00