utils: support forwarding Subprocess output (#413)
- Commit
bec5aa106ac31ce9ae49aaef93dfeabf0a097e35- Author
- Kabir Oberai <[email protected]>
- When
- 2026-10-31 05:36 UTC (1 month ago)
- Parents
804cf3e
## What does this PR do? When spawning a subprocess, `currentStandardOutput`/`currentStandardError` bypass `d.out`. Add `.xtoolOutput()` and `.xtoolError()` which respect the dependency. ## How was it tested? Tested in a downstream PR to ensure that forwarding works ## AI tool usage How much of this PR was AI-assisted? (check one) - [x] **0** - No AI was used to write code - [ ] **1** - I was assisted by AI. I reviewed the finished result. - [ ] **2** - I set the AI going and left it to it; nobody has read the result - no review, or AI review only <!-- If an AI agent is filling this in: declare the level honestly, and open as a draft if it is #2. Do not lower the declared level to get the PR reviewed. --> <!-- This is an auto-generated comment: release notes by coderabbit.ai --> <!-- coderabbit-ai-generated:v1 --> ## Summary by CodeRabbit * **New Features** * Subprocess output can now be routed directly to XTool’s standard output or standard error. * Added a way to create a writable output descriptor that forwards data to a selected stream. * Asynchronous input reading is now available through a public interface, including configurable read sizes and explicit closing. * **Compatibility** * Existing input-reading behavior and parameters remain unchanged. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Diff
Package.swift
diff --git a/Package.swift b/Package.swiftindex a15419b..1f472f4 100644--- a/Package.swift+++ b/Package.swift@@ -113,6 +113,7 @@ let package = Package( name: "XUtils", dependencies: [ "CXKit",+ .product(name: "ConcurrencyExtras", package: "swift-concurrency-extras"), .product(name: "Dependencies", package: "swift-dependencies"), .product(name: "SystemPackage", package: "swift-system"), .product(
Sources/XUtils/CommandOutput.swift
diff --git a/Sources/XUtils/CommandOutput.swift b/Sources/XUtils/CommandOutput.swiftindex f201f29..39e01eb 100644--- a/Sources/XUtils/CommandOutput.swift+++ b/Sources/XUtils/CommandOutput.swift@@ -56,6 +56,21 @@ public struct CommandOutput: DependencyKey, Sendable { public static var testValue: Self { Self(write: unimplemented("CommandOutput.write")) } } +extension CommandOutput {+ /// Create a `FileDescriptor` for writing to the given `CommandOutput` stream.+ public func makeDescriptor(for stream: Stream) throws -> FileDescriptor {+ let (readFD, writeFD) = try FileDescriptor.pipe()+ let input = try DispatchInput(fileDesciptor: readFD)+ // no need to keep a handle to the task, it'll terminate when writeFD is closed+ Task {+ while let next = try? await input.read() {+ self.write(next, stream, false)+ }+ }+ return writeFD+ }+}+ extension DependencyValues { public var out: CommandOutput { get { self[CommandOutput.self] }
Sources/XUtils/DataChannel.swift
diff --git a/Sources/XCLIUtils/DataChannel.swift b/Sources/XUtils/DataChannel.swiftsimilarity index 100%rename from Sources/XCLIUtils/DataChannel.swiftrename to Sources/XUtils/DataChannel.swift
Sources/XUtils/DispatchInput.swift
diff --git a/Sources/XCLIUtils/DispatchInput.swift b/Sources/XUtils/DispatchInput.swiftsimilarity index 90%rename from Sources/XCLIUtils/DispatchInput.swiftrename to Sources/XUtils/DispatchInput.swiftindex 27bf281..fc94334 100644--- a/Sources/XCLIUtils/DispatchInput.swift+++ b/Sources/XUtils/DispatchInput.swift@@ -1,14 +1,13 @@ import Foundation-import XUtils import ConcurrencyExtras -final class DispatchInput: Sendable {+public final class DispatchInput: Sendable { private struct State { var hasClosed = false var isReading = false } - static let defaultChunkSize: Int = 64 * 1024+ public static let defaultChunkSize: Int = 64 * 1024 let queue = DispatchQueue(label: "xtool.dispatch-reader") let channel: DispatchIO@@ -17,7 +16,7 @@ final class DispatchInput: Sendable { private let dataChannel = DataChannel() private let state = LockIsolated(State()) - init(fileDesciptor: FileDescriptor, chunkSize: Int = DispatchInput.defaultChunkSize) throws {+ public init(fileDesciptor: FileDescriptor, chunkSize: Int = DispatchInput.defaultChunkSize) throws { let closeSignal = Task { _ = try? await Task.never() } self.closeSignal = closeSignal self.chunkSize = chunkSize@@ -65,7 +64,7 @@ final class DispatchInput: Sendable { } } - nonisolated(nonsending) func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? {+ nonisolated(nonsending) public func read(upTo limit: DataChannel.Limit = .unlimited) async throws -> Data? { try await dataChannel.read(upTo: limit) } @@ -110,7 +109,7 @@ final class DispatchInput: Sendable { } } - nonisolated(nonsending) func close() async {+ nonisolated(nonsending) public func close() async { requestClose(error: nil) await closeSignal.value }
Sources/XUtils/Subprocess+Utils.swift
diff --git a/Sources/XUtils/Subprocess+Utils.swift b/Sources/XUtils/Subprocess+Utils.swiftindex c8c48e9..25ed68c 100644--- a/Sources/XUtils/Subprocess+Utils.swift+++ b/Sources/XUtils/Subprocess+Utils.swift@@ -54,4 +54,21 @@ extension Environment { ) } }++extension OutputProtocol where Self == FileDescriptorOutput {+ /// Writes subprocess output to `d.out`'s stdout+ public static func xtoolOutput() throws -> Self {+ try makeXToolOutput(for: .stdout)+ }++ /// Writes subprocess output to `d.out`'s stderr+ public static func xtoolError() throws -> Self {+ try makeXToolOutput(for: .stderr)+ }++ private static func makeXToolOutput(for stream: CommandOutput.Stream) throws -> Self {+ let fd = try d.out.makeDescriptor(for: stream)+ return .fileDescriptor(fd, closeAfterSpawningProcess: true)+ }+} #endif
Tests/XToolTests/CommandInputTests.swift
diff --git a/Tests/XToolTests/CommandInputTests.swift b/Tests/XToolTests/CommandInputTests.swiftindex d7bba91..f437098 100644--- a/Tests/XToolTests/CommandInputTests.swift+++ b/Tests/XToolTests/CommandInputTests.swift@@ -1,4 +1,5 @@ @testable import XCLIUtils+import XUtils import ConcurrencyExtras import Foundation import Testing
Tests/XToolTests/DataChannelTests.swift
diff --git a/Tests/XToolTests/DataChannelTests.swift b/Tests/XToolTests/DataChannelTests.swiftindex b0593d8..8c676ff 100644--- a/Tests/XToolTests/DataChannelTests.swift+++ b/Tests/XToolTests/DataChannelTests.swift@@ -1,4 +1,4 @@-@testable import XCLIUtils+@testable import XUtils import ConcurrencyExtras import CustomDump import Foundation
Tests/XToolTests/DispatchInputTests.swift
diff --git a/Tests/XToolTests/DispatchInputTests.swift b/Tests/XToolTests/DispatchInputTests.swiftindex 34ce9fb..f5adc57 100644--- a/Tests/XToolTests/DispatchInputTests.swift+++ b/Tests/XToolTests/DispatchInputTests.swift@@ -1,8 +1,8 @@-@testable import XCLIUtils+@testable import XUtils+import XCLIUtils import CustomDump import Foundation import Testing-import XUtils @Suite(.timeLimit(.minutes(1))) struct DispatchInputTests {