ITADN

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

#1546Openjpsim 创建于 2025-06-23
J
jpsimcommented
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` ```python module( name = "duplicate_symbol_repro", version = "1.0.0", ) bazel_dep(name = "rules_swift", version = "3.0.2") ``` `BUILD` ```python 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` ```swift @main struct MyApp { static func main() { print("Hello from MyApp!") } static func businessLogic() -> String { return "some business logic" } } ``` `MyAppTests.swift` ```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: ```console $ bazel clean --expunge && bazel test //:MyAppTests //:MyAppTests PASSED in 0.4s ``` With rules_swift 3.0.2: ```console $ 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. ```
2 条评论