From d6806de017deefe1974232ea454223938424b638 Mon Sep 17 00:00:00 2001 From: Alex Hoppen Date: Sun, 27 Oct 2024 21:35:19 -0700 Subject: [PATCH] Don't fail `createDirectory` if directory is created concurrently by another process --- Sources/TSCBasic/FileSystem.swift | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/Sources/TSCBasic/FileSystem.swift b/Sources/TSCBasic/FileSystem.swift index 941b888b..43988cb8 100644 --- a/Sources/TSCBasic/FileSystem.swift +++ b/Sources/TSCBasic/FileSystem.swift @@ -494,7 +494,17 @@ private struct LocalFileSystem: FileSystem { // Don't fail if path is already a directory. if isDirectory(path) { return } - try FileManager.default.createDirectory(atPath: path.pathString, withIntermediateDirectories: recursive, attributes: [:]) + do { + try FileManager.default.createDirectory(atPath: path.pathString, withIntermediateDirectories: recursive, attributes: [:]) + } catch { + if isDirectory(path) { + // `createDirectory` failed but we have a directory now. This might happen if the directory is created + // by another process between the check above and the call to `createDirectory`. + // Since we have the expected end result, this is fine. + return + } + throw error + } } func createSymbolicLink(_ path: AbsolutePath, pointingAt destination: AbsolutePath, relative: Bool) throws {