Commit Graph

67 Commits

Author SHA1 Message Date
Alex Hoppen
c6b82a7556 Fix a compilation issue caused by #1533 and #1524 racing 2024-06-28 22:54:52 +02:00
Alex Hoppen
6b88cfe294 Merge pull request #1533 from ahoppen/background-indexing-configuration-option
Make background indexing a proper option in `SourceKitLSPOptions`
2024-06-28 22:14:19 +02:00
Alex Hoppen
bfe1a24110 Support using SourceKit-LSP for projects that are cross-compiled
We previously always assumed that the project was being built for the host.

Fixes #786
Fixes #1475
rdar://129662080
rdar://113099964
2024-06-28 10:28:46 +02:00
Alex Hoppen
d8e0fff6da Make background indexing a proper option in SourceKitLSPOptions
This allows us to flip the default in the future more easily. It also allows users to disable background indexing when it’s enabled by default.

rdar://130280855
2024-06-28 10:23:29 +02:00
Alex Hoppen
d1cddb8c41 Allow configuring of SourceKit-LSP’s options using .sourcekit-lsp configuration files
The idea here is to unify the different ways in which we can currently set options on SourceKit-LSP in a scalable way: Environment variables, command line arguments to `sourcekit-lsp` and initialization options.

The idea is that a user can define a `~/.sourcekit-lsp/.sourcekit-lsp` file (we store logs in `~/.sourcekit-lsp/logs` on non-Darwin platforms), which will be used as the default configuration for all SourceKit-LSP instances. They can also place a `.sourcekit-lsp` file in the root of a workspace to configure SourceKit-LSP for that project specifically, eg. setting arguments that need to be passed to `swift build` for that project and which thus also need to be set on SourceKit-LSP.

For compatibility reasons, I’m mapping the existing command line options into the new options structure for now. I hope to delete the command line arguments in the future and solely rely on `.sourcekit-lsp` configuration files.

Environment variable will be migrated to `.sourcekit-lsp` in a follow-up commit.
2024-06-27 17:36:16 +02:00
Alex Hoppen
96f6e69dd8 Merge pull request #1513 from ahoppen/swiftlang-migration
Update links for repositories moved to the swiftlang org on GitHub
2024-06-25 21:09:10 -07:00
Alex Hoppen
da1f6dae80 Update links for repositories moved to the swiftlang org on GitHub 2024-06-25 04:34:28 -07:00
Alex Hoppen
89bd9b0372 Add test case that we don't reload the package if a .swift file get added in a folder that doesn't affect compilation
rdar://128573306
2024-06-22 09:06:40 -07:00
Alex Hoppen
4d90f40f72 Watch for changes to Package.resolved
We need to watch for changes to `Package.resolved` so that we can update the dependency checkouts in `.index-build` when the user runs `swift package update`.
2024-06-21 07:19:57 -07:00
Alex Hoppen
8642768902 Fix a few Swift concurrency warnings 2024-06-18 18:07:22 -07:00
Alex Hoppen
c9e6348302 Merge pull request #1447 from ahoppen/atomics-not-unsafe
Change all variables that are `Atomic*` types to not be `nonisolated(unsafe)`
2024-06-12 09:12:31 -07:00
Alex Hoppen
7f90508e51 Add a document to describe which log level to use
Also change a few log levels and make all log messages consistently start with an uppercase letter.
2024-06-11 12:01:50 -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
76a0db79be Merge pull request #1445 from ahoppen/use-build-setup
Inspect the build setup to determine the extra flags to pass to the `swift build` invocations during preparation
2024-06-06 16:23:29 -07:00
Alex Hoppen
b89d267930 Inspect the build setup to determine the extra flags to pass to the swift build invocations during preparation
The `BuildParameters` that we were previously inspecting contained some parameters that SwiftPM synthesizes and that weren’t specified on the command line. For example, it contains `-g` for all languages. This causes us to add `-Xswiftc -g -Xcc -g -Xcxx -g` to the `swift build` invocations for preparation, which is not necessary.
2024-06-06 13:38:28 -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
ce7f36e9ec Let the build system determine which toolchain to use for a document
This allows us to fix a toolchain when using a `SwiftPMBuildSystem`, which is critical to ensure that a target gets prepared using the same toolchain that is used to index it and that is used for sourcekitd.
2024-06-06 09:33:00 -07:00
Alex Hoppen
efabae8808 Take run destinations into account for SwiftPM build targets
We can have two targets with the same name in a SwiftPM workspace, one for a build target and one for the destination. We need to be able to tell them apart based on the run destination.
2024-06-06 09:33:00 -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
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
f98da773a9 Make passing --experimental-prepare-for-indexing to swift build an experimental feature
I’d like to qualify `--experimental-prepare-for-indexing` independently of background indexing using `swift build`. Because of this, I think there is value in using SourceKit-LSP using background indexing but without `--experimental-prepare-for-indexing`. It could also be useful to determine if bugs we may find are due to `--experimental-prepare-for-indexing` or also occur when running plain `swift build` commands.
2024-06-03 15:22:00 -07:00
Alex Hoppen
f203b3a527 Merge pull request #1395 from ahoppen/uri-everywhere
Use `DocumentURI` instead of `URL` in more locations
2024-06-03 14:05:00 -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
3c5d8b9119 Change SwiftPMBuildSystem to use DocumentURI instead of URL 2024-06-03 11:26:19 -07:00
Alex Hoppen
259d49e12c Share module cache between test projects
This improves serial test execution time of eg. `DocumentTestDiscoveryTests` from 36s to 22s because we don’t need to re-build the XCTest module from its interface when using an open source toolchain.

This also uncovered that we weren‘t passing the build setup flags to the prepare command.

rdar://126493151
2024-06-03 09:29:16 -07:00
Alex Hoppen
e56c71f4b3 Don’t run a swift build command to prepare a package manifest
Package manifests don’t have an associated target to prepare and are represented by a `ConfiguredTarget` with an empty target ID. We were mistakingly running `swift build` with an empty target name to prepare them, which failed. There is nothing to prepare.
2024-05-31 21:45:12 -07:00
Alex Hoppen
b7a502c1f4 When SwiftPM supports --experimental-prepare-for-indexing pass it to the prepare command
The main purpose for now is that this makes it easier for me to live on background indexing combined with https://github.com/apple/swift-package-manager/pull/7574.
2024-05-31 15:50:33 -07:00
Alex Hoppen
0b6ad2725f Merge pull request #1371 from ahoppen/tests-pass-on-released-xcode
Make tests pass with Xcode 15.4
2024-05-31 15:44:07 -07:00
Pavel Yaskevich
d439d12871 Merge pull request #1361 from xedin/buildplan-parameter-split
[SwiftPMBuildSystem] Adjust `BuildParameters` use to indicate a desti…
2024-05-30 20:17:55 -07:00
Alex Hoppen
6d4953b3ef Make tests pass with Xcode 15.4
We are no longer skipping these tests, which means that we need to make them account for different error messages emitted by Swift 5.10.

I added warnings behind `#if compiler(>=6.1)` to give us a reminder that we can remove these checks when we no longer support running SourceKit-LSP with SwiftPM from Swift 5.10. Swift 6.1 doesn’t have to be this cut-off point but it’s the most likely candidate for now if we want to support the current and last Swift version from tip SourceKit-LSP.
2024-05-30 19:23:45 -07:00
Pavel Yaskevich
12b848ed58 [SwiftPMBuildSystem] Adjust BuildParameters use to indicate a destination
The change is introduced by https://github.com/apple/swift-package-manager/pull/7593
2024-05-30 15:39:50 -07:00
Alex Hoppen
942bb0a6c3 Merge pull request #1366 from ahoppen/fix-modules-include-directory
Support running tests that require building with a Swift 5.10 toolchain
2024-05-30 07:49:50 -07:00
Alex Hoppen
48df05478c Support running tests that require building with a Swift 5.10 toolchain
When we detect that we’re running using a 5.10 toolchain, adjust the compiler arguments that we received from SwiftPM to drop any `/Modules` suffixes in the build directory, to account for the fact that 5.10 stores the modules one directory higher up (https://github.com/apple/swift-package-manager/pull/7103).
2024-05-29 14:20:17 -07:00
Alex Hoppen
e87d9e8d5e Show message if background indexing is enabled but the workspace doesn’t support background indexing
If the user has enabled background indexing in sourcekit-lsp but opens a project that doesn’t support background indexing (compilation database, build server), we should show a message after opening the workspace, informing the user that background indexing is only supported in SwiftPM projects at the moment.

Fixes #1255
rdar://127474711
2024-05-28 14:12:19 -07:00
Alex Hoppen
f85d821839 Don’t cause any file system file effects when trying to find an implicit workspace for a file
When looking for a workspace that can handle a file, we were creating full-fledged workspaces along the way, which we would then discard if they couldn’t handle the file being opened. This had multiple problems:
1. When background indexing is enabled, it caused semantic indexing of the workspace, which wrote files to a `.index-build` directory and was a waste of work
2. When background indexing is enabled, it caused package resolution, which also created a `.index-build` folder to be created
3. It caused a syntactic test index of the workspace, which was a waste of work.

To fix this, do multiple things:
1. When creating a workspace, add a check right after build system creation. This allows us to early exit if the build system can’t handle the file and prevents us from generating the `Workspace`, fixing (1) and (3)
2. Don’t call `reloadPackage` when creating a `SwiftPMWorkspace`. Instead, explicitly call `generateBuildGraph` once we committed to creating the workspace.
2024-05-28 08:29:55 -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
44acd0d550 Merge pull request #1325 from ahoppen/fix-topological-sorting
Fix issue that caused topological order to be incorrect
2024-05-21 22:03:41 -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
ce34ec25a3 Fix issue that caused topological order to be incorrect 2024-05-21 18:05:11 -07:00
Alex Hoppen
af6cdeba37 Merge pull request #1313 from ahoppen/only-mark-dependent-targets-as-out-of-date
When a file is changed, only mark targets that depend on it as out-of-date
2024-05-20 13:40:51 -07:00
Alex Hoppen
a850cb6849 When a file is changed, only mark targets that depend on it as out-of-date 2024-05-17 14:32:49 -07:00
Alex Hoppen
372673f3c2 Address review comments to #1249 2024-05-17 06:23:08 -07:00
Alex Hoppen
72083762a4 When interacting with a document, prepare the target it belongs to
Whenever we get request for a document, open it or edit it, trigger a preparation of its target, but don’t block any interaction based on it. This should ensure that the target is usually prepared when the user is interacting with it.

We need to track the preparation status of targets somewhat accurately in `SemanticIndexManager`, so we don’t unnecessarily re-prepare a target. When updating the index store, it is acceptable to schedule another `UpdateIndexStoreTaskDescription` because it will early exit based on an `mtime` check with the unit file. Null builds of a target take significantly longer and thus we want to avoid them.

Fixes #1252
rdar://127474003
2024-05-16 10:40:47 -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
31f2674b80 Don’t rely on working directory during index preparation
Amazon Linux and CentOS don’t support working directory. We can work around this by passing `--package-path` to `swift build`.

rdar://128037023
2024-05-13 21:07:18 -07:00
Alex Hoppen
740262cb24 Don’t set forceResolvedVersions to true when we have an index build directory
When `SwiftPMBuildSystem` operates on a ` .index-build` directory, it owns the checkouts and is thus also allowed to resolve the package versions. This is necessary
2024-05-10 14:43:32 -07:00
Alex Hoppen
13176b012d Address review comments 2024-05-10 14:43:32 -07:00