Skip to content

Commit f157ff9

Browse files
authored
fix a regression in swift run signal handling (#5933)
motivation: swift run should handle signals correctly, recent refactoring broke it changes: move the exec wrapper back to SwiftRunTool and rename it so its harder to regress
1 parent 689d162 commit f157ff9

File tree

3 files changed

+15
-13
lines changed

3 files changed

+15
-13
lines changed

Sources/Commands/SwiftRunTool.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
import ArgumentParser
1414
import Basics
1515
import CoreCommands
16+
import Foundation
1617
import PackageGraph
1718
import PackageModel
1819
import TSCBasic
@@ -257,7 +258,7 @@ public struct SwiftRunTool: SwiftCommand {
257258
}
258259

259260
let pathRelativeToWorkingDirectory = executablePath.relative(to: originalWorkingDirectory)
260-
try exec(path: executablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments)
261+
try execute(path: executablePath.pathString, args: [pathRelativeToWorkingDirectory.pathString] + arguments)
261262
}
262263

263264
/// Determines if a path points to a valid swift file.
@@ -280,6 +281,17 @@ public struct SwiftRunTool: SwiftCommand {
280281
return fileSystem.isFile(absolutePath)
281282
}
282283

284+
/// A safe wrapper of TSCBasic.exec.
285+
private func execute(path: String, args: [String]) throws -> Never {
286+
#if !os(Windows)
287+
// On platforms other than Windows, signal(SIGINT, SIG_IGN) is used for handling SIGINT by DispatchSourceSignal,
288+
// but this process is about to be replaced by exec, so SIG_IGN must be returned to default.
289+
signal(SIGINT, SIG_DFL)
290+
#endif
291+
292+
try TSCBasic.exec(path: path, args: args)
293+
}
294+
283295
public init() {}
284296
}
285297

@@ -288,3 +300,4 @@ private extension Basics.Diagnostic {
288300
.warning("'swift run file.swift' command to interpret swift files is deprecated; use 'swift file.swift' instead")
289301
}
290302
}
303+

Sources/CoreCommands/SwiftTool.swift

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -102,17 +102,6 @@ extension SwiftCommand {
102102
}
103103
}
104104

105-
/// A safe wrapper of TSCBasic.exec.
106-
func exec(path: String, args: [String]) throws -> Never {
107-
#if !os(Windows)
108-
// On platforms other than Windows, signal(SIGINT, SIG_IGN) is used for handling SIGINT by DispatchSourceSignal,
109-
// but this process is about to be replaced by exec, so SIG_IGN must be returned to default.
110-
signal(SIGINT, SIG_DFL)
111-
#endif
112-
113-
try TSCBasic.exec(path: path, args: args)
114-
}
115-
116105
public final class SwiftTool {
117106
#if os(Windows)
118107
// unfortunately this is needed for C callback handlers used by Windows shutdown handler

Tests/CommandsTests/RunToolTests.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ final class RunToolTests: CommandsTestCase {
142142
#if os(Windows)
143143
XCTAssertEqual(result.exitStatus, .abnormal(exception: 2))
144144
#else
145-
XCTAssertEqual(result.exitStatus, .signalled(signal: 2))
145+
XCTAssertEqual(result.exitStatus, .signalled(signal: SIGINT))
146146
#endif
147147
}
148148

0 commit comments

Comments
 (0)