apple / swift-argument-parser

Straightforward, type-safe argument parsing for Swift
Apache License 2.0
3.34k stars 320 forks source link

Code compiles but not with "-c release" argument #80

Closed fappelman closed 4 years ago

fappelman commented 4 years ago

I have completed an application that compiles and works. When I compile for release I get linker errors about invalid redeclaration.

ArgumentParser version: 0.0.2 Swift version: Apple Swift version 5.1.3 (swiftlang-1100.0.282.1 clang-1100.0.33.15) Target: x86_64-apple-darwin19.3.0

Checklist

Steps to Reproduce

The code needs to be spread over two files:

main.swift:

import Foundation
import ArgumentParser

struct MainCommand: ParsableCommand {
    static var configuration = CommandConfiguration(
        abstract: "An application to prove a bug",
        subcommands: [
            SubCommand.self
        ]
    )
}

extension MainCommand {
    struct GlobalOptions: ParsableCommand {
        @Flag(help: "Debug mode") var debug: Bool
    }
}

MainCommand.main()

File2.swift:

import Foundation
import ArgumentParser

extension MainCommand {
    struct SubCommand: ParsableCommand {
        static var configuration = CommandConfiguration(
            subcommands: [
                SubSubCommand1.self
            ]
        )

        struct SubSubCommand1: ParsableCommand {
            @OptionGroup()
            var globalOptions: MainCommand.GlobalOptions
        }
    }
}

This code compiles as expected using:

$ swift build
...
[65/65] Linking glr-bug-test

But then when the application is ready for distribution:

$ swift build -c release
<unknown>:0: error: invalid redeclaration of 'globalOptions'
<unknown>:0: note: 'globalOptions' previously declared here

The error seems to occur during linking.

Expected behavior

I expect that the code can be compiled with -c release without an error.

Actual behavior

The code does not compile. It provides an error which only shows when -c release is used and not in normal (debug) mode.

natecook1000 commented 4 years ago

Would you mind seeing if this is reproducible using a Swift 5.2 toolchain? This may be a resolved issue with the way CodingKeys are automatically generated by the compiler.

If you can’t upgrade to Swift 5.2, I believe some workarounds are to de-nest your command types or to manually define the CodingKeys enumerations for your nested command types.

fappelman commented 4 years ago

I can confirm that this is solved with Swift 5.2

natecook1000 commented 4 years ago

Glad to hear it! Given that, and the workarounds, this isn't something we'll try to address within the library.