after dealing with some SwiftPM weirdness today, i did some deeper investigation into the significance of the filename main.swift
to the toolchain and came across this passage within SE-0294:
On the implementation side, this proposal also updates the logic in SwiftPM so that:
- if the package tools-version is less than 5.4, a target is considered to be executable in exactly the same way as in versions of SwiftPM prior to 5.4
- if the package tools-version is greater than or equal to 5.4, then:
- if the target is declared using
.executableTarget
, then it is considered to be an executable target- if the target is declared using
.target
, then source files are examined using the same rules as prior to 5.4, and a warning suggesting that the target be declared using.executableTarget
is emitted if the target is considered executable under those rules
The effect of this logic is that, starting with SwiftPM tools-version 5.4, declaring an executable target by using
.target
and having it inferred to be an executable by the presence of a source file with a base name ofmain
is considered deprecated but still works.
This approach eases the transition for any package that adopts tools-version 5.4, and provides better diagnostics to change the declarations of executable targets to use
.executableTarget
. If technically feasible, the warning should have a fix-it to change the associated.target
declaration to.executableTarget
.
A future tools-version will remove the warning and treat any target that is declared using
.target
as a library target.
as i understand it, the rules boil down to:
-
if swift-tools-version < some unspecified tools version, then a module is executable if it contains a file named
main.swift
. -
if swift-tools-version >= 5.4, then a module is also executable if it is declared as an
executableTarget
, regardless of whether it has amain.swift
file.
i wondered what to substitute for some unspecified tools version, and empirically the behavior still seems to be in place as of swift-tools-version 5.10.
is there any plan to fix the upper bound of this version range to some defined swift-tools-version (e.g. 5.10?) if not, should we declare the meaning of main.swift
to be permanent?