From 2bbeee21b548d1bff4b84410b8270b7c7afb6214 Mon Sep 17 00:00:00 2001 From: Boris Buegling Date: Tue, 13 Feb 2024 14:17:06 -0800 Subject: [PATCH] Add hidden `--ignore-lock` escape hatch This is obviously dangerous, but arguably has been the default behavior of SwiftPM for many years, so offering an escape hatch seems appropriate. --- Sources/CoreCommands/Options.swift | 3 +++ Sources/CoreCommands/SwiftTool.swift | 15 ++++++++++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/Sources/CoreCommands/Options.swift b/Sources/CoreCommands/Options.swift index 8b558320371..e04e4e7ecfe 100644 --- a/Sources/CoreCommands/Options.swift +++ b/Sources/CoreCommands/Options.swift @@ -121,6 +121,9 @@ public struct LocationOptions: ParsableArguments { completion: .directory ) public var pkgConfigDirectories: [AbsolutePath] = [] + + @Option(name: .customLong("ignore-lock"), help: .hidden) + public var ignoreLock: Bool = false } public struct CachingOptions: ParsableArguments { diff --git a/Sources/CoreCommands/SwiftTool.swift b/Sources/CoreCommands/SwiftTool.swift index eb5e57af1b1..c9e43e0f63e 100644 --- a/Sources/CoreCommands/SwiftTool.swift +++ b/Sources/CoreCommands/SwiftTool.swift @@ -917,11 +917,16 @@ public final class SwiftTool { try workspaceLock.lock(type: .exclusive, blocking: false) } catch let ProcessLockError.unableToAquireLock(errno) { if errno == EWOULDBLOCK { - self.outputStream.write("Another instance of SwiftPM is already running using '\(self.scratchDirectory)', waiting until that process has finished execution...".utf8) - self.outputStream.flush() - - // Only if we fail because there's an existing lock we need to acquire again as blocking. - try workspaceLock.lock(type: .exclusive, blocking: true) + if self.options.locations.ignoreLock { + self.outputStream.write("Another instance of SwiftPM is already running using '\(self.scratchDirectory)', but this will be ignored since `--ignore-lock` has been passed".utf8) + self.outputStream.flush() + } else { + self.outputStream.write("Another instance of SwiftPM is already running using '\(self.scratchDirectory)', waiting until that process has finished execution...".utf8) + self.outputStream.flush() + + // Only if we fail because there's an existing lock we need to acquire again as blocking. + try workspaceLock.lock(type: .exclusive, blocking: true) + } } }