swiftlang / swift-package-manager

The Package Manager for the Swift Programming Language
Apache License 2.0
9.66k stars 1.32k forks source link

Duplicate `main` symbol for `swift test` on Windows #6367

Open remko opened 1 year ago

remko commented 1 year ago

Description

I have a package with an executable target, and a test target. The tests in the test target do a @testable import of the executable target. The main target has a @main struct ....

Running swift test works on macOS and Linux. Running swift test on Windows fails.

Expected behavior

When running swift test on Windows, the tests run.

Actual behavior

When running swift test on windows, it fails with an error at link time:

lld-link: error: duplicate symbol: main
>>> defined at C:\\...\\MyPackageTests.build\\runner.swift.o
>>> defined at C:\\...\\MyPackage\\MyMainStruct.swift.o

Steps to reproduce

No response

Swift Package Manager version/commit hash

version included in the official Swift download for Windows

Swift & OS version (output of swift --version && uname -a)

Swift: 5.8 OS: Microsoft Windows Server 2022

(also happens on other versions of swift and OS).

neonichu commented 1 year ago

Sounds as if -entry-point-function-name is maybe not working correctly on Windows? We could disable the feature via BuildParameters.canRenameEntrypointFunctionName if it doesn't work there.

cc @compnerd

compnerd commented 1 year ago

@neonichu that definitely works: https://github.com/compnerd/swift-win32-application/blob/main/Package.swift even has an example. The problem is that you cannot alias functions using the linker on Windows and this needs to be done in SPM.

compnerd commented 1 year ago

https://github.com/apple/swift-package-manager/blob/main/Sources/Build/BuildPlan.swift#L138-L151

compnerd commented 7 months ago

I think that I have a solution to this that is cross-platform as long as we are reliant on LLVM! The symbol rewriter should do this properly on all file formats!

dabrahams commented 6 months ago

Oof; I should have searched for this before creating the minimal reproducer. But there it is in case it helps! Be sure you get the spm-bug-windows-test-executable-dependency tag.

grynspan commented 2 months ago

@MaxDesiatov Do you think this issue would be resolved now with your cross-compilation macros work?

compnerd commented 2 months ago

I'm still seeing it recently with swift-foundation at least.