Skip to content

Commit 2e45582

Browse files
authored
Merge pull request #72 from egorzhdan/master
Fix search for the Swift compiler on Windows
2 parents 6f8a6d6 + bd00544 commit 2e45582

File tree

2 files changed

+22
-12
lines changed

2 files changed

+22
-12
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,12 @@ private struct UNIXPath: Path {
421421
return name != "" && name != "." && name != ".." && !name.contains("/")
422422
}
423423

424+
#if os(Windows)
425+
static func isAbsolutePath(_ path: String) -> Bool {
426+
return !path.withCString(encodedAs: UTF16.self, PathIsRelativeW)
427+
}
428+
#endif
429+
424430
var dirname: String {
425431
#if os(Windows)
426432
let dir = string.deletingLastPathComponent
@@ -445,7 +451,11 @@ private struct UNIXPath: Path {
445451
}
446452

447453
var isAbsolute: Bool {
448-
string.hasPrefix("/")
454+
#if os(Windows)
455+
return UNIXPath.isAbsolutePath(string)
456+
#else
457+
return string.hasPrefix("/")
458+
#endif
449459
}
450460

451461
var basename: String {
@@ -632,7 +642,7 @@ private struct UNIXPath: Path {
632642
defer { fsr.deallocate() }
633643

634644
let realpath = String(cString: fsr)
635-
if realpath.withCString(encodedAs: UTF16.self, PathIsRelativeW) {
645+
if !UNIXPath.isAbsolutePath(realpath) {
636646
throw PathValidationError.invalidAbsolutePath(path)
637647
}
638648
self.init(normalizingAbsolutePath: path)
@@ -654,7 +664,7 @@ private struct UNIXPath: Path {
654664
defer { fsr.deallocate() }
655665

656666
let realpath: String = String(cString: fsr)
657-
if !realpath.withCString(encodedAs: UTF16.self, PathIsRelativeW) {
667+
if UNIXPath.isAbsolutePath(realpath) {
658668
throw PathValidationError.invalidRelativePath(path)
659669
}
660670
self.init(normalizingRelativePath: path)

Sources/TSCBasic/misc.swift

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,16 +54,16 @@ public func getEnvSearchPaths(
5454
currentWorkingDirectory: AbsolutePath?
5555
) -> [AbsolutePath] {
5656
// Compute search paths from PATH variable.
57-
return (pathString ?? "").split(separator: ":").map(String.init).compactMap({ pathString in
58-
// If this is an absolute path, we're done.
59-
if pathString.first == "/" {
60-
return AbsolutePath(pathString)
61-
}
62-
// Otherwise convert it into absolute path relative to the working directory.
63-
guard let cwd = currentWorkingDirectory else {
64-
return nil
57+
#if os(Windows)
58+
let pathSeparator: Character = ";"
59+
#else
60+
let pathSeparator: Character = ":"
61+
#endif
62+
return (pathString ?? "").split(separator: pathSeparator).map(String.init).compactMap({ pathString in
63+
if let cwd = currentWorkingDirectory {
64+
return AbsolutePath(pathString, relativeTo: cwd)
6565
}
66-
return AbsolutePath(pathString, relativeTo: cwd)
66+
return try? AbsolutePath(validating: pathString)
6767
})
6868
}
6969

0 commit comments

Comments
 (0)