Skip to content
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

Drain stdout pipe while command is running #614

Merged
merged 5 commits into from
Oct 14, 2024
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@

## Master

- Drain stdout while shell commands are running to prevent execution from locking up if there's too much output [@jflan-dd][] - [#614](https://github.com/danger/swift/pull/614)

## 3.20.0
- Remove deprecated `lint` function with `lintAllFiles` flag [@417-72KI][] - [#622](https://github.com/danger/swift/pull/622)
- Updated Swift 6 build process: Danger files moved to .build/debug/Modules, and SwiftFormat module map conflict resolved by adjusting the Swift import search path. [@abhi-m-simformsolutons][] -[#626](https://github.com/danger/swift/pull/626)
Expand Down
11 changes: 8 additions & 3 deletions Sources/DangerShellExecutor/ShellExecutor.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,11 @@ public struct ShellExecutor: ShellExecuting {
let pipe = Pipe()
task.standardOutput = pipe
task.launch()
task.waitUntilExit()

let data = pipe.fileHandleForReading.readDataToEndOfFile()

task.waitUntilExit()
Copy link
Member

Choose a reason for hiding this comment

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

Would you still receive all the text output coming from the executed shell command after this change?
Given we are reading before the task ends

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We're calling readDataToEndOfFile() so we'll get the entire output of the command from the pipe before continuing to task.waitUntilExit(), which I assume will basically immediately complete at that point.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@f-meloni does that seem correct?

Copy link
Member

Choose a reason for hiding this comment

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

Let's give it a chance :)


return String(data: data, encoding: .utf8)!.trimmingCharacters(in: .whitespacesAndNewlines)
}

Expand All @@ -83,19 +85,22 @@ public struct ShellExecutor: ShellExecuting {
let stderr = Pipe()
task.standardError = stderr
task.launch()
task.waitUntilExit()

// Pull out the STDOUT as a string because we'll need that regardless
let stdoutData = stdout.fileHandleForReading.readDataToEndOfFile()
let stdoutString = String(data: stdoutData, encoding: .utf8)!

// Read from STDERR to ensure the `Pipe` does not fill up
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()

task.waitUntilExit()

// 0 is no problems in unix land
if task.terminationStatus == 0 {
return stdoutString.trimmingCharacters(in: .whitespacesAndNewlines)
}

// OK, so it failed, raise a new error with all the useful metadata
let stderrData = stderr.fileHandleForReading.readDataToEndOfFile()
let stderrString = String(data: stderrData, encoding: .utf8)!

throw SpawnError.commandFailed(command: command,
Expand Down
Loading