Skip to content
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

Fix relative symlinks when using --follow-symlinks #837

Merged
merged 5 commits into from
Oct 18, 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
8 changes: 4 additions & 4 deletions Sources/SwiftFormat/Utilities/FileIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
else {
break
}
next = URL(fileURLWithPath: destination)
next = URL(fileURLWithPath: destination, relativeTo: next)
fallthrough

case .typeDirectory:
Expand All @@ -97,12 +97,12 @@ public struct FileIterator: Sequence, IteratorProtocol {
output = next
}
}
if let out = output, visited.contains(out.absoluteURL.standardized.path) {
if let out = output, visited.contains(out.standardizedFileURL.path) {
output = nil
}
}
if let out = output {
visited.insert(out.absoluteURL.standardized.path)
visited.insert(out.standardizedFileURL.path)
}
return output
}
Expand Down Expand Up @@ -135,7 +135,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
else {
break
}
path = destination
path = URL(fileURLWithPath: destination, relativeTo: item).path
fallthrough

case .typeRegular:
Expand Down
16 changes: 14 additions & 2 deletions Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ final class FileIteratorTests: XCTestCase {
try touch("project/.hidden.swift")
try touch("project/.build/generated.swift")
try symlink("project/link.swift", to: "project/.hidden.swift")
try symlink("project/rellink.swift", relativeTo: ".hidden.swift")
}

override func tearDownWithError() throws {
Expand Down Expand Up @@ -55,7 +56,10 @@ final class FileIteratorTests: XCTestCase {
// passed to the iterator. This is meant to avoid situations where a symlink could be hidden by
// shell expansion; for example, if the user writes `swift-format --no-follow-symlinks *`, if
// the current directory contains a symlink, they would probably *not* expect it to be followed.
let seen = allFilesSeen(iteratingOver: [tmpURL("project/link.swift")], followSymlinks: false)
let seen = allFilesSeen(
iteratingOver: [tmpURL("project/link.swift"), tmpURL("project/rellink.swift")],
followSymlinks: false
)
XCTAssertTrue(seen.isEmpty)
}
}
Expand All @@ -81,14 +85,22 @@ extension FileIteratorTests {
}
}

/// Create a symlink between files or directories in the test's temporary space.
/// Create a absolute symlink between files or directories in the test's temporary space.
private func symlink(_ source: String, to target: String) throws {
try FileManager.default.createSymbolicLink(
at: tmpURL(source),
withDestinationURL: tmpURL(target)
)
}

/// Create a relative symlink between files or directories in the test's temporary space.
private func symlink(_ source: String, relativeTo target: String) throws {
try FileManager.default.createSymbolicLink(
atPath: tmpURL(source).path,
withDestinationPath: target
)
}

/// Computes the list of all files seen by using `FileIterator` to iterate over the given URLs.
private func allFilesSeen(iteratingOver urls: [URL], followSymlinks: Bool) -> [String] {
let iterator = FileIterator(urls: urls, followSymlinks: followSymlinks)
Expand Down