Skip to content

TSCBasic: adjust AbsolutePath(_:relativeTo:) on Windows #320

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

Merged
merged 1 commit into from
May 13, 2022
Merged
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
31 changes: 17 additions & 14 deletions Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -76,25 +76,28 @@ public struct AbsolutePath: Hashable {
self.init(str)
} else {
#if os(Windows)
var joined: PWSTR!
_ = basePath._impl.string.withCString(encodedAs: UTF16.self) { base in
str.withCString(encodedAs: UTF16.self) { path in
PathAllocCombine(base, path, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &joined)
}
assert(!basePath.pathString.isEmpty)
guard !str.isEmpty else {
self.init(basePath._impl)
return
}
defer { LocalFree(joined) }

let buffer: UnsafePointer<Int8> =
String(decodingCString: joined, as: UTF16.self).fileSystemRepresentation
defer { buffer.deallocate() }
let base: UnsafePointer<Int8> =
basePath.pathString.fileSystemRepresentation
defer { base.deallocate() }

let path: UnsafePointer<Int8> = str.fileSystemRepresentation
defer { path.deallocate() }

var canonical: PWSTR!
_ = String(cString: buffer).withCString(encodedAs: UTF16.self) {
PathAllocCanonicalize($0, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &canonical)
var pwszResult: PWSTR!
_ = String(cString: base).withCString(encodedAs: UTF16.self) { pwszBase in
String(cString: path).withCString(encodedAs: UTF16.self) { pwszPath in
PathAllocCombine(pwszBase, pwszPath, ULONG(PATHCCH_ALLOW_LONG_PATHS.rawValue), &pwszResult)
}
}
defer { LocalFree(canonical) }
defer { LocalFree(pwszResult) }

self.init(String(decodingCString: canonical, as: UTF16.self))
self.init(String(decodingCString: pwszResult, as: UTF16.self))
#else
self.init(basePath, RelativePath(str))
#endif
Expand Down