You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
add-target breaks resulting package when target name is not valid Swift identifier (#7764)
Executing `swift package add-target my-target --type executable` breaks
the updated package because the executable target name is not a valid
Swift identifier.
### Motivation:
After executing `swift package add-target my-target --type executable`,
I expect the generated stub to not break the modified package. However,
in this case, the resulting stub file would contain invalid Swift code:
```swift
@main
struct my-targetMain {
static func main() {
print("Hello, world")
}
}
```
The same issue applies to `macro` and `test` target types. It's also
important to note that `dash case` is quite often used for target names,
like e.g. in
[swift-argument-parser](https://github.com/apple/swift-argument-parser/blob/main/Package.swift#L65-L68).
Additionaly, when `AddTarget` is consumed via an API (as opposed to the
CLI), the following parsing error is thrown into `stdout`:
<img width="796" alt="Screenshot 2024-07-08 at 08 40 51"
src="https://github.com/swiftlang/swift-package-manager/assets/1008612/c2696127-3e34-47a2-aa60-a843cfb64bda">
### Modifications:
I believe there are at least two ways to solve this issue:
1. Use a predefined type name in the generated stub, such as `struct
ExampleMain`, so it doesn't depend on the target name.
2. Introduce a sanitizer function to ensure that the target name is
always a valid Swift identifier. For example, a function that would
convert "$my-target-name%" into "myTargetName".
For the sake of simplicity, I opted for approach `#1` in the proposed
PR. However, if it's necessary to preserve target names in the generated
stubs, we can certainly go with approach `#2`.
### Result:
`swift package add-target my-target --type executable`
```swift
@main
struct ExampleMain {
static func main() {
print("Hello, world")
}
}
```
0 commit comments