Skip to content

When interacting with a document, prepare the target it belongs to #1307

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion Sources/SKCore/BuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
//===----------------------------------------------------------------------===//

import BuildServerProtocol
import LSPLogging
import LanguageServerProtocol

import struct TSCBasic.AbsolutePath
Expand Down Expand Up @@ -50,7 +51,7 @@ public struct SourceFileInfo: Sendable {

/// A target / run destination combination. For example, a configured target can represent building the target
/// `MyLibrary` for iOS.
public struct ConfiguredTarget: Hashable, Sendable {
public struct ConfiguredTarget: Hashable, Sendable, CustomLogStringConvertible {
/// An opaque string that represents the target.
///
/// The target's ID should be generated by the build system that handles the target and only interpreted by that
Expand All @@ -67,6 +68,14 @@ public struct ConfiguredTarget: Hashable, Sendable {
self.targetID = targetID
self.runDestinationID = runDestinationID
}

public var description: String {
"\(targetID)-\(runDestinationID)"
}

public var redactedDescription: String {
"\(targetID.hashForLogging)-\(runDestinationID.hashForLogging)"
}
}

/// An error build systems can throw from `prepare` if they don't support preparation of targets.
Expand Down
27 changes: 13 additions & 14 deletions Sources/SKSwiftPMWorkspace/SwiftPMBuildSystem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,9 @@ public actor SwiftPMBuildSystem {
logger.log(level: diagnostic.severity.asLogLevel, "SwiftPM log: \(diagnostic.description)")
})

/// Whether the SwiftPMBuildSystem may modify `Package.resolved` or not.
///
/// This is `false` if the `SwiftPMBuildSystem` is pointed at a `.index-build` directory that's independent of the
/// user's build. In this case `SwiftPMBuildSystem` is allowed to clone repositories even if no `Package.resolved`
/// exists.
private let forceResolvedVersions: Bool
/// Whether the `SwiftPMBuildSystem` is pointed at a `.index-build` directory that's independent of the
/// user's build.
private let isForIndexBuild: Bool

/// Creates a build system using the Swift Package Manager, if this workspace is a package.
///
Expand All @@ -148,13 +145,13 @@ public actor SwiftPMBuildSystem {
toolchainRegistry: ToolchainRegistry,
fileSystem: FileSystem = localFileSystem,
buildSetup: BuildSetup,
forceResolvedVersions: Bool,
isForIndexBuild: Bool,
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void = { _ in }
) async throws {
self.workspacePath = workspacePath
self.fileSystem = fileSystem
self.toolchainRegistry = toolchainRegistry
self.forceResolvedVersions = forceResolvedVersions
self.isForIndexBuild = isForIndexBuild

guard let packageRoot = findPackageDirectory(containing: workspacePath, fileSystem) else {
throw Error.noManifest(workspacePath: workspacePath)
Expand Down Expand Up @@ -234,7 +231,7 @@ public actor SwiftPMBuildSystem {
url: URL,
toolchainRegistry: ToolchainRegistry,
buildSetup: BuildSetup,
forceResolvedVersions: Bool,
isForIndexBuild: Bool,
reloadPackageStatusCallback: @escaping (ReloadPackageStatus) async -> Void
) async {
do {
Expand All @@ -243,7 +240,7 @@ public actor SwiftPMBuildSystem {
toolchainRegistry: toolchainRegistry,
fileSystem: localFileSystem,
buildSetup: buildSetup,
forceResolvedVersions: forceResolvedVersions,
isForIndexBuild: isForIndexBuild,
reloadPackageStatusCallback: reloadPackageStatusCallback
)
} catch Error.noManifest {
Expand Down Expand Up @@ -272,7 +269,7 @@ extension SwiftPMBuildSystem {

let modulesGraph = try self.workspace.loadPackageGraph(
rootInput: PackageGraphRootInput(packages: [AbsolutePath(projectRoot)]),
forceResolvedVersions: forceResolvedVersions,
forceResolvedVersions: !isForIndexBuild,
observabilityScope: observabilitySystem.topScope
)

Expand Down Expand Up @@ -430,6 +427,8 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
for target in targets {
try await prepare(singleTarget: target)
}
let filesInPreparedTargets = targets.flatMap { self.targets[$0.targetID]?.buildTarget.sources ?? [] }
await fileDependenciesUpdatedDebouncer.scheduleCall(Set(filesInPreparedTargets.map(DocumentURI.init)))
}

private func prepare(singleTarget target: ConfiguredTarget) async throws {
Expand Down Expand Up @@ -561,9 +560,9 @@ extension SwiftPMBuildSystem: SKCore.BuildSystem {
// The file watching here is somewhat fragile as well because it assumes that the `.swiftmodule` files are being
// written to a directory within the workspace root. This is not necessarily true if the user specifies a build
// directory outside the source tree.
// All of this shouldn't be necessary once we have background preparation, in which case we know when preparation of
// a target has finished.
if events.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) {
// If we have background indexing enabled, this is not necessary because we call `fileDependenciesUpdated` when
// preparation of a target finishes.
if !isForIndexBuild, events.contains(where: { $0.uri.fileURL?.pathExtension == "swiftmodule" }) {
filesWithUpdatedDependencies.formUnion(self.fileToTarget.keys.map { DocumentURI($0.asURL) })
}
await self.fileDependenciesUpdatedDebouncer.scheduleCall(filesWithUpdatedDependencies)
Expand Down
Loading