Skip to content

Commit 3e9e9a0

Browse files
committed
Fix infinite searching for .swift-format file on Linux
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.
1 parent 7ee11c7 commit 3e9e9a0

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

Sources/SwiftFormat/API/Configuration.swift

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -480,7 +480,9 @@ fileprivate extension URL {
480480
// https://github.com/swiftlang/swift-format/issues/844
481481
return self.pathComponents.count == 1
482482
#else
483-
return self.path == "/"
483+
// On Linux, we may end up with an string for the path due to https://github.com/swiftlang/swift-foundation/issues/980
484+
// TODO: Remove the check for "" once https://github.com/swiftlang/swift-foundation/issues/980 is fixed.
485+
return self.path == "/" || self.path == ""
484486
#endif
485487
}
486488
}

Tests/SwiftFormatTests/API/ConfigurationTests.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,4 +26,13 @@ final class ConfigurationTests: XCTestCase {
2626
#endif
2727
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
2828
}
29+
30+
func testMissingConfigurationFileInSubdirectory() {
31+
#if os(Windows)
32+
let path = #"C:\whatever\test.swift"#
33+
#else
34+
let path = "/whatever/test.swift"
35+
#endif
36+
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
37+
}
2938
}

0 commit comments

Comments
 (0)