swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.7k stars 1.33k forks source link

Building an executable target doesn't link it #7133

Open dabrahams opened 9 months ago

dabrahams commented 9 months ago

Description

If you swift build --target SomeExecutableTarget and then swift run --skip-build SomeExecutableTarget, it fails because the executable is missing.

Expected behavior

Creating executable package: demo
Creating Package.swift
Creating Sources/
Creating Sources/main.swift

Building for debugging...
[1/2] Emitting module demo
[2/2] Compiling demo main.swift
[2/3] Linking demo
Build complete! (3.88s)
BUILT TARGET
Hello, world!
Hello, world!

Actual behavior

Creating executable package: demo
Creating Package.swift
Creating Sources/
Creating Sources/main.swift

Building for debugging...
[1/2] Emitting module demo
[2/2] Compiling demo main.swift
Build complete! (3.64s)
BUILT TARGET
error: exec error: No such file or directory (2): /private/tmp/demo/.build/arm64-apple-macosx/debug/demo .build/arm64-apple-macosx/debug/demo
RUN SKIPPING BUILD FAILED
[0/1] Planning build
Building for debugging...
[0/1] Linking demo
Build complete! (0.40s)
Hello, world!

Steps to reproduce

  1. mdkir demo && cd demo && swift package init --type executable && echo
  2. swift build --target demo && echo BUILT TARGET
  3. swift run --skip-build demo || echo RUN SKIPPING BUILD FAILED
  4. swift run demo

Swift Package Manager version/commit hash

5.9

Swift & OS version (output of swift --version ; uname -a)

swift-driver version: 1.87.1 Apple Swift version 5.9 (swiftlang-5.9.0.128.108 clang-1500.0.40.1) Target: arm64-apple-macosx14.0 Darwin DaveA-MBP16.localdomain 23.1.0 Darwin Kernel Version 23.1.0: Mon Oct 9 21:27:24 PDT 2023; root:xnu-10002.41.9~6/RELEASE_ARM64_T6000 arm64

dabrahams commented 9 months ago

It appears that the only way to request that the executable be linked without building the whole package is to make a product and use swift build --product.

dabrahams commented 9 months ago

If I make a library target that depends on the executable target and build that, the executable is linked.

neonichu commented 9 months ago

This sounds like an issue with the implicitness of executable products. It seems like --target should actually build the implicit product for an executable to make this behavior more consistent.