Skip to content

Commit d27bd63

Browse files
committed
Improve stability on Windows
1 parent df853dc commit d27bd63

File tree

2 files changed

+23
-9
lines changed

2 files changed

+23
-9
lines changed

Sources/TSCBasic/Path.swift

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,28 @@ public struct AbsolutePath: Path {
276276
if let rootPath = localFileSystem.currentWorkingDirectory?.root {
277277
return AbsolutePath(rootPath)
278278
} else {
279-
return AbsolutePath(FilePath._root)
279+
#if !os(Windows)
280+
return AbsolutePath("/")
281+
#else
282+
if let drive = ProcessEnv.vars["SystemDrive"] ?? ProcessEnv.vars["HomeDrive"] {
283+
return AbsolutePath(drive + "\\")
284+
} else {
285+
fatalError("cannot determine the drive")
286+
}
287+
#endif
288+
}
289+
}
290+
291+
public static func withPOSIX(path: String) -> AbsolutePath {
292+
#if os(Windows)
293+
var filepath = FilePath(path)
294+
if !filepath.isAbsolute {
295+
filepath.root = root.filepath.root
280296
}
297+
return AbsolutePath(filepath)
298+
#else
299+
return AbsolutePath(path)
300+
#endif
281301
}
282302
}
283303

@@ -408,14 +428,6 @@ extension PathValidationError: CustomNSError {
408428
}
409429

410430
extension FilePath {
411-
static var _root: FilePath {
412-
#if os(Windows)
413-
return FilePath("\\")
414-
#else
415-
return FilePath("/")
416-
#endif
417-
}
418-
419431
init(validatingAbsolutePath path: String) throws {
420432
self.init(path)
421433
guard self.isAbsolute else {

Tests/TSCBasicTests/TemporaryFileTests.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ class TemporaryFileTests: XCTestCase {
137137
XCTAssertFalse(localFileSystem.isDirectory(pathTwo))
138138
}
139139

140+
#if !os(Windows)
140141
/// Check that the temporary file doesn't leak file descriptors.
141142
func testLeaks() throws {
142143
// We check this by testing that we get back the same FD after a
@@ -150,4 +151,5 @@ class TemporaryFileTests: XCTestCase {
150151
let endFD = try Int(withTemporaryFile { return $0.fileHandle.fileDescriptor })
151152
XCTAssertEqual(initialFD, endFD)
152153
}
154+
#endif
153155
}

0 commit comments

Comments
 (0)