Skip to content

Commit 8a31bf0

Browse files
authored
Merge pull request #143 from apple/revert-122-dev
Revert "Add support for Process stdin stream"
2 parents 8f9838e + 81eda02 commit 8a31bf0

File tree

3 files changed

+21
-87
lines changed

3 files changed

+21
-87
lines changed

Sources/TSCBasic/Process.swift

Lines changed: 21 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -331,11 +331,8 @@ public final class Process: ObjectIdentifierProtocol {
331331
}
332332
}
333333

334-
/// Launch the subprocess. Returns a WritableByteStream object that can be used to communicate to the process's
335-
/// stdin. If needed, the stream can be closed using the close() API. Otherwise, the stream will be closed
336-
/// automatically.
337-
@discardableResult
338-
public func launch() throws -> WritableByteStream {
334+
/// Launch the subprocess.
335+
public func launch() throws {
339336
precondition(arguments.count > 0 && !arguments[0].isEmpty, "Need at least one argument to launch the process.")
340337
precondition(!launched, "It is not allowed to launch the same process object again.")
341338

@@ -354,15 +351,12 @@ public final class Process: ObjectIdentifierProtocol {
354351
throw Process.Error.missingExecutableProgram(program: executable)
355352
}
356353

357-
#if os(Windows)
354+
#if os(Windows)
358355
_process = Foundation.Process()
359356
_process?.arguments = Array(arguments.dropFirst()) // Avoid including the executable URL twice.
360357
_process?.executableURL = executablePath.asURL
361358
_process?.environment = environment
362359

363-
let stdinPipe = Pipe()
364-
_process?.standardInput = stdinPipe
365-
366360
if outputRedirection.redirectsOutput {
367361
let stdoutPipe = Pipe()
368362
let stderrPipe = Pipe()
@@ -385,8 +379,6 @@ public final class Process: ObjectIdentifierProtocol {
385379
}
386380

387381
try _process?.run()
388-
389-
return stdinPipe.fileHandleForWriting
390382
#else
391383
// Initialize the spawn attributes.
392384
#if canImport(Darwin) || os(Android)
@@ -461,17 +453,14 @@ public final class Process: ObjectIdentifierProtocol {
461453
#endif
462454
}
463455

464-
var stdinPipe: [Int32] = [-1, -1]
465-
try open(pipe: &stdinPipe)
466-
467-
let stdinStream = try LocalFileOutputByteStream(filePointer: fdopen(stdinPipe[1], "wb"), closeOnDeinit: true)
468-
469-
// Dupe the read portion of the remote to 0.
470-
posix_spawn_file_actions_adddup2(&fileActions, stdinPipe[0], 0)
471-
472-
// Close the other side's pipe since it was dupped to 0.
473-
posix_spawn_file_actions_addclose(&fileActions, stdinPipe[0])
474-
posix_spawn_file_actions_addclose(&fileActions, stdinPipe[1])
456+
// Workaround for https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=89e435f3559c53084498e9baad22172b64429362
457+
// Change allowing for newer version of glibc
458+
guard let devNull = strdup("/dev/null") else {
459+
throw SystemError.posix_spawn(0, arguments)
460+
}
461+
defer { free(devNull) }
462+
// Open /dev/null as stdin.
463+
posix_spawn_file_actions_addopen(&fileActions, 0, devNull, O_RDONLY, 0)
475464

476465
var outputPipe: [Int32] = [-1, -1]
477466
var stderrPipe: [Int32] = [-1, -1]
@@ -482,7 +471,7 @@ public final class Process: ObjectIdentifierProtocol {
482471
// Open the write end of the pipe.
483472
posix_spawn_file_actions_adddup2(&fileActions, outputPipe[1], 1)
484473

485-
// Close the other ends of the pipe since they were dupped to 1.
474+
// Close the other ends of the pipe.
486475
posix_spawn_file_actions_addclose(&fileActions, outputPipe[0])
487476
posix_spawn_file_actions_addclose(&fileActions, outputPipe[1])
488477

@@ -494,7 +483,7 @@ public final class Process: ObjectIdentifierProtocol {
494483
try open(pipe: &stderrPipe)
495484
posix_spawn_file_actions_adddup2(&fileActions, stderrPipe[1], 2)
496485

497-
// Close the other ends of the pipe since they were dupped to 2.
486+
// Close the other ends of the pipe.
498487
posix_spawn_file_actions_addclose(&fileActions, stderrPipe[0])
499488
posix_spawn_file_actions_addclose(&fileActions, stderrPipe[1])
500489
}
@@ -511,14 +500,11 @@ public final class Process: ObjectIdentifierProtocol {
511500
throw SystemError.posix_spawn(rv, arguments)
512501
}
513502

514-
// Close the local read end of the input pipe.
515-
try close(fd: stdinPipe[0])
516-
517503
if outputRedirection.redirectsOutput {
518504
let outputClosures = outputRedirection.outputClosures
519505

520-
// Close the local write end of the output pipe.
521-
try close(fd: outputPipe[1])
506+
// Close the write end of the output pipe.
507+
try close(fd: &outputPipe[1])
522508

523509
// Create a thread and start reading the output on it.
524510
var thread = Thread { [weak self] in
@@ -531,8 +517,8 @@ public final class Process: ObjectIdentifierProtocol {
531517

532518
// Only schedule a thread for stderr if no redirect was requested.
533519
if !outputRedirection.redirectStderr {
534-
// Close the local write end of the stderr pipe.
535-
try close(fd: stderrPipe[1])
520+
// Close the write end of the stderr pipe.
521+
try close(fd: &stderrPipe[1])
536522

537523
// Create a thread and start reading the stderr output on it.
538524
thread = Thread { [weak self] in
@@ -544,8 +530,6 @@ public final class Process: ObjectIdentifierProtocol {
544530
self.stderr.thread = thread
545531
}
546532
}
547-
548-
return stdinStream
549533
#endif // POSIX implementation
550534
}
551535

@@ -747,15 +731,11 @@ private func open(pipe: inout [Int32]) throws {
747731
}
748732

749733
/// Close the given fd.
750-
private func close(fd: Int32) throws {
751-
func innerClose(_ fd: inout Int32) throws {
752-
let rv = TSCLibc.close(fd)
753-
guard rv == 0 else {
754-
throw SystemError.close(rv)
755-
}
734+
private func close(fd: inout Int32) throws {
735+
let rv = TSCLibc.close(fd)
736+
guard rv == 0 else {
737+
throw SystemError.close(rv)
756738
}
757-
var innerFd = fd
758-
try innerClose(&innerFd)
759739
}
760740

761741
extension Process.Error: CustomStringConvertible {
@@ -808,27 +788,3 @@ extension ProcessResult.Error: CustomStringConvertible {
808788
}
809789
}
810790
}
811-
812-
#if os(Windows)
813-
extension FileHandle: WritableByteStream {
814-
public var position: Int {
815-
return Int(offsetInFile)
816-
}
817-
818-
public func write(_ byte: UInt8) {
819-
write(Data([byte]))
820-
}
821-
822-
public func write<C: Collection>(_ bytes: C) where C.Element == UInt8 {
823-
write(Data(bytes))
824-
}
825-
826-
public func flush() {
827-
synchronizeFile()
828-
}
829-
830-
public func close() throws {
831-
closeFile()
832-
}
833-
}
834-
#endif

Tests/TSCBasicTests/ProcessTests.swift

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -188,23 +188,6 @@ class ProcessTests: XCTestCase {
188188
XCTAssertEqual(result2, "hello\n")
189189
}
190190

191-
func testStdin() throws {
192-
var stdout = [UInt8]()
193-
let process = Process(args: script("in-to-out"), outputRedirection: .stream(stdout: { stdoutBytes in
194-
stdout += stdoutBytes
195-
}, stderr: { _ in }))
196-
let stdinStream = try process.launch()
197-
198-
stdinStream.write("hello\n")
199-
stdinStream.flush()
200-
201-
try stdinStream.close()
202-
203-
try process.waitUntilExit()
204-
205-
XCTAssertEqual(String(decoding: stdout, as: UTF8.self), "hello\n")
206-
}
207-
208191
func testStdoutStdErr() throws {
209192
// A simple script to check that stdout and stderr are captured separatly.
210193
do {

Tests/TSCBasicTests/processInputs/in-to-out

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)