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

[windows] path: drop the trailing backslash before invoking PathCchRe… #479

Merged
merged 2 commits into from
Aug 5, 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/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,21 @@ private struct WindowsPath: Path, Sendable {
let fsr: UnsafePointer<Int8> = self.string.fileSystemRepresentation
defer { fsr.deallocate() }

let path: String = String(cString: fsr)
var path: String = String(cString: fsr)
// PathCchRemoveFileSpec removes trailing '\' for a
// path like 'c:\root\path\', which doesn't give us the parent
// directory name. Thus, drop the trailing '\' before calling
// PathCchRemoveFileSpec.
var substring = path[path.startIndex..<path.endIndex]
while !substring.isEmpty && substring.utf8.last == UInt8(ascii: "\\") {
substring = substring.dropLast()
}
if !substring.isEmpty && substring.last != ":" {
// Drop the trailing '\', unless the string path only
// has '\', and unless the slashes are right after the drive letter.
path = String(substring)
}
compnerd marked this conversation as resolved.
Show resolved Hide resolved

return path.withCString(encodedAs: UTF16.self) {
let data = UnsafeMutablePointer(mutating: $0)
PathCchRemoveFileSpec(data, path.count)
Expand Down
9 changes: 9 additions & 0 deletions Tests/TSCBasicTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,15 @@ class PathTests: XCTestCase {
XCTAssertEqual(RelativePath("abc").dirname, ".")
XCTAssertEqual(RelativePath("").dirname, ".")
XCTAssertEqual(RelativePath(".").dirname, ".")
#if os(Windows)
XCTAssertEqual(AbsolutePath("C:\\a\\b").dirname, "C:\\a")
XCTAssertEqual(AbsolutePath("C:\\").dirname, "C:\\")
XCTAssertEqual(AbsolutePath("C:\\\\").dirname, "C:\\")
XCTAssertEqual(AbsolutePath("C:\\\\\\").dirname, "C:\\")
XCTAssertEqual(AbsolutePath("C:\\a\\b\\").dirname, "C:\\a")
XCTAssertEqual(AbsolutePath("C:\\a\\b\\\\").dirname, "C:\\a")
XCTAssertEqual(AbsolutePath("C:\\a\\").dirname, "C:\\")
#endif
}

func testBaseNameExtraction() {
Expand Down