Skip to content

Commit 7571d6a

Browse files
Display PID of other running SwiftPM processes (#8575)
1 parent f770c45 commit 7571d6a

File tree

1 file changed

+11
-2
lines changed

1 file changed

+11
-2
lines changed

Sources/CoreCommands/SwiftCommandState.swift

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,29 +1055,38 @@ public final class SwiftCommandState {
10551055
self.workspaceLockState = .locked
10561056

10571057
let workspaceLock = try FileLock.prepareLock(fileToLock: self.scratchDirectory)
1058+
let lockFile = self.scratchDirectory.appending(".lock").pathString
10581059

10591060
// Try a non-blocking lock first so that we can inform the user about an already running SwiftPM.
10601061
do {
10611062
try workspaceLock.lock(type: .exclusive, blocking: false)
1063+
let pid = ProcessInfo.processInfo.processIdentifier
1064+
try? String(pid).write(toFile: lockFile, atomically: true, encoding: .utf8)
10621065
} catch ProcessLockError.unableToAquireLock(let errno) {
10631066
if errno == EWOULDBLOCK {
1067+
let lockingPID = try? String(contentsOfFile: lockFile, encoding: .utf8)
1068+
let pidInfo = lockingPID.map { "(PID: \($0)) " } ?? ""
1069+
10641070
if self.options.locations.ignoreLock {
10651071
self.outputStream
10661072
.write(
1067-
"Another instance of SwiftPM is already running using '\(self.scratchDirectory)', but this will be ignored since `--ignore-lock` has been passed"
1073+
"Another instance of SwiftPM \(pidInfo)is already running using '\(self.scratchDirectory)', but this will be ignored since `--ignore-lock` has been passed"
10681074
.utf8
10691075
)
10701076
self.outputStream.flush()
10711077
} else {
10721078
self.outputStream
10731079
.write(
1074-
"Another instance of SwiftPM is already running using '\(self.scratchDirectory)', waiting until that process has finished execution..."
1080+
"Another instance of SwiftPM \(pidInfo)is already running using '\(self.scratchDirectory)', waiting until that process has finished execution..."
10751081
.utf8
10761082
)
10771083
self.outputStream.flush()
10781084

10791085
// Only if we fail because there's an existing lock we need to acquire again as blocking.
10801086
try workspaceLock.lock(type: .exclusive, blocking: true)
1087+
1088+
let pid = ProcessInfo.processInfo.processIdentifier
1089+
try? String(pid).write(toFile: lockFile, atomically: true, encoding: .utf8)
10811090
}
10821091
}
10831092
}

0 commit comments

Comments
 (0)