bazelbuild / bazelbuild/rules_swift

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

Open
#1,546 2 comments 2 reactions 0 assignees View on GitHub
Dominant language
Starlark
Stars
353
Forks
172
Avg merge
23h 22m
Merged PRs (30d)
14

Description

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.
```

Contributor guide

Open the contributing guide

Research direction

Reproduce the failure using MODULE.bazel, BUILD, MyApp.swift, and MyAppTests.swift with rules_swift 3.0.2, comparing it with 2.8.2 or 2.9.0. Start from the duplicate _main linker output and the generated MyAppTests_test_discovery_srcs/main.swift.o entry. Done means the test target links and passes without the duplicate symbol.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
build-system, testing
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.