executableTarget() methods are deprecated. What is the alternative?

Hi folks,

In the documentation for PackageDescription.Target, both of the two static methods .executableTarget(...) are marked as deprecated. There is no suggestion in the documentation as to what alternative people should use, if not those static methods.

Most examples in the wild I've found currently use .executableTarget(). The swift package init --type tool command will generate a Package.swift file which uses .executableTarget() (on Apple Swift version 6.0.2).

The only example I've found which has a possible alternative is the first example within the Products section of this doc. In this example, the more generic .target() is used, while a corresponding product is also specified using the Product.executable() static method. Is this the recommended practice? I'm inclined to believe its not - since when I use this approach and build I see a warning similar to:

warning: 'my-package': 'SomeTarget' was identified as an executable target given the presence of a 'main.swift' file. Starting with tools version 5.4.0 executable targets should be declared as 'executableTarget()'

This seems to directly state that I should be using the deprecated .executableTarget(...). Is that only because I happened to use a main.swift file? Is there some alternative to naming that would eliminate this warning, without having to introduce the deprecated .executableTarget(...)?

Is there any more nuance available regarding when and why one approach or another should be chosen?

Thanks

This might be the executableTarget you are looking for:

1 Like

Wow! I guess I missed that. :man_facepalming:

I'm probably not the only one. The structure of the PackageDescription.Target document is very confusing. I'll try to describe the shortcomings in an issue so it can be addressed.

Part of my take on improving the doc

Upon really careful inspection, it seems the packageAccess optional argument is the only difference. It's a bit silly, since that argument has the default value of true. Therefore from the consumer's perspective, the deprecated method is fully compatible with this non-deprecated version. The "deprecated" badge is unnecessarily alarming. I'd argue that the deprecated methods should just be removed from the documentation in favor of the non-deprecated one.

Yet still, my questions regarding the alternative (using an executable product which refers to a generic target) still remain. What are the nuances? Using this alternative, I just tried to use swift run (both with and without the executable product name) and noticed it will build my product but will not run it. Instead the command just hangs. Is this expected (or is it a bug)?