SwiftPM executable target – assets work from Xcode but not the command-line

Context

Consider a simple Swift Package with a MyLibrary and MyExecutable target.
The aim is for MyExecutable to use MyLibrary (which has an image).

The MyLibrary target has 2 files, a Resources/Assets.xcassets catalog (with 1 image named “Test”) and an Images.swift file like so:

private extension NSImage {
    static let test = Bundle.module.image(forResource: "Test")
}

public struct Images {
    public static func printTestExists() {
        print("Exists: (NSImage.test != nil)")
    }
}

Please note in particular the use of Bundle.module here. The MyExecutable consists of merely a main.swift file that imports MyLibrary and runs:

Images.printTestExists()

Issue

When running the MyExecutable scheme in Xcode, it works as expected printing:

Exists: true

However, when running the executable from the command line:

swift run -c release MyExecutable

it crashes with the error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[NSBundle imageForResource:]: unrecognized selector sent to instance

Question

Why do assets seem not to work from the command-line? I’m only executing the swift run command so the executable file should have access to the library bundle, right?

Package.swift

The assets are processed as a resource as you would expect

let package = Package(
    name: "MyLibrary",
    platforms: [.macOS(.v13)],
    products: [
        .library(name: "MyLibrary", targets: ["MyLibrary"]),
        .executable(name: "MyExecutable", targets: ["MyExecutable"])
    ],
    targets: [
        .target(name: "MyLibrary", resources: [.process("Resources")] ),
        .executableTarget(name: "MyExecutable", dependencies: ["MyLibrary"])
    ]
)

Aside

Using the following SwiftUI code from the command-line would similarly not show an image but works when run from Xcode:

Image("Test", bundle: .module)

Attachments

References

I found a similar issue on Swift forums

To only half answer my own question, I believe the issue is just that swift run does not support assets. Hopefully, a future answer shows us how to make it work.

I managed to workaround the issue using the xcodebuild command instead

# Set this to what you want (this is inside the SwiftPM `.build` directory)
derivedDataPath=".build/xcode-derived-data"
    
xcodebuild build 
    -configuration release 
    -scheme MyExecutable 
    -sdk macosx 
    -destination 'platform=macOS' 
    -derivedDataPath "$derivedDataPath"
   
$derivedDataPath/Build/Products/release/MyExecutable

To see other destinations run

xcodebuild -showdestinations -scheme MyExecutable

It does not require making an .xcodeproj file which is nice, so we can stay in the realm of SwiftPM. Still, swift run would be preferable, but I hope this helps in the interim.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật