Skip to content

Commit

Permalink
TSCBasic: normalise the drive letter spelling on Windows
Browse files Browse the repository at this point in the history
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!
  • Loading branch information
compnerd committed Jun 15, 2023
1 parent 9b384c8 commit 11a9611
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
8 changes: 7 additions & 1 deletion Sources/TSCBasic/Path.swift
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,13 @@ private struct WindowsPath: Path, Sendable {
}

init(string: String) {
self.string = string
if string.first?.isASCII ?? false, string.first?.isLetter ?? false, string.first?.isLowercase ?? false,
string[string.index(string.startIndex, offsetBy: 1)] == ":"
{
self.string = "\(string.first!.uppercased())\(string.dropFirst(1))"
} else {
self.string = string
}
}

private static func repr(_ path: String) -> String {
Expand Down
15 changes: 15 additions & 0 deletions Tests/TSCBasicTests/PathTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -404,4 +404,19 @@ class PathTests: XCTestCase {
// FIXME: We also need tests for dirname, basename, suffix, etc.

// FIXME: We also need test for stat() operations.

#if os(Windows)
func testNormalization() {
XCTAssertEqual(
AbsolutePath(#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#)
.pathString,
#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#
)
XCTAssertEqual(
AbsolutePath(#"c:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#)
.pathString,
#"C:\Users\compnerd\AppData\Local\Programs\Swift\Toolchains\0.0.0+Asserts\usr\bin\swiftc.exe"#
)
}
#endif
}

0 comments on commit 11a9611

Please sign in to comment.