Skip to content
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
8 changes: 8 additions & 0 deletions Sources/SemanticIndex/CheckedIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -355,11 +355,14 @@ private struct IndexOutOfDateChecker {

private enum Error: Swift.Error, CustomStringConvertible {
case fileAttributesDontHaveModificationDate
case circularSymlink(URL)

var description: String {
switch self {
case .fileAttributesDontHaveModificationDate:
return "File attributes don't contain a modification date"
case .circularSymlink(let url):
return "Circular symlink at \(url)"
}
}
}
Expand Down Expand Up @@ -517,13 +520,18 @@ private struct IndexOutOfDateChecker {
}
var modificationDate = try Self.modificationDate(atPath: fileURL.filePath)

var visited: Set<URL> = [fileURL]

// Get the maximum mtime in the symlink chain as the modification date of the URI. That way if either the symlink
// is changed to point to a different file or if the underlying file is modified, the modification time is
// updated.
while let relativeSymlinkDestination = try? FileManager.default.destinationOfSymbolicLink(
atPath: fileURL.filePath
) {
fileURL = URL(fileURLWithPath: relativeSymlinkDestination, relativeTo: fileURL)
if !visited.insert(fileURL).inserted {
throw Error.circularSymlink(fileURL)
}
modificationDate = max(modificationDate, try Self.modificationDate(atPath: fileURL.filePath))
}

Expand Down
18 changes: 18 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -2521,6 +2521,24 @@ final class BackgroundIndexingTests: XCTestCase {
let workspaceSymbolsAfterUpdate = try await project.testClient.send(WorkspaceSymbolsRequest(query: "myTest"))
XCTAssertEqual(workspaceSymbolsAfterUpdate?.compactMap(\.symbolInformation?.name), ["myTestA_updated", "myTestB"])
}

func testCircularSymlink() async throws {
let project = try await SwiftPMTestProject(
files: [
"Symlink.swift": ""
],
enableBackgroundIndexing: true
)
let circularSymlink = try XCTUnwrap(project.uri(for: "Symlink.swift").fileURL)
try FileManager.default.removeItem(at: circularSymlink)
try FileManager.default.createSymbolicLink(at: circularSymlink, withDestinationURL: circularSymlink)

project.testClient.send(
DidChangeWatchedFilesNotification(changes: [FileEvent(uri: URI(circularSymlink), type: .changed)])
)
// Check that we don't enter an infinite loop trying to index the circular symlink.
try await project.testClient.send(SynchronizeRequest(index: true))
}
}

extension HoverResponseContents {
Expand Down