dyld: Library not loaded in Swift executable release build

I've been running into a dyld loading issue while trying to build a Swift command line application. My Package.swift is fairly simple, with dependencies on only ArgumentParser and SwiftToolsSupport:

Code Block
dependencies: [
.package(url: "https://github.com/apple/swift-tools-support-core.git", from: "0.1.10"),
.package(url: "https://github.com/apple/swift-argument-parser.git", from: "0.2.0"),
],
targets: [
.target(name: "progress",
dependencies: [
.product(name: "ArgumentParser", package: "swift-argument-parser"),
.product(name: "SwiftToolsSupport", package: "swift-tools-support-core")]),
...


It runs fine via swift run. Here's what I'm doing to produce a release executable:
Code Block
swift build -c release
cp -f .build/release/progress /usr/local/bin/progress

However, when I run the executable, I get the issue:
Code Block
dyld: Library not loaded: @rpath/libSwiftToolsSupport.dylib
Referenced from: /usr/local/bin/progress
Reason: image not found


I've run otool -L /usr/local/bin/progress, which outputs the paths:
  • /System/Library/Frameworks/CoreFoundation.framework/Versions/A/CoreFoundation (compatibility version 150.0.0, current version 1677.104.0)

  • /usr/lib/libobjc.A.dylib (compatibility version 1.0.0, current version 228.0.0)

  • /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 1281.100.1)

  • @rpath/libSwiftToolsSupport.dylib (compatibility version 0.0.0, current version 0.0.0)

  • /System/Library/Frameworks/Foundation.framework/Versions/C/Foundation (compatibility version 300.0.0, current version 1677.104.0)

  • @rpath/libswiftCore.dylib (compatibility version 1.0.0, current version 1103.8.25)

  • @rpath/libswiftDarwin.dylib (compatibility version 1.0.0, current version 0.0.0)

  • @rpath/libswiftFoundation.dylib (compatibility version 1.0.0, current version 0.0.0)


I can get the release executable to work by copying libSwiftToolsSupport.dylib into /usr/local/lib/ and overriding its install path on the executable:
Code Block
cp -f .build/release/libSwiftToolsSupport.dylib /usr/local/lib/
install_name_tool -change "@rpath/libSwiftToolsSupport.dylib" "/usr/local/lib/libSwiftToolsSupport.dylib" "/usr/local/bin/progress"


However, this is seems like a bad workaround. Is there a better way to do this, that doesn't require touching the local filesystem? Note that I'm not using Xcode, purely the Package.swift and command line arguments (e.g. swift build -c release).
if you specify "SwiftToolsSupport-auto" product rather than "SwiftToolsSupport" as your dependency, it will compile all the code into one binary, then you do not need to worry about where to place your libSwiftToolsSupport.dylib file.
Just tried out the "SwiftToolsSupport-auto" and you have to make sure in all sub dependencies it is also added as "SwiftToolsSupport-auto" otherwise it will not work in sandboxed applications.

I tested this by adding one of my projects to xcode 12 project as a swift package dependency. This is because I want to use this on iOS too.
dyld: Library not loaded in Swift executable release build
 
 
Q