Skip to content

Commit e6cc238

Browse files
committed
Implement arg0 override feature for Windows
1 parent 4392939 commit e6cc238

File tree

7 files changed

+738
-499
lines changed

7 files changed

+738
-499
lines changed

Sources/Subprocess/Configuration.swift

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -250,40 +250,6 @@ extension Executable: CustomStringConvertible, CustomDebugStringConvertible {
250250
}
251251
}
252252

253-
extension Executable {
254-
internal func possibleExecutablePaths(
255-
withPathValue pathValue: String?
256-
) -> Set<String> {
257-
switch self.storage {
258-
case .executable(let executableName):
259-
#if os(Windows)
260-
// Windows CreateProcessW accepts executable name directly
261-
return Set([executableName])
262-
#else
263-
var results: Set<String> = []
264-
// executableName could be a full path
265-
results.insert(executableName)
266-
// Get $PATH from environment
267-
let searchPaths: Set<String>
268-
if let pathValue = pathValue {
269-
let localSearchPaths = pathValue.split(separator: ":").map { String($0) }
270-
searchPaths = Set(localSearchPaths).union(Self.defaultSearchPaths)
271-
} else {
272-
searchPaths = Self.defaultSearchPaths
273-
}
274-
for path in searchPaths {
275-
results.insert(
276-
FilePath(path).appending(executableName).string
277-
)
278-
}
279-
return results
280-
#endif
281-
case .path(let executablePath):
282-
return Set([executablePath.string])
283-
}
284-
}
285-
}
286-
287253
// MARK: - Arguments
288254

289255
/// A collection of arguments to pass to the subprocess.
@@ -304,7 +270,6 @@ public struct Arguments: Sendable, ExpressibleByArrayLiteral, Hashable {
304270
self.executablePathOverride = nil
305271
}
306272

307-
#if !os(Windows) // Windows does NOT support arg0 override
308273
/// Create an `Argument` object using the given values, but
309274
/// override the first Argument value to `executablePathOverride`.
310275
/// If `executablePathOverride` is nil,
@@ -321,7 +286,7 @@ public struct Arguments: Sendable, ExpressibleByArrayLiteral, Hashable {
321286
self.executablePathOverride = nil
322287
}
323288
}
324-
289+
#if !os(Windows) // Windows does not support non-unicode arguments
325290
/// Create an `Argument` object using the given values, but
326291
/// override the first Argument value to `executablePathOverride`.
327292
/// If `executablePathOverride` is nil,
@@ -338,12 +303,12 @@ public struct Arguments: Sendable, ExpressibleByArrayLiteral, Hashable {
338303
self.executablePathOverride = nil
339304
}
340305
}
341-
#endif
342306

343307
public init(_ array: [[UInt8]]) {
344308
self.storage = array.map { .rawBytes($0) }
345309
self.executablePathOverride = nil
346310
}
311+
#endif
347312
}
348313

349314
extension Arguments: CustomStringConvertible, CustomDebugStringConvertible {

Sources/Subprocess/Platforms/Subprocess+Unix.swift

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,33 @@ extension Executable {
300300
return executablePath.string
301301
}
302302
}
303+
304+
internal func possibleExecutablePaths(
305+
withPathValue pathValue: String?
306+
) -> Set<String> {
307+
switch self.storage {
308+
case .executable(let executableName):
309+
var results: Set<String> = []
310+
// executableName could be a full path
311+
results.insert(executableName)
312+
// Get $PATH from environment
313+
let searchPaths: Set<String>
314+
if let pathValue = pathValue {
315+
let localSearchPaths = pathValue.split(separator: ":").map { String($0) }
316+
searchPaths = Set(localSearchPaths).union(Self.defaultSearchPaths)
317+
} else {
318+
searchPaths = Self.defaultSearchPaths
319+
}
320+
for path in searchPaths {
321+
results.insert(
322+
FilePath(path).appending(executableName).string
323+
)
324+
}
325+
return results
326+
case .path(let executablePath):
327+
return Set([executablePath.string])
328+
}
329+
}
303330
}
304331

305332
// MARK: - PreSpawn

0 commit comments

Comments
 (0)