swift: [SR-12683] `@main` is no longer usable due to misdetection of top level code
| Previous ID | SR-12683 |
| Radar | rdar://problem/62475602 |
| Original Reporter | @compnerd |
| Type | Bug |
Attachment: Download
Additional Detail from JIRA
| Votes | 16 |
| Component/s | |
| Labels | Bug |
| Assignee | None |
| Priority | Medium |
md5: d99a1a03d2003d32b1bfd6a2ae65a2fa
is duplicated by:
Issue Description:
There seems to be a recent regression with comment blocks and functions being treated as top level code which prevents the use of `@main`.
About this issue
- Original URL
- State: open
- Created 7 years ago
- Reactions: 7
- Comments: 15 (5 by maintainers)
Commits related to this issue
- Reanme main.swift to App.swift Fix 'main' attribute cannot be used in a module that contains top-level code https://github.com/apple/swift/issues/55127 — committed to ykws/swift-cloud-example by ykws 4 years ago
- feat: Implement PR endpoint and argument parsing Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Asyn... — committed to cybersonik/github-stats by cybersonik 4 years ago
- feat: Implement PR endpoint and argument parsing Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async... — committed to cybersonik/github-stats by cybersonik 4 years ago
- feat: Implement PR endpoint and argument parsing Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup A... — committed to cybersonik/github-stats by cybersonik 4 years ago
- feat: Implement PR endpoint and argument parsing Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup A... — committed to cybersonik/github-stats by cybersonik 4 years ago
- feat: Implement PR endpoint and argument parsing Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup A... — committed to cybersonik/github-stats by cybersonik 4 years ago
- Update languages/swift.py to work around apple/swift#55127 — committed to vergenzt/pre-commit by vergenzt 3 years ago
- Update languages/swift.py to work around apple/swift#55127 — committed to vergenzt/pre-commit by vergenzt 3 years ago
- Example CLI: Fix name and cleanup When working on the next features, we ran into a [known bug in Swift](https://github.com/apple/swift/issues/55127) that prevents the annotation `@main` from being us... — committed to Concordium/concordium-swift-sdk by bisgardo 3 years ago
- Example CLI: Fix name and cleanup When working on the next features, we ran into a [known bug in Swift](https://github.com/apple/swift/issues/55127) that prevents the annotation `@main` from being us... — committed to Concordium/concordium-swift-sdk by bisgardo 3 years ago
- Refactor: Example CLI naming fix and various cleanup (#22) # Example CLI renaming When working on the next features, we ran into a [known bug in Swift](https://github.com/apple/swift/issues/55127)... — committed to Concordium/concordium-swift-sdk by bisgardo 3 years ago
Comment by Eneko Alonso (JIRA)
This seems to still be an issue for new Swift 5.5 packages containing a main.swift file.
Steps to reproduce:
{{
@main
struct Program {
static func main() {
print(“Hello, world!”)
}
}
}}
4. swift run
5. Observe error: main.swift:2:1: error: ‘main’ attribute cannot be used in a module that contains top-level code
Multiple files
Adding a second file does not seem to fix the issue:
{{
func hello() {
print(“Hello, world!”)
}
}}
2. Update `main.swift` as follows:
{{
@main
struct Program {
static func main() {
hello()
}
}
}}
3. swift run
4. Observe error: main.swift:2:1: error: ‘main’ attribute cannot be used in a module that contains top-level code
Solution
Renaming `main.swift` to something else (eg. `Program.swift`) solves the issue, whether there are only one file or more.
{{
func hello() {
print(“Hello, world!”)
}
@main
struct Program {
static func main() {
hello()
}
}
}}
3. swift run
4. Observer the output “Hello, world!”
This issue seems very old but I’d like to chime in that it doesn’t seem fixed at all in Swift 5.9. See https://github.com/apple/swift-getting-started-cli/issues/3 - I hit the bug midway through the “getting started” tutorial for a command line program.
I initially had the same problem when following the CLI tutorial, but the issue went away when I added
path: "Sources"to the MyCLI target in Package.swift. I didn’t use-parse-as-library.This is with Swift 6.1.2 on Windows 11 24H2
Ran into the same issue in the same tutorial as @ayaen , Swift 5.10.1 on WIndows. This worked but it was
-Xswiftcwith a lowercases.Trying to use this example for using raw nio:https://theswiftdev.com/swiftnio-tutorial-the-echo-server/ its only from 3 months ago, but I the newest info I’ve found. seems the renaming worked for me was well.