@@ -121,6 +121,9 @@ public final class Process: ObjectIdentifierProtocol {
121121 public enum Error : Swift . Error {
122122 /// The program requested to be executed cannot be found on the existing search paths, or is not executable.
123123 case missingExecutableProgram( program: String )
124+
125+ /// The current OS does not support the workingDirectory API.
126+ case workingDirectoryNotSupported
124127 }
125128
126129 public enum OutputRedirection {
@@ -226,7 +229,6 @@ public final class Process: ObjectIdentifierProtocol {
226229 /// Value: Path to the executable, if found.
227230 static private var validatedExecutablesMap = [ String: AbsolutePath? ] ( )
228231
229- #if os(macOS)
230232 /// Create a new process instance.
231233 ///
232234 /// - Parameters:
@@ -254,7 +256,6 @@ public final class Process: ObjectIdentifierProtocol {
254256 self . verbose = verbose
255257 self . startNewProcessGroup = startNewProcessGroup
256258 }
257- #endif
258259
259260 /// Create a new process instance.
260261 ///
@@ -404,16 +405,24 @@ public final class Process: ObjectIdentifierProtocol {
404405 posix_spawn_file_actions_init ( & fileActions)
405406 defer { posix_spawn_file_actions_destroy ( & fileActions) }
406407
407- #if os(macOS)
408408 if let workingDirectory = workingDirectory? . pathString {
409+ #if os(macOS)
409410 // The only way to set a workingDirectory is using an availability-gated initializer, so we don't need
410411 // to handle the case where the posix_spawn_file_actions_addchdir_np method is unavailable. This check only
411412 // exists here to make the compiler happy.
412413 if #available( macOS 10 . 15 , * ) {
413414 posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory)
414415 }
416+ #elseif os(Linux)
417+ guard SPM_posix_spawn_file_actions_addchdir_np_supported ( ) else {
418+ throw Process . Error. workingDirectoryNotSupported
419+ }
420+
421+ SPM_posix_spawn_file_actions_addchdir_np ( & fileActions, workingDirectory)
422+ #else
423+ throw Process . Error. workingDirectoryNotSupported
424+ #endif
415425 }
416- #endif
417426
418427 // Workaround for https://sourceware.org/git/gitweb.cgi?p=glibc.git;h=89e435f3559c53084498e9baad22172b64429362
419428 // Change allowing for newer version of glibc
@@ -691,6 +700,8 @@ extension Process.Error: CustomStringConvertible {
691700 switch self {
692701 case . missingExecutableProgram( let program) :
693702 return " could not find executable for ' \( program) ' "
703+ case . workingDirectoryNotSupported:
704+ return " workingDirectory is not supported in this platform "
694705 }
695706 }
696707}
0 commit comments