New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[SR-12683] @main
is no longer usable due to misdetection of top level code
#55127
Comments
@swift-ci create |
It looks like compilation is failing whenever the compiler's in single-file mode. Using the two attached source files, I get this result from trying to compile just $ swiftc MyMain.swift
MyMain.swift:3:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
MyMain.swift:1:1: note: top-level code defined in this source file
// @main attribute test
^ When I add $ swiftc MyMain.swift Empty.swift
(compiles without issue) |
To make this work, you can tell the compiler to parse the file as a library instead of in single-file mode with the $ swiftc -parse-as-library MyMain.swift |
Comment by Josh Wright (JIRA) Any updates to this? Hitting this issue as well when writing a single file script. Nate's workaround works, but would be great to have this work out of the box (was using it indirectly through https://github.com/mxcl/swift-sh so it wasn't apparent how to update the compiler flags). |
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:
{{ 4. swift run Multiple filesAdding a second file does not seem to fix the issue:
{{ 2. Update `main.swift` as follows: {{ 3. swift run SolutionRenaming `main.swift` to something else (eg. `Program.swift`) solves the issue, whether there are only one file or more.
{{ @main 3. swift run |
I can confirm the issue for this example where I import a cooperating module: // Swift version 5.6 (swift-5.6-RELEASE)
// Target: arm64-apple-macosx12.0
import Foundation
import SwiftUI
import HelloCore
@main
struct HelloApp: App {
var body: some Scene {
WindowGroup {
HelloView().onAppear {
print("loaded")
signal(SIGINT) { _ in
print("exit")
exit(EXIT_SUCCESS)
}
}
}
}
} The renaming solution works. However, you may then see
You can fix that by changing the |
Fix 'main' attribute cannot be used in a module that contains top-level code apple/swift#55127
Trying to use this example for using raw nio:https://theswiftdev.com/swiftnio-tutorial-the-echo-server/ |
Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async commands including one for retrieving PRs - Rename main.swift to work around swift ([SR-12683](apple/swift#55127)) Implement PR endpoint with PR state filtering and results limiting - Add Repo struct that maps to the GitHub repo and can be initialized with various forms of the GitHub URL details - Add PR endpoint and associated types to create and asynchronously fetch PR data from corresponding GitHub endpoint - Create types for resulting PR model objects - Add tests for Repo initialization and fetching PRs using endpoint Update VS Code project with: - Fix new environment builds by creating package resolve task that build depends on - Move GITHUB_TOKEN environment variable to env file used by debug launch task - TODO: Remove postCreateCommand from devcontainer.json so container can be used by VS Code (and doesn't just call swift --version) - Swift extension incorrectly expects `cwd` being set in tasks but since it can't be hardcoded or use the `workspaceFolder` variable, set it to an empty string Address Linux build issues: - Import new FoundationNetworking library yon Linux - Implement asynchronous version of `URLSession.dataTask()` on Linux where async `URLSession.data()` Follow up tasks: - TODO: Refactor top-level GitHubRepo object and delete associated GitHubStatsCore tests
Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async commands including one for retrieving PRs - Rename main.swift to work around swift ([SR-12683](apple/swift#55127)) Implement PR endpoint with PR state filtering and results limiting - Add Repo struct that maps to the GitHub repo and can be initialized with various forms of the GitHub URL details - Add PR endpoint and associated types to create and asynchronously fetch PR data from corresponding GitHub endpoint - Create types for resulting PR model objects - Add tests for Repo initialization and fetching PRs using endpoint Update VS Code project with: - Fix new environment builds by creating package resolve task that build depends on - Move GITHUB_TOKEN environment variable to env file used by debug launch task - TODO: Remove postCreateCommand from devcontainer.json so container can be used by VS Code (and doesn't just call swift --version) - Swift extension incorrectly expects `cwd` being set in tasks but since it can't be hardcoded or use the `workspaceFolder` variable, set it to an empty string Address Linux build issues: - Import new FoundationNetworking library on Linux - Implement asynchronous version of `URLSession.dataTask()` on Linux where async `URLSession.data()` Follow up tasks: - TODO: Refactor top-level GitHubRepo object and delete associated GitHubStatsCore tests
Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async commands including one for retrieving PRs - Rename main.swift to work around swift ([SR-12683](apple/swift#55127)) Implement PR endpoint with PR state filtering and results limiting - Add Repo struct that maps to the GitHub repo and can be initialized with various forms of the GitHub URL details - Add PR endpoint and associated types to create and asynchronously fetch PR data from corresponding GitHub endpoint - Create types for resulting PR model objects - Add tests for Repo initialization and fetching PRs using endpoint Update VS Code project with: - Fix new environment builds by creating package resolve task that build depends on - Move GITHUB_TOKEN environment variable to env file used by debug launch task - Remove postCreateCommand from devcontainer.json so container can be used by VS Code (and not just call swift CLI) - Swift extension incorrectly expects `cwd` being set in tasks but since it can't be hardcoded or use the `workspaceFolder` variable, set it to an empty string Address Linux build issues: - Import new FoundationNetworking library on Linux - Implement asynchronous version of `URLSession.dataTask()` on Linux where async `URLSession.data()` Follow up tasks: - TODO: Refactor top-level GitHubRepo object and delete associated GitHubStatsCore tests
Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async commands including one for retrieving PRs - Rename main.swift to work around swift ([SR-12683](apple/swift#55127)) Implement PR endpoint with PR state filtering and results limiting - Add Repo struct that maps to the GitHub repo and can be initialized with various forms of the GitHub URL details - Add PR endpoint and associated types to create and asynchronously fetch PR data from corresponding GitHub endpoint - Create types for resulting PR model objects - Add tests for Repo initialization and fetching PRs using endpoint Update VS Code project with: - Fix new environment builds by creating package resolve task that build depends on - Move GITHUB_TOKEN environment variable to env file used by debug launch task - Remove postCreateCommand from devcontainer.json so container can be used by VS Code (and not just call swift CLI) - Swift extension incorrectly expects `cwd` being set in tasks but since it can't be hardcoded or use the `workspaceFolder` variable, set it to an empty string Address Linux build issues: - Import new FoundationNetworking library on Linux - Implement asynchronous version of `URLSession.dataTask()` on Linux where async `URLSession.data()` Follow up tasks: - TODO: Refactor top-level GitHubRepo object and delete associated GitHubStatsCore tests
Add argument parsing using ArgumentParser Swift package - Create options for GitHub URLs or repo and organization (including validation) - Setup Async commands including one for retrieving PRs - Rename main.swift to work around swift ([SR-12683](apple/swift#55127)) Implement PR endpoint with PR state filtering and results limiting - Add Repo struct that maps to the GitHub repo and can be initialized with various forms of the GitHub URL details - Add PR endpoint and associated types to create and asynchronously fetch PR data from corresponding GitHub endpoint - Create types for resulting PR model objects - Add tests for Repo initialization and fetching PRs using endpoint Update VS Code project with: - Fix new environment builds by creating package resolve task that build depends on - Move GITHUB_TOKEN environment variable to env file used by debug launch task - Remove postCreateCommand from devcontainer.json so container can be used by VS Code (and not just call swift CLI) - Swift extension incorrectly expects `cwd` being set in tasks but since it can't be hardcoded or use the `workspaceFolder` variable, set it to an empty string Address Linux build issues: - Import new FoundationNetworking library on Linux - Implement asynchronous version of `URLSession.dataTask()` on Linux where async `URLSession.data()` Follow up tasks: - TODO: Refactor top-level GitHubRepo object and delete associated GitHubStatsCore tests
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 apple/swift-getting-started-cli#3 - I hit the bug midway through the "getting started" tutorial for a command line program. |
Studying Apple's tutorial Getting Started with ArgumentParser, perhaps this is expected behaviour. See:
|
That was my conclusion as well, but then you get a different error if you don't have any main.swift files:
@ubunatic had the actual fix in his comment above: That seems like a different issue. First, the tutorial should be updated. Second, the error could probably say something that would help direct the person to using |
This is frustrating for someone writing tutorials about Swift development using an asynchronous API for both Apple platforms and Linux, because:
It would be nice if whoever needs to fix this would just do it. I can't imagine it being that hard to fix... |
When working on the next features, we ran into a [known bug in Swift](apple/swift#55127) that prevents the annotation `@main` from being usable from within a file named `main.swift`. According to a [note in the docs of `ArgumentParser`](https://apple.github.io/swift-argument-parser/documentation/argumentparser/gettingstarted/) (and apparently not documented anywhere else), this shouldn't work at all: > The Swift compiler uses either the type marked with @main or a main.swift file as the entry point for an executable program. You can use either one, but not both [...] So it's a bit of a mystery why it works in the current version... To fix the problem we rename `main.swift` to `CLI.swift`. Also, the executable is renamed to `concordium-example-client` to ensure that it never is mistaken for a production ready tool. Finally, some variables were renamed for consistency and the readme got an overhaul.
When working on the next features, we ran into a [known bug in Swift](apple/swift#55127) that prevents the annotation `@main` from being usable from within a file named `main.swift`. According to a [note in the docs of `ArgumentParser`](https://apple.github.io/swift-argument-parser/documentation/argumentparser/gettingstarted/) (and apparently not documented anywhere else), this shouldn't work at all: > The Swift compiler uses either the type marked with @main or a main.swift file as the entry point for an executable program. You can use either one, but not both [...] So it's a bit of a mystery why it works in the current version... To fix the problem we rename `main.swift` to `CLI.swift`. Also, the executable is renamed to `concordium-example-client` to ensure that it never is mistaken for a production ready tool. Finally, some variables were renamed for consistency and the readme got an overhaul.
# Example CLI renaming When working on the next features, we ran into a [known bug in Swift](apple/swift#55127) that prevents the annotation `@main` from being usable from within a file named `main.swift`. According to a [note in the docs of `ArgumentParser`](https://apple.github.io/swift-argument-parser/documentation/argumentparser/gettingstarted/) (and apparently not documented anywhere else), this shouldn't work at all: > The Swift compiler uses either the type marked with @main or a main.swift file as the entry point for an executable program. You can use either one, but not both [...] So it's a bit of a mystery why it works in the current version, but not once more features are added... To fix the problem we rename `main.swift` to `commands.swift`. It also turns out that by upgrading to `swift-tools-version: 5.9`, we can abbreviate the setup to omit the "executable product" declaration and the intermediary directory inside `Sources`. Also, the executable is renamed to `concordium-example-client` to ensure that it never is mistaken for a production ready tool. Finally, some variables are renamed for consistency and the readme got an overhaul. # Vaious cleanup - Some types and variables that aren't named according to the official [API Design Guidelines](https://www.swift.org/documentation/api-design-guidelines/) but were missed in #21 have been renamed appropriately. - New type aliases for identity parameters are added. - The intermediate type `VerifyKeyGrpc` is replaced with an extension `fromGRPCType` on `VerifyKey` from `ConcordiumWalletCrypto`. - The functions the convert values from the generated gRPC types have been tightened up to not accept `nil` values and ensure that optional values are explicitly handled at the right place.
Attachment: Download
Additional Detail from JIRA
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`.
The text was updated successfully, but these errors were encountered: