Skip to content

Commit 5f92a78

Browse files
TSCBasic: normalise the drive letter spelling on Windows
Windows normalises the drive letter to an uppercase letter. However, some environments (e.g. nodejs) use the lower case spelling. This results in a path spelling difference which triggers a rebuild in the case that the LSP is running within VSCode. Normalise the spelling to the upper case always when building an `AbsolutePath`. Based on a patch by Ami Fischman! Co-authored-by: Ami Fischman <[email protected]>
1 parent 9b384c8 commit 5f92a78

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,13 @@ private struct WindowsPath: Path, Sendable {
501501
}
502502

503503
init(string: String) {
504-
self.string = string
504+
if string.first?.isASCII ?? false, string.first?.isLetter ?? false, string.first?.isLowercase ?? false,
505+
string.count > 1, string[string.index(string.startIndex, offsetBy: 1)] == ":"
506+
{
507+
self.string = "\(string.first!.uppercased())\(string.dropFirst(1))"
508+
} else {
509+
self.string = string
510+
}
505511
}
506512

507513
private static func repr(_ path: String) -> String {

Tests/TSCBasicTests/PathTests.swift

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -404,4 +404,19 @@ class PathTests: XCTestCase {
404404
// FIXME: We also need tests for dirname, basename, suffix, etc.
405405

406406
// FIXME: We also need test for stat() operations.
407+
408+
#if os(Windows)
409+
func testNormalization() {
410+
XCTAssertEqual(
411+
AbsolutePath(#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#)
412+
.pathString,
413+
#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#
414+
)
415+
XCTAssertEqual(
416+
AbsolutePath(#"c:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#)
417+
.pathString,
418+
#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#
419+
)
420+
}
421+
#endif
407422
}

0 commit comments

Comments
 (0)