Skip to content

TSCBasic: properly track abnormal exits on Windows #271

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

Merged
merged 1 commit into from
Dec 20, 2021
Merged
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
20 changes: 17 additions & 3 deletions Sources/TSCBasic/Process.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,10 @@ public struct ProcessResult: CustomStringConvertible {
public enum ExitStatus: Equatable {
/// The process was terminated normally with a exit code.
case terminated(code: Int32)
#if !os(Windows)
#if os(Windows)
/// The process was terminated abnormally.
case abnormal(exception: UInt32)
#else
/// The process was terminated due to a signal.
case signalled(signal: Int32)
#endif
Expand Down Expand Up @@ -64,12 +67,17 @@ public struct ProcessResult: CustomStringConvertible {
arguments: [String],
environment: [String: String],
exitStatusCode: Int32,
normal: Bool,
output: Result<[UInt8], Swift.Error>,
stderrOutput: Result<[UInt8], Swift.Error>
) {
let exitStatus: ExitStatus
#if os(Windows)
exitStatus = .terminated(code: exitStatusCode)
if normal {
exitStatus = .terminated(code: exitStatusCode)
} else {
exitStatus = .abnormal(exception: UInt32(exitStatusCode))
}
#else
if WIFSIGNALED(exitStatusCode) {
exitStatus = .signalled(signal: WTERMSIG(exitStatusCode))
Expand Down Expand Up @@ -726,6 +734,7 @@ public final class Process {
let p = _process!
p.waitUntilExit()
let exitStatusCode = p.terminationStatus
let normalExit = p.terminationReason == .exit
#else
var exitStatusCode: Int32 = 0
var result = waitpid(processID, &exitStatusCode, 0)
Expand All @@ -735,13 +744,15 @@ public final class Process {
if result == -1 {
self.state = .failed(SystemError.waitpid(errno))
}
let normalExit = !WIFSIGNALED(result)
#endif

// Construct the result.
let executionResult = ProcessResult(
arguments: arguments,
environment: environment,
exitStatusCode: exitStatusCode,
normal: normalExit,
output: stdoutResult,
stderrOutput: stderrResult
)
Expand Down Expand Up @@ -970,7 +981,10 @@ extension ProcessResult.Error: CustomStringConvertible {
switch result.exitStatus {
case .terminated(let code):
stream <<< "terminated(\(code)): "
#if !os(Windows)
#if os(Windows)
case .abnormal(let exception):
stream <<< "abnormal(\(exception)): "
#else
case .signalled(let signal):
stream <<< "signalled(\(signal)): "
#endif
Expand Down