Skip to content

Commit b86d22e

Browse files
authored
[NFC] Vendor AsyncProcess, make more tests async (#7679)
We'd like to implement more changes to `TSCBasic.Process` without fearing of breaking clients, in addition to the fact that TSC is deprecated. One thing in particular we need is backpressure logic (coming in a future PR) when reading from process instances in plugins communication with the host SwiftPM process. Currently without this logic, a naive rewrite of plugins communication with structured concurrency is quite bug-prone, as it loses messages ordering and can quickly fill up all memory with resource-intensive plugins. Thus `TSCBasic.Process` is slightly cleaned up and is vendored as `AsyncProcess` in this change. We also give it more coverage in tests by utilizing `async` overloads, which are the overloads we should be using more in the structured concurrency conversion of the SwiftPM codebase.
1 parent 74aea87 commit b86d22e

File tree

67 files changed

+2781
-1631
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+2781
-1631
lines changed

Package.swift

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ let package = Package(
177177
"SPMBuildCore",
178178
],
179179
exclude: ["CMakeLists.txt"],
180-
swiftSettings: [.enableExperimentalFeature("AccessLevelOnImport")]
180+
swiftSettings: [
181+
.enableExperimentalFeature("AccessLevelOnImport"),
182+
]
181183
),
182184

183185
// MARK: SwiftPM specific support libraries
@@ -196,6 +198,7 @@ let package = Package(
196198
exclude: ["CMakeLists.txt", "Vendor/README.md"],
197199
swiftSettings: [
198200
.enableExperimentalFeature("StrictConcurrency"),
201+
.enableExperimentalFeature("AccessLevelOnImport"),
199202
]
200203
),
201204

Sources/Basics/Archiver/TarArchiver.swift

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import class Dispatch.DispatchQueue
1414
import struct Dispatch.DispatchTime
1515
import struct TSCBasic.FileSystemError
16-
import class TSCBasic.Process
1716

1817
/// An `Archiver` that handles Tar archives using the command-line `tar` tool.
1918
public struct TarArchiver: Archiver {
@@ -58,7 +57,7 @@ public struct TarArchiver: Archiver {
5857
throw FileSystemError(.notDirectory, destinationPath.underlying)
5958
}
6059

61-
let process = TSCBasic.Process(
60+
let process = AsyncProcess(
6261
arguments: [self.tarCommand, "zxf", archivePath.pathString, "-C", destinationPath.pathString]
6362
)
6463

@@ -91,10 +90,10 @@ public struct TarArchiver: Archiver {
9190
throw FileSystemError(.notDirectory, directory.underlying)
9291
}
9392

94-
let process = TSCBasic.Process(
93+
let process = AsyncProcess(
9594
arguments: [self.tarCommand, "acf", destinationPath.pathString, directory.basename],
9695
environment: .current,
97-
workingDirectory: directory.parentDirectory.underlying
96+
workingDirectory: directory.parentDirectory
9897
)
9998

10099
guard let registrationKey = self.cancellator.register(process) else {
@@ -122,7 +121,7 @@ public struct TarArchiver: Archiver {
122121
throw FileSystemError(.noEntry, path.underlying)
123122
}
124123

125-
let process = TSCBasic.Process(arguments: [self.tarCommand, "tf", path.pathString])
124+
let process = AsyncProcess(arguments: [self.tarCommand, "tf", path.pathString])
126125
guard let registrationKey = self.cancellator.register(process) else {
127126
throw CancellationError.failedToRegisterProcess(process)
128127
}

Sources/Basics/Archiver/ZipArchiver.swift

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212

1313
import Dispatch
1414
import struct TSCBasic.FileSystemError
15-
import class TSCBasic.Process
1615

1716
/// An `Archiver` that handles ZIP archives using the command-line `zip` and `unzip` tools.
1817
public struct ZipArchiver: Archiver, Cancellable {
@@ -49,11 +48,9 @@ public struct ZipArchiver: Archiver, Cancellable {
4948
}
5049

5150
#if os(Windows)
52-
let process = TSCBasic
53-
.Process(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
51+
let process = AsyncProcess(arguments: ["tar.exe", "xf", archivePath.pathString, "-C", destinationPath.pathString])
5452
#else
55-
let process = TSCBasic
56-
.Process(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
53+
let process = AsyncProcess(arguments: ["unzip", archivePath.pathString, "-d", destinationPath.pathString])
5754
#endif
5855
guard let registrationKey = self.cancellator.register(process) else {
5956
throw CancellationError.failedToRegisterProcess(process)
@@ -85,10 +82,10 @@ public struct ZipArchiver: Archiver, Cancellable {
8582
}
8683

8784
#if os(Windows)
88-
let process = TSCBasic.Process(
85+
let process = AsyncProcess(
8986
// FIXME: are these the right arguments?
9087
arguments: ["tar.exe", "-a", "-c", "-f", destinationPath.pathString, directory.basename],
91-
workingDirectory: directory.parentDirectory.underlying
88+
workingDirectory: directory.parentDirectory
9289
)
9390
#else
9491
// This is to work around `swift package-registry publish` tool failing on
@@ -97,7 +94,7 @@ public struct ZipArchiver: Archiver, Cancellable {
9794
// Instead of passing `workingDirectory` param to TSC.Process, which will trigger
9895
// SPM_posix_spawn_file_actions_addchdir_np_supported check, we shell out and
9996
// do `cd` explicitly before `zip`.
100-
let process = TSCBasic.Process(
97+
let process = AsyncProcess(
10198
arguments: [
10299
"/bin/sh",
103100
"-c",
@@ -132,9 +129,9 @@ public struct ZipArchiver: Archiver, Cancellable {
132129
}
133130

134131
#if os(Windows)
135-
let process = TSCBasic.Process(arguments: ["tar.exe", "tf", path.pathString])
132+
let process = AsyncProcess(arguments: ["tar.exe", "tf", path.pathString])
136133
#else
137-
let process = TSCBasic.Process(arguments: ["unzip", "-t", path.pathString])
134+
let process = AsyncProcess(arguments: ["unzip", "-t", path.pathString])
138135
#endif
139136
guard let registrationKey = self.cancellator.register(process) else {
140137
throw CancellationError.failedToRegisterProcess(process)

0 commit comments

Comments
 (0)