rules_swift icon indicating copy to clipboard operation
rules_swift copied to clipboard

[3.0.2] `duplicate symbol '_main'` error in tests

Open jpsim opened this issue 6 months ago • 2 comments

This seems to be a regression in 3.x. This used to work in rules_swift 2.x but fails with rules_swift 3.x:

MODULE.bazel

module(
    name = "duplicate_symbol_repro",
    version = "1.0.0",
)

bazel_dep(name = "rules_swift", version = "3.0.2")

BUILD

load("@rules_swift//swift:swift.bzl", "swift_binary", "swift_library", "swift_test")

swift_library(
    name = "mylib",
    srcs = ["MyApp.swift"],
    module_name = "MyApp",
)

swift_binary(
    name = "myapp",
    deps = ["mylib"],
)

swift_test(
    name = "MyAppTests",
    srcs = ["MyAppTests.swift"],
    deps = ["mylib"],
)

MyApp.swift

@main
struct MyApp {
    static func main() {
        print("Hello from MyApp!")
    }
    
    static func businessLogic() -> String {
        return "some business logic"
    }
}

MyAppTests.swift

import XCTest
@testable import MyApp

final class MyAppTests: XCTestCase {
    func testBusinessLogic() {
        XCTAssertEqual(MyApp.businessLogic(), "some business logic")
    }
}

With rules_swift 2.8.2 / 2.9.0:

$ bazel clean --expunge && bazel test //:MyAppTests
//:MyAppTests                                                            PASSED in 0.4s

With rules_swift 3.0.2:

$ bazel clean --expunge && bazel test //:MyAppTests
ERROR: /private/tmp/duplicate_symbol_repro/BUILD:14:11: Linking MyAppTests.xctest/Contents/MacOS/MyAppTests failed: (Exit 1): cc_wrapper.sh failed: error executing CppLink command (from target //:MyAppTests) bazel-out/darwin_arm64-opt-exec-ST-d57f47055a04/bin/external/apple_support+/crosstool/cc_wrapper.sh @bazel-out/darwin_arm64-fastbuild/bin/MyAppTests.xctest/Contents/MacOS/MyAppTests-0.params

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
duplicate symbol '_main' in:
    bazel-out/darwin_arm64-fastbuild/bin/libmylib.a[2](MyApp.swift.o)
    /private/var/tmp/_bazel_jsimard/5cd4c90ce95638f680541d109ff0573d/execroot/_main/bazel-out/darwin_arm64-fastbuild/bin/MyAppTests__GeneratedTestDiscoveryRunner_objs/MyAppTests_test_discovery_srcs/main.swift.o
ld: 1 duplicate symbols
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Error in child process '/usr/bin/xcrun'. 1
Target //:MyAppTests failed to build
Use --verbose_failures to see the command lines of failed build steps.
INFO: Elapsed time: 25.972s, Critical Path: 13.50s
INFO: 59 processes: 22 internal, 29 darwin-sandbox, 8 worker.
ERROR: Build did NOT complete successfully
//:MyAppTests                                                   FAILED TO BUILD

Executed 0 out of 1 test: 1 fails to build.

jpsim avatar Jun 23 '25 20:06 jpsim

I can confirm the same is happening for me, in a similar setup, on the 3.1.2 release. A workaround with macos_unit_test seems to do the trick. Is this going to be solved on a rules_swift level or is it a permanent change?

sewerynplazuk avatar Sep 22 '25 09:09 sewerynplazuk

bisected to https://github.com/bazelbuild/rules_swift/commit/9af31ccfeb6640112098b58a37dab874accdd4a3

keith avatar Sep 24 '25 14:09 keith