swiftlang / swift-package-manager

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

Tests fail to link on Windows when macros are present #7174

Open lynchsft opened 12 months ago

lynchsft commented 12 months ago

Description

Tests fail to link on Windows using swift 5.9.1-RELEASE when macros are present.

This is a trivial repo that reproduces the issue: https://github.com/lynchsft/swift-windows-macro-testing

The locus of this problem is on Windows platform, but I conclude that it indirectly negatively affects the entire Swift community. As macros proliferate through common libraries, all swift-on-windows tools will either have to abandon testing or wholly abandon windows as a platform. Neither of these outcomes speaks well of the "swift is a cross platform language" story.

Particularly, I'm asserting that this is a rapidly worsening problem because library authors that don't usually concern themselves with Windows are eagerly adopting macros feature. Soon, the libraries that Windows projects currently rely on will be un-ingestiable, effectively locking our dependences at the last-macro-free version. This in turn, is a security problem as code-currency (up-to-date dependencies) are a crucial topic for averting and responding to security threats.

@neonichu, @MaxDesiatov During construction of that ^^ trivial reproduction repo, I actually encountered a success case, where I WAS able to ingest a macro and use it on Windows. The linker error did not arise. Including this state in case its a useful resource to have.

Branch: success_case Repo: https://github.com/lynchsft/swift-windows-macro-testing

Expected behavior

The tests should link and run properly.

Actual behavior

The tests fail to link (compilation succeeds):

Building for debugging...
lld-link: error: duplicate symbol: main
>>> defined at C:\Users\lynchsft\swift-windows-macro-testing\.build\x86_64-unknown-windows-msvc\debug\PowerAssertPlugin.build\PowerAssertPlugin.swift.o
>>> defined at C:\Users\lynchsft\swift-windows-macro-testing\.build\x86_64-unknown-windows-msvc\debug\SwiftWindowsPackageTestingPackageTests.build\runner.swift.o
clang: error: linker command failed with exit code 1 (use -v to see invocation)
[238/239] Linking C:\Users\lynchsft\swift-windows-macro-testing\.build\x86_64-unknown-windows-msvc\debug\SwiftWindowsPackageTestingPackageTests.xctest
error: fatalError

Steps to reproduce

swift --test

Swift Package Manager version/commit hash

5.9.1-RELEASE

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

Swift version 5.9.1 (swift-5.9.1-RELEASE)
Target: x86_64-unknown-windows-msvc
MSYS_NT-10.0-22631 lynchal-z01 3.4.6.x86_64 2023-05-18 20:39 UTC x86_64 Msys
MaxDesiatov commented 11 months ago

Is this reproducible for you with latest development snapshots? (at the moment of writing it's https://download.swift.org/development/windows10/swift-DEVELOPMENT-SNAPSHOT-2023-11-27-a/swift-DEVELOPMENT-SNAPSHOT-2023-11-27-a-windows10.exe)

tishin commented 11 months ago

It is reproducible with the latest snapshot:

> swift --version
Swift version 5.11-dev (LLVM d50917983d84235, Swift 3f06b5efd0ebcfb)
Target: x86_64-unknown-windows-msvc

I have a slightly different example though: https://github.com/tishin/MacroTestIssue It has two targets - an empty macro implementation target with a @main attribute (which I believe causes this issue)

import SwiftCompilerPlugin
import SwiftSyntaxMacros

@main
struct MyMacroPlugin: CompilerPlugin {
    let providingMacros: [Macro.Type] = [
    ]
}

And a trivial test target:

import XCTest

final class MyMacroTests: XCTestCase {
    func testMacro() throws {
        XCTAssertTrue(true)
    }
}

Running swift test -v using the latest snapshot (as well as 5.9.2 release) fails with the duplication error:

lld-link: error: duplicate symbol: main
>>> defined at C:\Projects\MacroTestIssue\.build\x86_64-unknown-windows-msvc\debug\MyMacroMacros.build\MyMacroMacro.swift.o
>>> defined at C:\Projects\MacroTestIssue\.build\x86_64-unknown-windows-msvc\debug\MyMacroPackageTests.build\runner.swift.o
lynchsft commented 11 months ago

Thank you @tishin . @MaxDesiatov, @neonichu Are you able to provide a read on the situation? Have I concluded wrongly that as time goes on this bug will strand Windows projects in the past?