apple / swift-getting-started-cli

Getting started with Swift: CLI tool with SwiftPM
https://www.swift.org
Apache License 2.0
30 stars 5 forks source link

Following this tutorial generates a compile error on swift 5.9 #3

Open DeCarabas opened 8 months ago

DeCarabas commented 8 months ago

When I follow this tutorial, after entering the code in the step entitled "A small application", running swift run MyCLI gives me the following errors:

C:\src\misc\MyCLI>swift run MyCLI
Building for debugging...
error: emit-module command failed with exit code 1 (use -v to see invocation)
C:\src\misc\MyCLI\Sources\MyCLI.swift:3:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
C:\src\misc\MyCLI\Sources\MyCLI.swift:1:1: note: top-level code defined in this source file
import Figlet
^
C:\src\misc\MyCLI\Sources\MyCLI.swift:1:1: note: pass '-parse-as-library' to compiler invocation if this is intentional
import Figlet
^
C:\src\misc\MyCLI\Sources\MyCLI.swift:3:1: error: 'main' attribute cannot be used in a module that contains top-level code
@main
^
C:\src\misc\MyCLI\Sources\MyCLI.swift:1:1: note: top-level code defined in this source file
import Figlet
^
C:\src\misc\MyCLI\Sources\MyCLI.swift:1:1: note: pass '-parse-as-library' to compiler invocation if this is intentional
import Figlet
^
error: fatalError

Here's what my files and whatnot look like:

C:\src\misc\MyCLI>swift --version
Swift version 5.9.1 (swift-5.9.1-RELEASE)
Target: x86_64-unknown-windows-msvc

C:\src\misc\MyCLI>cat Package.swift
// swift-tools-version: 5.9
// The swift-tools-version declares the minimum version of Swift required to build this package.

import PackageDescription

let package = Package(
  name: "MyCLI",
  dependencies: [
    .package(url: "https://github.com/apple/example-package-figlet", branch:"main"),
  ],
  targets: [
    // Targets are the basic building blocks of a package, defining a module or a test suite.
    // Targets can depend on other targets in this package and products from dependencies.
    .executableTarget(
      name: "MyCLI",
      dependencies: [
        .product(name: "Figlet", package: "example-package-figlet"),
      ],
      path: "Sources"),
  ]
)

C:\src\misc\MyCLI>dir Sources
 Volume in drive C is Primary
 Volume Serial Number is 869D-696D

 Directory of C:\src\misc\MyCLI\Sources

11/19/2023  11:11 AM    <DIR>          .
11/19/2023  11:03 AM    <DIR>          ..
11/19/2023  11:04 AM               118 MyCLI.swift
               1 File(s)            118 bytes
               2 Dir(s)  251,722,711,040 bytes free

C:\src\misc\MyCLI>cat Sources\MyCLI.swift
import Figlet

@main
struct FigletTool {
    static func main() {
        Figlet.say("Hello, Swift!")
    }
}

This looks like it might be related to https://github.com/apple/swift/issues/55127; indeed, when I create an empty file next to MyCLI.swift the project runs correctly:

C:\src\misc\MyCLI>touch Sources\empty.swift

C:\src\misc\MyCLI>dir Sources
 Volume in drive C is Primary
 Volume Serial Number is 869D-696D

 Directory of C:\src\misc\MyCLI\Sources

11/19/2023  11:15 AM    <DIR>          .
11/19/2023  11:03 AM    <DIR>          ..
11/19/2023  11:15 AM                 0 empty.swift
11/19/2023  11:04 AM               118 MyCLI.swift
               2 File(s)            118 bytes
               2 Dir(s)  251,721,732,096 bytes free

C:\src\misc\MyCLI>swift run MyCLI
Building for debugging...
lld-link: warning: C:\src\misc\MyCLI\.build\x86_64-unknown-windows-msvc\debug\MyCLI.build\MyCLI.swift.o: locally defined symbol imported: $s6FigletAAO3sayyySSFZ (defined in C:\src\misc\MyCLI\.build\x86_64-unknown-windows-msvc\debug\Figlet.build\Figlet.swift.o) [LNK4217]
[5/5] Linking C:\src\misc\MyCLI\.build\x86_64-unknown-windows-msvc\debug\MyCLI.exe
Build complete! (1.30s)
  _   _          _   _                 ____               _    __   _     _
 | | | |   ___  | | | |   ___         / ___|  __      __ (_)  / _| | |_  | |
 | |_| |  / _ \ | | | |  / _ \        \___ \  \ \ /\ / / | | | |_  | __| | |
 |  _  | |  __/ | | | | | (_) |  _     ___) |  \ V  V /  | | |  _| | |_  |_|
 |_| |_|  \___| |_| |_|  \___/  ( )   |____/    \_/\_/   |_| |_|    \__| (_)
                                |/

Ideally the swift bug would be fixed, but it looks like it's been open since 2020, so at least the tutorial should be fixed to work around this problem?

0xTim commented 8 months ago

Yeah this is a definite Swift bug on Windows - I'll raise it with some people to see if this can get some eyes or whether it's worth adding a note about Windows