Skip to content

Mark target preparation out-of-date when it has changed in the build server #2025

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 1 commit into from
Mar 5, 2025
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
32 changes: 32 additions & 0 deletions Sources/SemanticIndex/SemanticIndexManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,38 @@ package final actor SemanticIndexManager {
)
}

package func buildTargetsChanged(_ changes: [BuildTargetEvent]?) async {
let targets = changes?.map(\.target)

if let targets {
var targetsAndDependencies = targets
targetsAndDependencies += await buildSystemManager.targets(dependingOn: Set(targets))
if !targetsAndDependencies.isEmpty {
logger.info(
"""
Marking dependent targets as out-of-date: \
\(String(targetsAndDependencies.map(\.uri.stringValue).joined(separator: ", ")))
"""
)
await preparationUpToDateTracker.markOutOfDate(targetsAndDependencies)
}
} else {
await preparationUpToDateTracker.markAllKnownOutOfDate()
}

await orLog("Scheduling re-indexing of changed targets") {
var sourceFiles = try await self.buildSystemManager.sourceFiles(includeNonBuildableFiles: false)
if let targets {
sourceFiles = sourceFiles.filter { !$0.value.targets.isDisjoint(with: targets) }
}
_ = await scheduleIndexing(
of: sourceFiles.keys,
indexFilesWithUpToDateUnit: false,
priority: .low
)
}
}

/// Returns the files that should be indexed to get up-to-date index information for the given files.
///
/// If `files` contains a header file, this will return a `FileToIndex` that re-indexes a main file which includes the
Expand Down
15 changes: 1 addition & 14 deletions Sources/SourceKitLSP/Workspace.swift
Original file line number Diff line number Diff line change
Expand Up @@ -435,20 +435,7 @@ package final class Workspace: Sendable, BuildSystemManagerDelegate {

package func buildTargetsChanged(_ changes: [BuildTargetEvent]?) async {
await sourceKitLSPServer?.fileHandlingCapabilityChanged()
await orLog("Scheduling re-indexing of changed targets") {
var sourceFiles = try await self.buildSystemManager.sourceFiles(includeNonBuildableFiles: false)
if let changes {
let changedTargets = changes.map(\.target)
sourceFiles = sourceFiles.filter {
!$0.value.targets.isDisjoint(with: changedTargets)
}
}
_ = await semanticIndexManager?.scheduleIndexing(
of: sourceFiles.keys,
indexFilesWithUpToDateUnit: false,
priority: .low
)
}
await semanticIndexManager?.buildTargetsChanged(changes)
await orLog("Scheduling syntactic test re-indexing") {
let testFiles = try await buildSystemManager.testFiles()
await syntacticTestIndex.listOfTestFilesDidChange(testFiles)
Expand Down
63 changes: 63 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1977,6 +1977,69 @@ final class BackgroundIndexingTests: XCTestCase {
return true
}
}

func testRePrepareTargetsWhenBuildServerChanges() async throws {
let project = try await SwiftPMTestProject(
files: [
"LibA/LibA.swift": """
#if ENABLE_FOO
public func foo() {}
#endif
""",
"LibB/LibB.swift": """
import LibA

func test() {
1️⃣foo()
}
""",
],
manifest: """
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "MyLibrary",
targets: [
.target(name: "LibA"),
.target(name: "LibB", dependencies: ["LibA"]),
]
)
""",
enableBackgroundIndexing: true
)

let (uri, positions) = try project.openDocument("LibB.swift")
let hoverWithMissingDependencyDeclaration = try await project.testClient.send(
HoverRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
XCTAssertNil(hoverWithMissingDependencyDeclaration)

let manifestUri = try project.uri(for: "Package.swift")
try """
// swift-tools-version: 5.7

import PackageDescription

let package = Package(
name: "MyLibrary",
targets: [
.target(name: "LibA", swiftSettings: [.define("ENABLE_FOO")]),
.target(name: "LibB", dependencies: ["LibA"]),
]
)
""".write(to: XCTUnwrap(project.uri(for: "Package.swift").fileURL), atomically: true, encoding: .utf8)

project.testClient.send(DidChangeWatchedFilesNotification(changes: [FileEvent(uri: manifestUri, type: .changed)]))

try await repeatUntilExpectedResult {
let hoverAfterAddingDependencyDeclaration = try await project.testClient.send(
HoverRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
return hoverAfterAddingDependencyDeclaration != nil
}
}
}

extension HoverResponseContents {
Expand Down