Skip to content

Commit 5023cab

Browse files
committed
TSCBasic: normalize the path before canonicalization
Normalize the path before canonicalization as it may fail to properly canonicalize otherwise. Furthermore, this ensures that we always create the normalized path string.
1 parent 3f140d7 commit 5023cab

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -570,11 +570,17 @@ private struct UNIXPath: Path {
570570

571571
init(normalizingAbsolutePath path: String) {
572572
#if os(Windows)
573-
var buffer: [WCHAR] = Array<WCHAR>(repeating: 0, count: Int(MAX_PATH + 1))
574-
_ = path.withCString(encodedAs: UTF16.self) {
575-
PathCanonicalizeW(&buffer, $0)
576-
}
577-
self.init(string: String(decodingCString: buffer, as: UTF16.self))
573+
let normalized: UnsafePointer<Int8> = path.fileSystemRepresentation
574+
defer { normalized.deallocate() }
575+
576+
self.init(string: String(cString: normalized)
577+
.withCString(encodedAs: UTF16.self) { pwszPath in
578+
var canonical: PWSTR!
579+
_ = PathAllocCanonicalize(pwszPath,
580+
ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue),
581+
&canonical)
582+
return String(decodingCString: canonical, as: UTF16.self)
583+
})
578584
#else
579585
precondition(path.first == "/", "Failure normalizing \(path), absolute paths should start with '/'")
580586

0 commit comments

Comments
 (0)