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 support for FileIterator when working directory is / #865

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Sources/SwiftFormat/Utilities/FileIterator.swift
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public struct FileIterator: Sequence, IteratorProtocol {
// be displayed as relative paths. Otherwise, they will still be displayed as absolute
// paths.
let relativePath =
path.hasPrefix(workingDirectory.path)
path.hasPrefix(workingDirectory.path) && FileManager.default.currentDirectoryPath != "/"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@compnerd What will this do on Windows? Probably not what's intended, I think?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my concern, I don't know how Foundation will deal with that there.

Copy link
Author

@macshome macshome Oct 28, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FileManager.default.currentDirectoryPath will never be / on Windows because Windows always has a drive letter, eg. C:\. Checking if a URL is a root path on Windows is not quite trivial because you also need to account for networks shares eg. \\your-server\blah and the only good way of doing it issuing the PathCchIsRoot Windows API (eg. https://github.com/swiftlang/swift-tools-support-core/blob/5b130e04cc939373c4713b91704b0c47ceb36170/Sources/TSCBasic/Path.swift#L491).

But maybe it’s not really an issue on Windows anyway, depending on how test output looks like. This is why I suggested making workingDirectory changeable from tests and then we can just check.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know that it is an issue on Windows either. Most people seem to have discovered it when using WASM. Are there Windows and Linux runners for CI so that we can see if this breaks anything? Otherwise I can install a toolchain on Windows here and check.

I can still make workingDirectory changeable from tests, but at this point I'm worried about the validity of the entire approach on Windows.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suppose I could also implement the isRoot property from the swift-tools-support-core as well in FileIterator. That way it would work on Windows as well.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests do fail on Windows when trying to change the working directory. I'll update everything and get it working.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don’t have a Windows machine to test this on, just write code that works to the best of you knowledge and some test cases for it. We have Windows PR testing (we just need to approve runs) and I’m happy to get the Windows code over the finish line if there are minor issues.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set a toolchain up yesterday on my gaming machine so I will at least be sure that we pass the tests everywhere.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, that’s great. Let me know if you have any questions.

? String(path.dropFirst(workingDirectory.path.count + 1))
: path
output =
Expand Down
40 changes: 30 additions & 10 deletions Tests/SwiftFormatTests/Utilities/FileIteratorTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ final class FileIteratorTests: XCTestCase {
#endif
let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: false)
XCTAssertEqual(seen.count, 2)
XCTAssertTrue(seen.contains { $0.hasSuffix("project/real1.swift") })
XCTAssertTrue(seen.contains { $0.hasSuffix("project/real2.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real1.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real2.swift") })
}

func testFollowSymlinks() throws {
Expand All @@ -41,10 +41,10 @@ final class FileIteratorTests: XCTestCase {
#endif
let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: true)
XCTAssertEqual(seen.count, 3)
XCTAssertTrue(seen.contains { $0.hasSuffix("project/real1.swift") })
XCTAssertTrue(seen.contains { $0.hasSuffix("project/real2.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real1.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/real2.swift") })
// Hidden but found through the visible symlink project/link.swift
XCTAssertTrue(seen.contains { $0.hasSuffix("project/.hidden.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.hidden.swift") })
}

func testTraversesHiddenFilesIfExplicitlySpecified() throws {
Expand All @@ -56,8 +56,8 @@ final class FileIteratorTests: XCTestCase {
followSymlinks: false
)
XCTAssertEqual(seen.count, 2)
XCTAssertTrue(seen.contains { $0.hasSuffix("project/.build/generated.swift") })
XCTAssertTrue(seen.contains { $0.hasSuffix("project/.hidden.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.build/generated.swift") })
XCTAssertTrue(seen.contains { $0.path.hasSuffix("project/.hidden.swift") })
}

func testDoesNotFollowSymlinksIfFollowSymlinksIsFalseEvenIfExplicitlySpecified() {
Expand All @@ -71,6 +71,26 @@ final class FileIteratorTests: XCTestCase {
)
XCTAssertTrue(seen.isEmpty)
}

func testDoesNotTrimFirstCharacterOfPathIfRunningInRoot() throws {
// Make sure that we don't drop the begining of the path if we are running in root.
// https://github.com/swiftlang/swift-format/issues/862
FileManager.default.changeCurrentDirectoryPath("/")
let seen = allFilesSeen(iteratingOver: [tmpdir], followSymlinks: false)
XCTAssertEqual(seen.count, 2)
XCTAssertTrue(seen.contains { $0.path.hasPrefix("/private/var") })
XCTAssertTrue(seen.contains { $0.path.hasPrefix("/private/var") })
}

func testShowsRelativePaths() throws {
// Make sure that we still show the relative path if using them.
// https://github.com/swiftlang/swift-format/issues/862
FileManager.default.changeCurrentDirectoryPath(tmpdir.path)
let seen = allFilesSeen(iteratingOver: [URL(fileURLWithPath: ".")], followSymlinks: false)
XCTAssertEqual(seen.count, 2)
XCTAssertTrue(seen.contains { $0.relativePath == "project/real1.swift" })
XCTAssertTrue(seen.contains { $0.relativePath == "project/real2.swift" })
}
}

extension FileIteratorTests {
Expand Down Expand Up @@ -111,11 +131,11 @@ extension FileIteratorTests {
}

/// 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] {
private func allFilesSeen(iteratingOver urls: [URL], followSymlinks: Bool) -> [URL] {
let iterator = FileIterator(urls: urls, followSymlinks: followSymlinks)
var seen: [String] = []
var seen: [URL] = []
for next in iterator {
seen.append(next.path)
seen.append(next)
}
return seen
}
Expand Down