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

[6.0.2] Fix infinite searching for .swift-format file on Windows and Linux #850

Merged
merged 1 commit into from
Oct 12, 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
16 changes: 15 additions & 1 deletion Sources/SwiftFormat/API/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ public struct Configuration: Codable, Equatable {
if FileManager.default.isReadableFile(atPath: candidateFile.path) {
return candidateFile
}
} while candidateDirectory.path != "/"
} while !candidateDirectory.isRoot

return nil
}
Expand Down Expand Up @@ -370,3 +370,17 @@ public struct NoAssignmentInExpressionsConfiguration: Codable, Equatable {

public init() {}
}

fileprivate extension URL {
var isRoot: Bool {
#if os(Windows)
// FIXME: We should call into Windows' native check to check if this path is a root once https://github.com/swiftlang/swift-foundation/issues/976 is fixed.
// https://github.com/swiftlang/swift-format/issues/844
return self.pathComponents.count == 1
#else
// 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
}
}
26 changes: 26 additions & 0 deletions Tests/SwiftFormatTests/API/ConfigurationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,30 @@ final class ConfigurationTests: XCTestCase {

XCTAssertEqual(defaultInitConfig, emptyJSONConfig)
}

func testMissingConfigurationFile() {
#if os(Windows)
let path = #"C:\test.swift"#
#else
let path = "/test.swift"
#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)))
}

func testMissingConfigurationFileMountedDirectory() throws {
#if !os(Windows)
try XCTSkipIf(true, #"\\ file mounts are only a concept on Windows"#)
#endif
let path = #"\\mount\test.swift"#
XCTAssertNil(Configuration.url(forConfigurationFileApplyingTo: URL(fileURLWithPath: path)))
}
}