Skip to content

Commit

Permalink
Fix infinite searching for .swift-format file on Linux
Browse files Browse the repository at this point in the history
Due to swiftlang/swift-foundation#980, the we may end up with a `path == ""` instead of `/`.

We didn’t catch this in the test because we only end up with an empty path when searching for a `.swift-format` file from a `.swift-file` that was not at the root of a directory.
  • Loading branch information
ahoppen committed Oct 11, 2024
1 parent 7ee11c7 commit 3e9e9a0
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 1 deletion.
4 changes: 3 additions & 1 deletion Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -480,7 +480,9 @@ fileprivate extension URL {
// https://github.com/swiftlang/swift-format/issues/844
return self.pathComponents.count == 1
#else
return self.path == "/"
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
return self.path == "/" || self.path == ""
#endif
}
}
9 changes: 9 additions & 0 deletions Tests/SwiftFormatTests/API/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,13 @@ final class ConfigurationTests: XCTestCase {
#endif
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}

func testMissingConfigurationFileInSubdirectory() {
#if os(Windows)
let path = #"C:\whatever\test.swift"#
#else
let path = "/whatever/test.swift"
#endif
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}
}

0 comments on commit 3e9e9a0

Please sign in to comment.