Skip to content

Pass the same set of object files when retrying linking for cgo#23447

Open
skirino wants to merge 1 commit into
pantsbuild:mainfrom
skirino:avoid_linker_error_in_cgo
Open

Pass the same set of object files when retrying linking for cgo#23447
skirino wants to merge 1 commit into
pantsbuild:mainfrom
skirino:avoid_linker_error_in_cgo

Conversation

@skirino

@skirino skirino commented Jun 26, 2026

Copy link
Copy Markdown

With the latest main branch, pants test src/python/pants/backend/go/util_rules/cgo_test.py:tests fails in my environment:

============================= test session starts ==============================
collecting ... collected 5 items

src/python/pants/backend/go/util_rules/cgo_test.py::test_cgo_compile PASSED [ 20%]
src/python/pants/backend/go/util_rules/cgo_test.py::test_cgo_with_cxx_source FAILED [ 40%]
src/python/pants/backend/go/util_rules/cgo_test.py::test_cgo_with_objc_source FAILED [ 60%]
src/python/pants/backend/go/util_rules/cgo_test.py::test_cgo_with_fortran_source FAILED [ 80%]
src/python/pants/backend/go/util_rules/cgo_test.py::test_cgo_with_embedded_static_library PASSED [100%]

=================================== FAILURES ===================================

The 3 tests fail with the same linker error:

ValueError: cgo binary link failed:
stdout:

stderr:
Undefined symbols for architecture arm64:
  "_main", referenced from:
      <initial-undefines>
ld: symbol(s) not found for architecture arm64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

(found this issue during playing around the tests in #23439)

In cgo.py, linking of object files are retried with additional linker options:
https://github.com/pantsbuild/pants/blob/main/src/python/pants/backend/go/util_rules/cgo.py#L500-L559
For relatively simple case, linking succeeds at the 1st try. In the case of ObjC, C++ and Fortran tests, linking fails (due to lack of the language's stdlib?) and retried. _main (cgo-generated dummy function) resides in _cgo_main.o file and is required by the linker, but the object file is not included in the linker flags at the 2nd try.

My environment:

  • OS: macOS 15.7.7
  • gcc: clang-1700.6.4.2
  • python: 3.14.6
  • go: 1.26.2

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant