Swift Subprocess - Failed to spawn UnderlyingError(rawValue: 20

Here is a simple program that should not crash. (Amazingly, it runs fine in Xcode Version 26.2 (17C52).) I am running Tahoe 26.2.

  1. Output
[I] release> ./subprocess-issue
Hi
Swift/ErrorType.swift:254: Fatal error: Error raised at top level: Failed to spawn the new process with underlying error: UnderlyingError(rawValue: 20)
fish: Job 1, './subprocess-issue' terminated by signal SIGTRAP (Trace or breakpoint trap)
  1. Swift
[I] SubprocessIssue> swift --version
swift-driver version: 1.127.14.1 Apple Swift version 6.2.3 (swiftlang-6.2.3.3.21 clang-1700.6.3.2)
Target: arm64-apple-macosx26.0
  1. Program Structure
[I] SubprocessIssue> tree -L 2
.
โ”œโ”€โ”€ Package.resolved
โ”œโ”€โ”€ Package.swift
โ””โ”€โ”€ Sources
    โ””โ”€โ”€ SubprocessIssue
  1. Program Package
// swift-tools-version: 6.2
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
    name: "SubprocessIssue",
    platforms: [.macOS(.v26)],
    dependencies: [
        .package(url: "https://github.com/swiftlang/swift-subprocess.git", branch: "main")
    ],
    targets: [
        .executableTarget(
            name: "subprocess-issue",
            dependencies: [
                .product(name: "Subprocess", package: "swift-subprocess")
            ]
        )
    ]
)
  1. Program Code
import Subprocess

@main
struct SubprocessIssue {
    static func main() async throws {
        print("Hi")
        let _ = try await run(.name("ls"), output: .discarded)
        print("Goodbye")
    }
}
  1. Build
[I] SubprocessIssue> rm -rf .build .swiftpm
[I] SubprocessIssue> swift build -c release

... bla bla ...

Building for production...
<module-includes>:1:9: note: in file included from <module-includes>:1:
1 | #import "process_shims.h"
  |         `- note: in file included from <module-includes>:1:
2 | #import "target_conditionals.h"
3 | 

/Users/po/ZLocal/ZDev/Swift/Play/SubprocessIssue/.build/checkouts/swift-subprocess/Sources/_SubprocessCShims/include/process_shims.h:44:5: warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
 42 | 
 43 | int _subprocess_pthread_create(
 44 |     pthread_t * _Nonnull ptr,
    |     |- warning: pointer is missing a nullability type specifier (_Nonnull, _Nullable, or _Null_unspecified)
    |     |- note: insert '_Nullable' if the pointer may be null
    |     `- note: insert '_Nonnull' if the pointer should never be null
 45 |     pthread_attr_t const * _Nullable attr,
 46 |     void * _Nullable (* _Nonnull start)(void * _Nullable),
[11/11] Linking subprocess-issue
Build complete! (8.60s)

  1. Output from debugger
[I] debug> ls  -lF | grep "*"
-rwxr-xr-x    1 po  staff  2137616 Jan 22 09:02 subprocess-issue*
[I] debug> ./subprocess-issue
Hi
Swift/ErrorType.swift:254: Fatal error: Error raised at top level: Failed to spawn the new process with underlying error: UnderlyingError(rawValue: 20)

๐Ÿ’ฃ Program crashed: System trap at 0x000000019e5b59d4

Platform: arm64 macOS 26.2 (25C56)

Thread 0 crashed:

  0 0x000000019e5b59d4 _assertionFailure(_:_:file:line:flags:) + 176 in libswiftCore.dylib
  1 0x000000019e661f8c swift_errorInMain + 604 in libswiftCore.dylib
... 

Backtrace took 0.57s

fish: Job 1, './subprocess-issue' terminated by signal SIGTRAP (Trace or breakpoint trap)

Probably this assertions issue? Try running the build command in verbose mode with -v and make sure which Swift compiler is being pulled in: the Xcode one you showed above or some other OSS compiler version, which have assertions enabled.

The result is the same with swiftly use 6.2.3., as with swiftly use xcode. The same with -c debug (which I think always has asserts enabled) and -c release.

As a general issue - I have code that (even with swift set to xcode in swiftly) builds release and debug just fine from the command line, but will not build in Xcode. But that's a different issue. (I'll wait for 6.3 to see if it still doesn't work before reporting it).

Here is what I got at the terminal, if that helps.

[I] SubprocessIssue> swift --version
Apple Swift version 6.2.3 (swift-6.2.3-RELEASE)
Target: arm64-apple-macosx26.0
Build config: +assertions

[I] SubprocessIssue> swift build -v -c release
... bla, bla

Then in .build/release:

[I] release> ls  -l@F | grep '\*'
-rwxr-xr-x   1 po  staff  1258928 Jan 23 08:53 subprocess-issue*
[I] release> ./subprocess-issue
Hi
Swift/ErrorType.swift:254: Fatal error: Error raised at top level: Failed to spawn the new process with underlying error: UnderlyingError(rawValue: 20)
fish: Job 1, './subprocess-issue' terminated by signal SIGTRAP (Trace or breakpoint trap)

Still the same issue.

Hi @psummerland! You might be hitting `ENOTDIR` error when `PATH` includes existing non-directory paths ยท Issue #213 ยท swiftlang/swift-subprocess ยท GitHub which was recently fixed. Can you try with the main branch again?

Hi,

  1. Using .package(url: "https://github.com/swiftlang/swift-subprocess.git", from: "0.3.0")
    fixed the problem.

  2. I am sure that it was the path issue:

With .package(url: "https://github.com/swiftlang/swift-subprocess.git", branch: "main"), this morning "/bin/ls" worked, "ls" crashed.

@main
struct SubprocessIssue {
    static func main() async throws {
        print("Hi")
        let _ = try await run(.name("/bin/ls"), output: .discarded)
//        let _ = try await run(.name("ls"), output: .string(limit: 4096))
        print("Goodbye")
    }
}

You might want to change the branch: "main", to, say, "from: "0.3.0" in the README here:

To use Subprocess in a SwiftPM project, add it as a package dependency to your Package.swift:

dependencies: [ .package(url: "https://github.com/swiftlang/swift-subprocess.git", branch: "main") ]

For what its worth - this package has a great API and a great README.

Thanks for fixing this.

1 Like