Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
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
28 changes: 25 additions & 3 deletions Sources/ScipioKit/Executor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,19 @@ private actor ProcessOutputBuffer {

@_spi(Internals)
public protocol Executor {
/// Executes the command with the given arguments and environment variables.
///
/// - Parameters:
/// - arguments: Command-line arguments for the process.
/// - environment: Complete set of environment variables for the process.
/// If `nil`, the current environment is used.
/// If non-nil, it **replaces** the entire environment.
/// Use `ProcessInfo.processInfo.environment` to preserve existing values if needed.
///
/// - Note:
/// This does not merge with the existing environment.
@discardableResult
func execute(_ arguments: [String]) async throws -> ExecutorResult
func execute(_ arguments: [String], environment: [String: String]?) async throws -> ExecutorResult
}

@_spi(Internals)
Expand Down Expand Up @@ -43,6 +54,11 @@ public protocol ExecutorResult: Sendable {
}

public extension Executor {
@discardableResult
func execute(_ arguments: [String]) async throws -> ExecutorResult {
try await execute(arguments, environment: nil)
}

@discardableResult
func execute(_ arguments: String...) async throws -> ExecutorResult {
try await execute(arguments)
Expand Down Expand Up @@ -98,7 +114,7 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable {

public var streamOutput: (@Sendable ([UInt8]) async -> Void)?

public func execute(_ arguments: [String]) async throws -> ExecutorResult {
public func execute(_ arguments: [String], environment: [String: String]?) async throws -> ExecutorResult {
guard let executable = arguments.first, !executable.isEmpty else {
throw ProcessExecutorError.executableNotFound
}
Expand All @@ -108,7 +124,7 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable {
throw ProcessExecutorError.executableNotFound
}

let process = Foundation.Process()
let process = Process()
let stdoutPipe = Pipe()
let stderrPipe = Pipe()

Expand All @@ -117,6 +133,12 @@ public struct ProcessExecutor<Decoder: ErrorDecoder>: Executor, Sendable {
process.standardOutput = stdoutPipe
process.standardError = stderrPipe

// Completely replace the process environment if provided.
// If nil, the process inherits the current environment.
if let environment {
process.environment = environment
}
Comment on lines +138 to +140
Copy link
Owner

Choose a reason for hiding this comment

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

[nits] It's enough to be

Suggested change
if let environment {
process.environment = environment
}
process.environment = environment

Copy link
Collaborator

@ikesyo ikesyo Aug 1, 2025

Choose a reason for hiding this comment

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

I don't think so. Setting the property explicitly to nil will not mean ineheriting the current process environment, rather it may clear out the environment. We should be careful about this behavior.

If this method isn’t used, the environment is inherited from the process that created the receiver.

Copy link
Collaborator

@ikesyo ikesyo Aug 1, 2025

Choose a reason for hiding this comment

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

I checked the implementation in swift-corelibs-foundation. If the property is nil ProcessInfo.processInfo.environment is used, so setting nil explicitly will be safe.

https://github.com/swiftlang/swift-corelibs-foundation/blob/d576c7596b718e1fc41e7902e4623c44eac31945/Sources/Foundation/Process.swift#L588-L593

However, I still prefer not to set the property if it's not needed.


let outputHandle = stdoutPipe.fileHandleForReading
let errorHandle = stderrPipe.fileHandleForReading

Expand Down
2 changes: 1 addition & 1 deletion Tests/ScipioKitTests/TestingExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ final class StubbableExecutor: Executor {
calledArguments.count
}

func execute(_ arguments: [String]) async throws -> ExecutorResult {
func execute(_ arguments: [String], environment: [String: String]?) async throws -> ExecutorResult {
calledArguments.append(arguments)
return try executeHook(arguments)
}
Expand Down