diff --git a/Sources/SwiftExtensions/URLExtensions.swift b/Sources/SwiftExtensions/URLExtensions.swift index 71cdee8a9..d14d26d74 100644 --- a/Sources/SwiftExtensions/URLExtensions.swift +++ b/Sources/SwiftExtensions/URLExtensions.swift @@ -12,6 +12,10 @@ package import Foundation +#if os(Windows) +import WinSDK +#endif + enum FilePathError: Error, CustomStringConvertible { case noFileSystemRepresentation(URL) case noFileURL(URL) @@ -83,14 +87,14 @@ extension URL { } package 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. - 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 + get throws { + let checkPath = try filePath + #if os(Windows) + return checkPath.withCString(encodedAs: UTF16.self, PathCchIsRoot) + #else + return checkPath == "/" + #endif + } } /// Returns true if the path of `self` starts with the path in `other`. diff --git a/Sources/ToolchainRegistry/Toolchain.swift b/Sources/ToolchainRegistry/Toolchain.swift index 13dd0dfe3..7c3c957af 100644 --- a/Sources/ToolchainRegistry/Toolchain.swift +++ b/Sources/ToolchainRegistry/Toolchain.swift @@ -369,8 +369,8 @@ public final class Toolchain: Sendable { func containingXCToolchain( _ path: URL ) -> (XCToolchainPlist, URL)? { - var path = path - while !path.isRoot { + var path = path.standardizedFileURL + while !((try? path.isRoot) ?? true) { if path.pathExtension == "xctoolchain" { if let infoPlist = orLog("Loading information from xctoolchain", { try XCToolchainPlist(fromDirectory: path) }) { return (infoPlist, path) diff --git a/Tests/ToolchainRegistryTests/ToolchainRegistryTests.swift b/Tests/ToolchainRegistryTests/ToolchainRegistryTests.swift index 929add581..f834e605e 100644 --- a/Tests/ToolchainRegistryTests/ToolchainRegistryTests.swift +++ b/Tests/ToolchainRegistryTests/ToolchainRegistryTests.swift @@ -336,7 +336,7 @@ final class ToolchainRegistryTests: XCTestCase { try ProcessEnv.setVar( "SOURCEKIT_PATH_FOR_TEST", - value: ["/bogus", binPath.filePath, "/bogus2"].joined(separator: separator) + value: ["/bogus/../parent", "/bogus", binPath.filePath, "/bogus2"].joined(separator: separator) ) defer { try! ProcessEnv.setVar("SOURCEKIT_PATH_FOR_TEST", value: "") }