From aa6245bf5152131ad1af5a24ee2da09a3ee325e6 Mon Sep 17 00:00:00 2001 From: Alex Lorenz Date: Thu, 1 Aug 2024 20:39:58 -0700 Subject: [PATCH 1/2] [windows] path: drop the trailing backslash before invoking PathCchRemoveFileSpec to compute the dirname PathCchRemoveFileSpec's is documented to not remove the file spec if it comes with a backslash suffix. Thus, we should remove the trailing backslashes before calling this API, to ensure we actually drop the current file/directory name --- Sources/TSCBasic/Path.swift | 16 +++++++++++++++- Tests/TSCBasicTests/PathTests.swift | 8 ++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) diff --git a/Sources/TSCBasic/Path.swift b/Sources/TSCBasic/Path.swift index 3ceb2252..208f9819 100644 --- a/Sources/TSCBasic/Path.swift +++ b/Sources/TSCBasic/Path.swift @@ -461,7 +461,21 @@ private struct WindowsPath: Path, Sendable { let fsr: UnsafePointer = 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.. Date: Fri, 2 Aug 2024 13:51:11 -0700 Subject: [PATCH 2/2] Test C:\\\ case --- Tests/TSCBasicTests/PathTests.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Tests/TSCBasicTests/PathTests.swift b/Tests/TSCBasicTests/PathTests.swift index d9b85be4..d18ce814 100644 --- a/Tests/TSCBasicTests/PathTests.swift +++ b/Tests/TSCBasicTests/PathTests.swift @@ -134,7 +134,8 @@ class PathTests: XCTestCase { #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:\\\\\\").dirname, "C:\\") XCTAssertEqual(AbsolutePath("C:\\a\\b\\").dirname, "C:\\a") XCTAssertEqual(AbsolutePath("C:\\a\\b\\\\").dirname, "C:\\a") XCTAssertEqual(AbsolutePath("C:\\a\\").dirname, "C:\\")