Skip to content

Commit 4bc3fea

Browse files
Executable.resolveExecutablePath should be async (#264)
This method requires hitting the filesystem and should therefore be async as it can block Closes #263
1 parent e417c3f commit 4bc3fea

4 files changed

Lines changed: 20 additions & 12 deletions

File tree

Sources/Subprocess/Configuration.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -290,9 +290,11 @@ public struct Executable: Sendable, Hashable {
290290
return .init(_config: .path(filePath))
291291
}
292292
/// Resolves the full executable path using the given environment.
293-
public func resolveExecutablePath(in environment: Environment) throws(SubprocessError) -> FilePath {
294-
let path = try self.resolveExecutablePath(withPathValue: environment.pathValue())
295-
return FilePath(path)
293+
public func resolveExecutablePath(in environment: Environment) async throws(SubprocessError) -> FilePath {
294+
try await runOnBackgroundThread { () throws(SubprocessError) -> FilePath in
295+
let path = try self.resolveExecutablePath(withPathValue: environment.pathValue())
296+
return FilePath(path)
297+
}
296298
}
297299
}
298300

Tests/SubprocessTests/LinterTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ import SystemPackage
1818
#endif
1919
@testable import Subprocess
2020

21-
private func enableLintingTest() -> Bool {
21+
private func enableLintingTest() async -> Bool {
2222
guard CommandLine.arguments.first(where: { $0.contains("/.build/") }) != nil else {
2323
return false
2424
}
2525
#if os(macOS)
2626
// Use xcrun
2727
do {
28-
_ = try Executable.path("/usr/bin/xcrun")
28+
_ = try await Executable.path("/usr/bin/xcrun")
2929
.resolveExecutablePath(in: .inherit)
3030
return true
3131
} catch {
@@ -34,7 +34,7 @@ private func enableLintingTest() -> Bool {
3434
#else
3535
// Use swift-format directly
3636
do {
37-
_ = try Executable.name("swift-format")
37+
_ = try await Executable.name("swift-format")
3838
.resolveExecutablePath(in: .inherit)
3939
return true
4040
} catch {

Tests/SubprocessTests/TestSupport.swift

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,10 @@ extension Trait where Self == ConditionTrait {
8989
/// This test requires bash to run (instead of sh)
9090
static var requiresBash: Self {
9191
enabled(
92-
if: (try? Executable.name("bash").resolveExecutablePath(in: .inherit)) != nil,
93-
"This test requires bash (install `bash` package on Linux/BSD)"
92+
"This test requires bash (install `bash` package on Linux/BSD)",
93+
{
94+
(try? await Executable.name("bash").resolveExecutablePath(in: .inherit)) != nil
95+
}
9496
)
9597
}
9698
}

Tests/SubprocessTests/UnixTests.swift

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,10 @@ extension SubprocessUnixTests {
120120
"This test requires root privileges"
121121
),
122122
.enabled(
123-
if: (try? Executable.name("ps").resolveExecutablePath(in: .inherit)) != nil,
124-
"This test requires ps (install procps package on Debian or RedHat Linux distros)"
123+
"This test requires ps (install procps package on Debian or RedHat Linux distros)",
124+
{
125+
(try? await Executable.name("ps").resolveExecutablePath(in: .inherit)) != nil
126+
}
125127
)
126128
)
127129
func testSubprocessPlatformOptionsProcessGroupID() async throws {
@@ -145,8 +147,10 @@ extension SubprocessUnixTests {
145147

146148
@Test(
147149
.enabled(
148-
if: (try? Executable.name("ps").resolveExecutablePath(in: .inherit)) != nil,
149-
"This test requires ps (install procps package on Debian or RedHat Linux distros)"
150+
"This test requires ps (install procps package on Debian or RedHat Linux distros)",
151+
{
152+
(try? await Executable.name("ps").resolveExecutablePath(in: .inherit)) != nil
153+
}
150154
)
151155
)
152156
func testSubprocessPlatformOptionsCreateSession() async throws {

0 commit comments

Comments
 (0)