@@ -101,6 +101,16 @@ public protocol SwiftCommand: ParsableCommand, _SwiftCommand {
101
101
extension SwiftCommand {
102
102
public static var _errorLabel : String { " error " }
103
103
104
+ private func acquireScratchDirectoryLockIfNeeded< T> ( swiftTool: SwiftTool , blocking: Bool = true , _ body: ( ) throws -> T ) throws {
105
+ let interactive = isatty ( STDOUT_FILENO) == 1
106
+ _ = try swiftTool. fileSystem. withLock (
107
+ on: swiftTool. scratchDirectory,
108
+ type: . exclusive,
109
+ blocking: interactive ? blocking : false ,
110
+ body
111
+ )
112
+ }
113
+
104
114
public func run( ) throws {
105
115
let swiftTool = try SwiftTool (
106
116
options: globalOptions,
@@ -112,7 +122,7 @@ extension SwiftCommand {
112
122
113
123
// Try a non-blocking lock first so that we can inform the user about an already running SwiftPM.
114
124
do {
115
- try swiftTool . fileSystem . withLock ( on : swiftTool. scratchDirectory , type : . exclusive , blocking: false ) { }
125
+ try acquireScratchDirectoryLockIfNeeded ( swiftTool: swiftTool , blocking: false ) { }
116
126
} catch let ProcessLockError . unableToAquireLock( errno) {
117
127
if errno == EWOULDBLOCK {
118
128
swiftTool. outputStream. write ( " Another instance of SwiftPM is already running using ' \( swiftTool. scratchDirectory) ', waiting until that process has finished execution... " . utf8)
@@ -121,7 +131,7 @@ extension SwiftCommand {
121
131
}
122
132
123
133
var toolError : Error ? = . none
124
- try swiftTool . fileSystem . withLock ( on : swiftTool. scratchDirectory , type : . exclusive ) {
134
+ try acquireScratchDirectoryLockIfNeeded ( swiftTool: swiftTool ) {
125
135
do {
126
136
try self . run ( swiftTool)
127
137
if swiftTool. observabilityScope. errorsReported || swiftTool. executionStatus == . failure {
0 commit comments