Skip to content

Take the maximum modification date in a symlink change as the mtime of the file #1572

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
Jul 25, 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
24 changes: 20 additions & 4 deletions Sources/SemanticIndex/CheckedIndex.swift
Original file line number Diff line number Diff line change
Expand Up @@ -326,15 +326,31 @@ private struct IndexOutOfDateChecker {
}
}

private static func modificationDate(atPath path: String) throws -> Date {
let attributes = try FileManager.default.attributesOfItem(atPath: path)
guard let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date else {
throw Error.fileAttributesDontHaveModificationDate
}
return modificationDate
}

private func modificationDateUncached(of uri: DocumentURI) throws -> ModificationTime {
do {
guard let fileURL = uri.fileURL else {
guard var fileURL = uri.fileURL else {
return .fileDoesNotExist
}
let attributes = try FileManager.default.attributesOfItem(atPath: fileURL.resolvingSymlinksInPath().path)
guard let modificationDate = attributes[FileAttributeKey.modificationDate] as? Date else {
throw Error.fileAttributesDontHaveModificationDate
var modificationDate = try Self.modificationDate(atPath: fileURL.path)

// 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.path),
let symlinkDestination = URL(string: relativeSymlinkDestination, relativeTo: fileURL)
{
fileURL = symlinkDestination
modificationDate = max(modificationDate, try Self.modificationDate(atPath: fileURL.path))
}

return .date(modificationDate)
} catch let error as NSError where error.domain == NSCocoaErrorDomain && error.code == NSFileReadNoSuchFileError {
return .fileDoesNotExist
Expand Down
61 changes: 61 additions & 0 deletions Tests/SourceKitLSPTests/BackgroundIndexingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,67 @@ final class BackgroundIndexingTests: XCTestCase {
// also testing that we don't wait for type checking of Test.swift to finish.
XCTAssert(Date().timeIntervalSince(dateStarted) < 30)
}

func testRedirectSymlink() async throws {
let project = try await SwiftPMTestProject(
files: [
"/original.swift": """
func original() {
foo()
}
""",
"/updated.swift": """
func updated() {
foo()
}
""",
"test.swift": """
func 1️⃣foo() {}
""",
],
workspaces: { scratchDirectory in
let symlink =
scratchDirectory
.appendingPathComponent("Sources")
.appendingPathComponent("MyLibrary")
.appendingPathComponent("symlink.swift")
try FileManager.default.createSymbolicLink(
at: symlink,
withDestinationURL: scratchDirectory.appendingPathComponent("original.swift")
)
return [WorkspaceFolder(uri: DocumentURI(scratchDirectory))]
},
enableBackgroundIndexing: true
)

let (uri, positions) = try project.openDocument("test.swift")

let prepare = try await project.testClient.send(
CallHierarchyPrepareRequest(textDocument: TextDocumentIdentifier(uri), position: positions["1️⃣"])
)
let initialItem = try XCTUnwrap(prepare?.only)
let callsBeforeRedirect = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
XCTAssertEqual(callsBeforeRedirect?.only?.from.name, "original()")

let symlink =
project.scratchDirectory
.appendingPathComponent("Sources")
.appendingPathComponent("MyLibrary")
.appendingPathComponent("symlink.swift")
try FileManager.default.removeItem(at: symlink)
try FileManager.default.createSymbolicLink(
at: symlink,
withDestinationURL: project.scratchDirectory.appendingPathComponent("updated.swift")
)

project.testClient.send(
DidChangeWatchedFilesNotification(changes: [FileEvent(uri: DocumentURI(symlink), type: .changed)])
)
try await project.testClient.send(PollIndexRequest())

let callsAfterRedirect = try await project.testClient.send(CallHierarchyIncomingCallsRequest(item: initialItem))
XCTAssertEqual(callsAfterRedirect?.only?.from.name, "updated()")
}
}

extension HoverResponseContents {
Expand Down