Skip to content

Commit f3bc044

Browse files
committed
Manually normalizing relative path on Windows
1 parent c048ed9 commit f3bc044

File tree

1 file changed

+8
-16
lines changed

1 file changed

+8
-16
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -620,21 +620,14 @@ private struct UNIXPath: Path {
620620
}
621621

622622
init(normalizingRelativePath path: String) {
623+
let pathSeparator: Character
623624
#if os(Windows)
624-
var result: PWSTR?
625-
defer { LocalFree(result) }
626-
627-
_ = path.replacingOccurrences(of: "/", with: "\\").withCString(encodedAs: UTF16.self) {
628-
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &result)
629-
}
630-
631-
var canonicalized: String = String(decodingCString: result!, as: UTF16.self)
632-
if canonicalized == "" || canonicalized == "\\" {
633-
canonicalized = "."
634-
}
635-
self.init(string: canonicalized)
625+
pathSeparator = "\\"
626+
let path = path.replacingOccurrences(of: "/", with: "\\")
636627
#else
637-
precondition(path.first != "/")
628+
pathSeparator = "/"
629+
#endif
630+
precondition(path.first != pathSeparator)
638631

639632
// FIXME: Here we should also keep track of whether anything actually has
640633
// to be changed in the string, and if not, just return the existing one.
@@ -644,7 +637,7 @@ private struct UNIXPath: Path {
644637
// the normalized string representation.
645638
var parts: [String] = []
646639
var capacity = 0
647-
for part in path.split(separator: "/") {
640+
for part in path.split(separator: pathSeparator) {
648641
switch part.count {
649642
case 0:
650643
// Ignore empty path components.
@@ -683,7 +676,7 @@ private struct UNIXPath: Path {
683676
if let first = iter.next() {
684677
result.append(contentsOf: first)
685678
while let next = iter.next() {
686-
result.append("/")
679+
result.append(pathSeparator)
687680
result.append(contentsOf: next)
688681
}
689682
}
@@ -694,7 +687,6 @@ private struct UNIXPath: Path {
694687

695688
// If the result is empty, return `.`, otherwise we return it as a string.
696689
self.init(string: result.isEmpty ? "." : result)
697-
#endif
698690
}
699691

700692
init(validatingAbsolutePath path: String) throws {

0 commit comments

Comments
 (0)