Skip to content

Display PID of other running SwiftPM processes #8575

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 6, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions Sources/CoreCommands/SwiftCommandState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1060,29 +1060,38 @@ public final class SwiftCommandState {
self.workspaceLockState = .locked

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

// Try a non-blocking lock first so that we can inform the user about an already running SwiftPM.
do {
try workspaceLock.lock(type: .exclusive, blocking: false)
let pid = ProcessInfo.processInfo.processIdentifier
try? String(pid).write(toFile: lockFile, atomically: true, encoding: .utf8)
} catch ProcessLockError.unableToAquireLock(let errno) {
if errno == EWOULDBLOCK {
let lockingPID = try? String(contentsOfFile: lockFile, encoding: .utf8)
let pidInfo = lockingPID.map { "(PID: \($0)) " } ?? ""

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"
"Another instance of SwiftPM \(pidInfo)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..."
"Another instance of SwiftPM \(pidInfo)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)

let pid = ProcessInfo.processInfo.processIdentifier
try? String(pid).write(toFile: lockFile, atomically: true, encoding: .utf8)
}
}
}
Expand Down