Skip to content

Conversation

@jsharpe
Copy link
Member

@jsharpe jsharpe commented Nov 28, 2025

When a go_library embeds another go_library that has cdeps but not cgo=True (because it itself embeds a library with cgo=True), the cdeps were not being propagated. This is a regression from v0.51.0.

In v0.50.1, cdeps and related cgo options were merged unconditionally:
source["cdeps"] = source["cdeps"] or s.cdeps

In v0.51.0 (PR #4030), the logic was changed to only merge these when s.cgo is True:
if s.cgo:
source["cdeps"] = s.cdeps

This breaks the pattern where:

  • Library A has cgo=True and C code
  • Library B embeds A, adds cdeps, but doesn't set cgo=True
  • Test embeds B

The cdeps from B don't propagate to the test because B doesn't have cgo=True, causing undefined symbol errors during linking.

This fix restores the v0.50.1 behavior: merge cgo-related attributes if the embedded library has them, regardless of the cgo flag.

cgo=True (because it itself embeds a library with cgo=True), the cdeps
were not being propagated. This is a regression from v0.51.0.

In v0.50.1, cdeps and related cgo options were merged unconditionally:
  source["cdeps"] = source["cdeps"] or s.cdeps

In v0.51.0 (PR bazel-contrib#4030), the logic was changed to only merge these when
s.cgo is True:
  if s.cgo:
      source["cdeps"] = s.cdeps

This breaks the pattern where:
- Library A has cgo=True and C code
- Library B embeds A, adds cdeps, but doesn't set cgo=True
- Test embeds B

The cdeps from B don't propagate to the test because B doesn't have
cgo=True, causing undefined symbol errors during linking.

This fix restores the v0.50.1 behavior: merge cgo-related attributes
if the embedded library has them, regardless of the cgo flag.
Copy link
Member

@fmeum fmeum left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks good. Would you be willing to add a test with the described setup?

@jsharpe
Copy link
Member Author

jsharpe commented Nov 28, 2025

Yes, I have a simple reproducer for the issue so I can turn that into a test.

@dzbarsky
Copy link
Contributor

Thanks! Hadn't realized this was a valid scenario, adding a test would be great

@jsharpe
Copy link
Member Author

jsharpe commented Nov 28, 2025

@fmeum I've added the test

@jsharpe
Copy link
Member Author

jsharpe commented Nov 28, 2025

Sorry will fix it in CI - I can't build locally due to macOS SDK versions needed to be installed so hadn't actually tested the compile!

@jsharpe jsharpe force-pushed the cgo_fix branch 2 times, most recently from e8938f0 to aec9719 Compare November 28, 2025 14:12
This test verifies that cdeps are properly propagated when a go_library
with cdeps embeds another go_library with cgo=True, and a go_test embeds
the library with cdeps. This pattern was broken in v0.51.0 and fixed in
commit a494a68.

The test reuses existing native_dep cc_library to minimize test
complexity and follows the same pattern as the external reproducer.
@jsharpe
Copy link
Member Author

jsharpe commented Nov 28, 2025

@fmeum any ideas how to fix the windows build? Seems to be failing to find the msys gcc.

@fmeum
Copy link
Member

fmeum commented Nov 28, 2025

I don't understand why this fails - @jayconrod do you? We exclude some cgo tests on Windows, but similar ones are run and pass.

@jayconrod
Copy link
Collaborator

Sorry, I didn't have much time today to dig into this. But I was able to reproduce the error on my Windows machine. It, er, doesn't look like we print errors from failing C compiler commands. Someone should fix that at some point 😬 But with a quick hack, here's the error output. Does this help?

ERROR: C:/users/jay/code/rules_go/tests/core/cgo/BUILD.bazel:565:11: GoCompilePkg tests/core/cgo/cdeps_embed_base.a failed: (Exit 1): builder.exe failed: error executing GoCompilePkg command (from target //tests/core/cgo:cdeps_embed_base) bazel-out\x64_windows-opt-exec-ST-7daeda47919b\bin\external\go_sdk\builder_reset\builder.exe compilepkg -sdk external/go_sdk -goroot bazel-out/x64_windows-fastbuild/bin/stdlib_ -installsuffix ... (remaining 39 arguments skipped)
compilepkg: WORK=C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554
CC=c:/msys64/mingw64/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG=winsymlink=0 \
GOEXPERIMENT= \
GOOS=windows \
GOPATH= \
GOROOT=bazel-out/x64_windows-fastbuild/bin/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=c:/msys64/mingw64/bin \
SYSTEMDRIVE=C: \
SYSTEMROOT=C:\WINDOWS \
TEMP=C:\Users\jay\AppData\Local\Temp \
TMP=C:\Users\jay\AppData\Local\Temp \
external\go_sdk\pkg\tool\windows_amd64\cgo.exe -srcdir C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go\tests\core\cgo -objdir C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed -trimpath C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go;C:\users\jay\_bazel_jay\ajtxh6lj\execroot=>.. -ldflags @C:\Users\jay\AppData\Local\Temp\cgo-ldflags-174498093.txt -importpath github.com/bazelbuild/rules_go/tests/core/cgo/cdeps_embed -- -I tests/core/cgo -iquote . -iquote C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go\tests\core\cgo -iquote C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed -fPIC cdeps_embed_base.go
CC=c:/msys64/mingw64/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG=winsymlink=0 \
GOEXPERIMENT= \
GOOS=windows \
GOPATH= \
GOROOT=bazel-out/x64_windows-fastbuild/bin/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=c:/msys64/mingw64/bin \
SYSTEMDRIVE=C: \
SYSTEMROOT=C:\WINDOWS \
TEMP=C:\Users\jay\AppData\Local\Temp \
TMP=C:\Users\jay\AppData\Local\Temp \
c:/msys64/mingw64/bin/gcc -I tests/core/cgo -iquote . -iquote C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go\tests\core\cgo -iquote C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed -fPIC -fdebug-prefix-map=C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go=. -fdebug-prefix-map=C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed=. -mthreads -c C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_export.c -o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x0.o
CC=c:/msys64/mingw64/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG=winsymlink=0 \
GOEXPERIMENT= \
GOOS=windows \
GOPATH= \
GOROOT=bazel-out/x64_windows-fastbuild/bin/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=c:/msys64/mingw64/bin \
SYSTEMDRIVE=C: \
SYSTEMROOT=C:\WINDOWS \
TEMP=C:\Users\jay\AppData\Local\Temp \
TMP=C:\Users\jay\AppData\Local\Temp \
c:/msys64/mingw64/bin/gcc -I tests/core/cgo -iquote . -iquote C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go\tests\core\cgo -iquote C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed -fPIC -fdebug-prefix-map=C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go=. -fdebug-prefix-map=C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed=. -mthreads -c C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\cdeps_embed_base.cgo2.c -o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x1.o
CC=c:/msys64/mingw64/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG=winsymlink=0 \
GOEXPERIMENT= \
GOOS=windows \
GOPATH= \
GOROOT=bazel-out/x64_windows-fastbuild/bin/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=c:/msys64/mingw64/bin \
SYSTEMDRIVE=C: \
SYSTEMROOT=C:\WINDOWS \
TEMP=C:\Users\jay\AppData\Local\Temp \
TMP=C:\Users\jay\AppData\Local\Temp \
c:/msys64/mingw64/bin/gcc -I tests/core/cgo -iquote . -iquote C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go\tests\core\cgo -iquote C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed -fPIC -fdebug-prefix-map=C:\users\jay\_bazel_jay\ajtxh6lj\execroot\io_bazel_rules_go=. -fdebug-prefix-map=C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed=. -mthreads -c C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_main.c -o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_main.o
CC=c:/msys64/mingw64/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG=winsymlink=0 \
GOEXPERIMENT= \
GOOS=windows \
GOPATH= \
GOROOT=bazel-out/x64_windows-fastbuild/bin/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=c:/msys64/mingw64/bin \
SYSTEMDRIVE=C: \
SYSTEMROOT=C:\WINDOWS \
TEMP=C:\Users\jay\AppData\Local\Temp \
TMP=C:\Users\jay\AppData\Local\Temp \
c:/msys64/mingw64/bin/gcc -o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_main.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x0.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x1.o -mthreads
compilepkg: error running subcommand c:/msys64/mingw64/bin/gcc c:/msys64/mingw64/bin/gcc -o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_cgo_main.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x0.o C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x1.o -mthreads: exit status 1
c:/msys64/mingw64/bin/../lib/gcc/x86_64-w64-mingw32/13.2.0/../../../../x86_64-w64-mingw32/bin/ld.exe: C:\Users\jay\AppData\Local\Temp\rules_go_work-1620827554\cgo\github.com\bazelbuild\rules_go\tests\core\cgo\cdeps_embed\_x1.o:cdeps_embed_base.cgo2.c:(.text+0x65): undefined reference to `native_greeting'
collect2.exe: error: ld returned 1 exit status
Target //tests/core/cgo:cdeps_embed_with_cdeps failed to build
Use --verbose_failures to see the command lines of failed build steps.

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.

4 participants