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

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

louisunlimited
Copy link

@louisunlimited louisunlimited commented Apr 30, 2025

Displays the PID of the other SwiftPM instance currently holding the lock

Motivation:

When we try to run different SwiftPM processes we already see notes like

Another instance of swiftPM is already running using '/Users/louissssss/swift-package-manager/.build', waiting until that process has finished execution...

I'll be nice to also have info about PID so I can choose to terminate it or wait for it to finish, so it might display smt like:

Another instance of SwiftPM (PID: 3404) is already running using '/Users/louissssss/swift-package-manager/.build', waiting until that process has finished execution...

Modifications:

My approach was trying to write process id from the process that acquires the lock to the lock file and then let other processes read from it and get the pid.

Result:

If another SwiftPM process is currently running, it now displays:

Another instance of SwiftPM (PID: 3404) is already running using '/Users/louissssss/swift-package-manager/.build', waiting until that process has finished execution...

} catch ProcessLockError.unableToAquireLock(let errno) {
if errno == EWOULDBLOCK {
let lockingPID = try? String(contentsOfFile: self.scratchDirectory.appending(".lock").pathString, encoding: .utf8)
let pidInfo = lockingPID.map { "(PID: \($0))" } ?? ""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd add a space after the closing parenthesis, like so:

let pidInfo = lockingPID.map { "(PID: \($0)) " } ?? ""

...and then correspondingly remove the space after the pifInfo interpolation a few lines down, like so:

"Another instance of SwiftPM \(pidInfo)is already running using '\(self.scratchDirectory)', but this will be ignored since `--ignore-lock` has been passed"

...so that we don't end up with a double space between "SwiftPM" and "is" if we couldn't read the PID, since that would look out of place.

@jakepetroules
Copy link
Contributor

Thanks for your PR, this is a nice little enhancement! Just had some minor formatting related-feedback.

@@ -1064,25 +1064,33 @@ public final class SwiftCommandState {
// 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: self.scratchDirectory.appending(".lock").pathString, atomically: true, encoding: .utf8)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: refactor out self.scratchDirectory.appending(".lock").pathString in to a variable

@MaxDesiatov MaxDesiatov changed the title feat: display PID of the other swiftPM process Display PID of the other SwiftPM process Apr 30, 2025
@MaxDesiatov MaxDesiatov changed the title Display PID of the other SwiftPM process Display PID of other running SwiftPM process Apr 30, 2025
@MaxDesiatov MaxDesiatov changed the title Display PID of other running SwiftPM process Display PID of other running SwiftPM processes Apr 30, 2025
@louisunlimited
Copy link
Author

Just did all the changes, one other thought: would it be better if we mark String(pid).write(toFile: lockFile, atomically: true, encoding: .utf8) with try? instead of try so this is not a critical throw and will not stop the program? I'd say as long as we can acquire the lock we should not interrupt normal execution.

@plemarquand
Copy link
Contributor

@louisunlimited I think that's a good idea. It's a non-essential call and worst case the message just falls back to what we have today.

@louisunlimited
Copy link
Author

@louisunlimited I think that's a good idea. It's a non-essential call and worst case the message just falls back to what we have today.

Cool, just pushed the changes, lmk if you need me for anything else

@plemarquand
Copy link
Contributor

@swift-ci test

@plemarquand plemarquand enabled auto-merge (squash) May 1, 2025 11:27
@johnbute
Copy link
Contributor

johnbute commented May 5, 2025

Great pr Louis! I was working on the same thing too:

#8618

If you want to take any changes from it to enhance yours please do :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Display the process ID in the when running a Swift PM command that has a hold of the .build directory
4 participants