Skip to content

Commit 238a305

Browse files
authored
Merge pull request #186 from neonichu/improve-removeFileTree
Improve `removeFileTree`
2 parents 32a081a + 6fa6017 commit 238a305

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

Sources/TSCBasic/FileSystem.swift

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,8 +440,13 @@ private class LocalFileSystem: FileSystem {
440440
}
441441

442442
func removeFileTree(_ path: AbsolutePath) throws {
443-
if self.exists(path, followSymlink: false) {
443+
do {
444444
try FileManager.default.removeItem(atPath: path.pathString)
445+
} catch let error as NSError {
446+
// If we failed because the directory doesn't actually exist anymore, ignore the error.
447+
if !(error.domain == NSCocoaErrorDomain && error.code == NSFileNoSuchFileError) {
448+
throw error
449+
}
445450
}
446451
}
447452

Tests/TSCBasicTests/FileSystemTests.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,4 +955,8 @@ private func removeFileTreeTester(fs: FileSystem, basePath path: AbsolutePath, f
955955
XCTAssert(fs.exists(filePath), file: file, line: line)
956956
try fs.removeFileTree(filePath)
957957
XCTAssertFalse(fs.exists(filePath), file: file, line: line)
958+
959+
// Test removing non-existent path.
960+
let nonExistingPath = folders.appending(component: "does-not-exist")
961+
XCTAssertNoThrow(try fs.removeFileTree(nonExistingPath))
958962
}

0 commit comments

Comments
 (0)