-
Notifications
You must be signed in to change notification settings - Fork 1.4k
ProcessID enhacement #8618
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
johnbute
wants to merge
9
commits into
swiftlang:main
Choose a base branch
from
johnbute:feature/pid-enhancement
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
ProcessID enhacement #8618
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
88f1b09
ProcessID enhacement
2662019
added simplification
6f02455
added tests, naming consistency
c16f2b7
formatting
511ece9
fixed simple mistake
cfdc8ae
fixed bug where .build directory was not built yet when testing spm
453c15b
added platform compatibility
74793e8
changed header of file
31cb5e3
added more tests and fixed diagnostics
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
//===----------------------------------------------------------------------===// | ||
// | ||
// This source file is part of the Swift.org open source project | ||
// | ||
// Copyright (c) 2025 Apple Inc. and the Swift project authors | ||
// Licensed under Apache License v2.0 with Runtime Library Exception | ||
// | ||
// See https://swift.org/LICENSE.txt for license information | ||
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
import Foundation | ||
|
||
public protocol PIDFileHandler { | ||
var scratchDirectory: AbsolutePath { get set } | ||
|
||
init(scratchDirectory: AbsolutePath) | ||
|
||
func readPID() throws -> Int32 | ||
func deletePIDFile() throws | ||
func writePID(pid: Int32) throws | ||
func getCurrentPID() -> Int32 | ||
} | ||
|
||
public struct PIDFile: PIDFileHandler { | ||
public var scratchDirectory: AbsolutePath | ||
|
||
public init(scratchDirectory: AbsolutePath) { | ||
self.scratchDirectory = scratchDirectory | ||
} | ||
|
||
/// Return the path of the PackageManager.lock.pid file where the PID is located | ||
private var lockFilePath: AbsolutePath { | ||
self.scratchDirectory.appending(component: "PackageManager.lock.pid") | ||
} | ||
|
||
/// Read the pid file | ||
public func readPID() throws -> Int32 { | ||
// Check if the file exists | ||
let filePath = self.lockFilePath.pathString | ||
guard FileManager.default.fileExists(atPath: filePath) else { | ||
throw PIDError.noSuchPiDFile | ||
} | ||
|
||
let pidString = try String(contentsOf: lockFilePath.asURL, encoding: .utf8) | ||
.trimmingCharacters(in: .whitespacesAndNewlines) | ||
|
||
// Try to convert to Int32, or throw an error | ||
guard let pid = Int32(pidString) else { | ||
throw PIDError.invalidPIDFormat | ||
} | ||
|
||
return pid | ||
} | ||
|
||
/// Get the current PID of the proces | ||
public func getCurrentPID() -> Int32 { | ||
ProcessInfo.processInfo.processIdentifier | ||
} | ||
|
||
/// Write .pid file containing PID of process currently using .build directory | ||
public func writePID(pid: Int32) throws { | ||
let parent = self.lockFilePath.parentDirectory | ||
try FileManager.default.createDirectory( | ||
at: parent.asURL, | ||
withIntermediateDirectories: true, | ||
attributes: nil | ||
) | ||
|
||
try "\(pid)".write(to: self.lockFilePath.asURL, atomically: true, encoding: .utf8) | ||
} | ||
|
||
/// Delete PID file at URL | ||
public func deletePIDFile() throws { | ||
do { | ||
try FileManager.default.removeItem(at: self.lockFilePath.asURL) | ||
} catch { | ||
throw PIDError.noSuchPiDFile | ||
} | ||
} | ||
|
||
public enum PIDError: Error { | ||
case invalidPIDFormat | ||
case noSuchPiDFile | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,168 @@ | ||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||
// PIDTests.swift | ||||||||||||||||||||||||||||||||||||
// SwiftPM | ||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||
// Created by John Bute on 2025-05-14. | ||||||||||||||||||||||||||||||||||||
// | ||||||||||||||||||||||||||||||||||||
Comment on lines
+1
to
+6
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. todo: marking this for visibility! :) for new files, this is the suggested header -->
Suggested change
|
||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
import Basics | ||||||||||||||||||||||||||||||||||||
import Foundation | ||||||||||||||||||||||||||||||||||||
import Testing | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
struct PIDTests { | ||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testWritePIDMultipleCalls() throws { | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidHandler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pid1: Int32 = 1234 | ||||||||||||||||||||||||||||||||||||
let pid2: Int32 = 5678 | ||||||||||||||||||||||||||||||||||||
let pid3: Int32 = 9012 | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
try pidHandler.writePID(pid: pid1) | ||||||||||||||||||||||||||||||||||||
try pidHandler.writePID(pid: pid2) | ||||||||||||||||||||||||||||||||||||
try pidHandler.writePID(pid: pid3) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(throws: Never.self) { | ||||||||||||||||||||||||||||||||||||
try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pid = try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(pid == pid3) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testDeleteExistingPIDFile() async throws { | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidHandler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
let currentPID = pidHandler.getCurrentPID() | ||||||||||||||||||||||||||||||||||||
try pidHandler.writePID(pid: currentPID) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(throws: Never.self) { | ||||||||||||||||||||||||||||||||||||
try pidHandler.deletePIDFile() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testDeleteNonExistingPIDFile() async throws { | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let filePath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let handler = PIDFile(scratchDirectory: filePath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.noSuchPiDFile) { | ||||||||||||||||||||||||||||||||||||
try handler.deletePIDFile() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testFileDoesNotExist() throws { | ||||||||||||||||||||||||||||||||||||
// Create a temporary directory | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let filePath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
let handler = PIDFile(scratchDirectory: filePath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.noSuchPiDFile) { | ||||||||||||||||||||||||||||||||||||
try handler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testInvalidPIDFormat() async throws { | ||||||||||||||||||||||||||||||||||||
// Create a temporary directory | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidFilePath = scratchPath.appending(component: "PackageManager.lock.pid") | ||||||||||||||||||||||||||||||||||||
let handler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Write invalid content (non-numeric PID) | ||||||||||||||||||||||||||||||||||||
let invalidPIDContent = "invalidPID" | ||||||||||||||||||||||||||||||||||||
try localFileSystem.writeFileContents(pidFilePath, bytes: .init(encodingAsUTF8: invalidPIDContent)) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.invalidPIDFormat) { | ||||||||||||||||||||||||||||||||||||
try handler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Test case to check if the function works when a valid PID is in the file | ||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testValidPIDFormat() throws { | ||||||||||||||||||||||||||||||||||||
// Create a temporary directory | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidFilePath = scratchPath.appending(component: "PackageManager.lock.pid") | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let handler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Write a valid PID content | ||||||||||||||||||||||||||||||||||||
let validPIDContent = "12345" | ||||||||||||||||||||||||||||||||||||
try localFileSystem.writeFileContents(pidFilePath, bytes: .init(encodingAsUTF8: validPIDContent)) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pid = try handler.readPID() | ||||||||||||||||||||||||||||||||||||
#expect(pid == 12345) | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testPIDFileHandlerLifecycle() throws { | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidHandler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Ensure no PID file exists initially | ||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.noSuchPiDFile) { | ||||||||||||||||||||||||||||||||||||
try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Write current PID | ||||||||||||||||||||||||||||||||||||
let currentPID = pidHandler.getCurrentPID() | ||||||||||||||||||||||||||||||||||||
try pidHandler.writePID(pid: currentPID) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Read PID back | ||||||||||||||||||||||||||||||||||||
let readPID = try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
#expect(readPID == currentPID, "PID read should match written PID") | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Delete the file | ||||||||||||||||||||||||||||||||||||
try pidHandler.deletePIDFile() | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
// Ensure file is gone | ||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.noSuchPiDFile) { | ||||||||||||||||||||||||||||||||||||
try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
@Test | ||||||||||||||||||||||||||||||||||||
func testMalformedPIDFile() throws { | ||||||||||||||||||||||||||||||||||||
try withTemporaryDirectory { tmpDir in | ||||||||||||||||||||||||||||||||||||
let scratchPath = tmpDir.appending(component: "scratch") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.createDirectory(scratchPath) | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidPath = scratchPath.appending(component: "PackageManager.lock.pid") | ||||||||||||||||||||||||||||||||||||
try localFileSystem.writeFileContents(pidPath, bytes: "notanumber") | ||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||
let pidHandler = PIDFile(scratchDirectory: scratchPath) | ||||||||||||||||||||||||||||||||||||
#expect(throws: PIDFile.PIDError.invalidPIDFormat) { | ||||||||||||||||||||||||||||||||||||
try pidHandler.readPID() | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.