bazel-contrib / bazel-contrib/rules_go

Cannot compile an application with cgo zmq

Open
#4,443 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Go
Stars
1.5k
Forks
762
Avg merge
1d 11h
Merged PRs (30d)
12

Description

Hi everyone,

I am trying to compile the following application using the https://github.com/pebbe/zmq4 (draft).

The application code is this:

```go
// Source: https://zeromq.org/get-started/?language=go#
package main

import (
"fmt"

zmq "github.com/pebbe/zmq4/draft"
)

func main() {
zctx, _ := zmq.NewContext()

// Socket to talk to server
fmt.Printf("Connecting to the server...\n")
s, _ := zctx.NewSocket(zmq.REQ)
s.Connect("tcp://localhost:5555")

// Do 10 requests, waiting each time for a response
for i := 0; i < 10; i++ {
fmt.Printf("Sending request %d...\n", i)
s.Send("Hello", 0)

msg, _ := s.Recv(0)
fmt.Printf("Received reply %d [ %s ]\n", i, msg)
}
}
```

The MODULE.bazel file:

```bazel
module(
name = "bazel-cgo",
version = "0.0.1",
)

###############################################################################
# Bazel dependencies
bazel_dep(name = "rules_cc", version = "0.0.13")
bazel_dep(name = "libzmq", version = "4.3.5.bcr.3")
bazel_dep(name = "gazelle", version = "0.39.0")
bazel_dep(name = "rules_go", version = "0.50.1")

###############################################################################
# Go extensions

# Go SDK
go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")

# Download an SDK for the host OS & architecture as well as common remote execution platforms.
go_sdk.download(version = "1.23.1")

# Go dependencies (centralized across all Go modules in this Git repo)
go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")

# All *direct* Go dependencies of all `go.mod` files referenced by the `go.work` file have to be listed explicitly.
use_repo(
go_deps,
"com_github_pebbe_zmq4",
)

single_version_override(module_name="libzmq", version = "4.3.5.bcr.3", patches = ["//:libzmq.patch"], patch_strip = 1)

inject_repo(go_deps, "libzmq")

go_deps.module_override(
patch_strip = 1,
patches = [
"//:zmq.patch",
],
path = "github.com/pebbe/zmq4",
)
```

And the zmq.patch

```patch
--- libzmq+/BUILD.bazel 2025-08-08 17:05:56.283980669 +0200
+++ libzmq+/BUILD.bazel 2025-08-12 13:53:33.544509621 +0200
@@ -20,9 +20,10 @@
"ZMQ_BUILD_TESTS": "OFF",
# Gnu TLS is not available in BCR.
"WITH_TLS": "OFF",
+ "ENABLE_DRAFTS": "ON", "ZMQ_BUILD_DRAFT_API": "ON"
},
copts = ["-Wno-error=missing-braces"],
- env = {"CMAKE_BUILD_TYPE": "Release"},
+ env = {"CMAKE_BUILD_TYPE": "Release", "ENABLE_DRAFTS": "ON", "ZMQ_BUILD_DRAFT_API": "ON"},
lib_source = ":srcs",
out_include_dir = "include",
out_static_libs = ["libzmq.a"],
```

The build file:

```bazel
load("@gazelle//:def.bzl", "gazelle")
load("@rules_go//go:def.bzl", "go_binary", "go_library")

gazelle(name = "gazelle")

go_binary(
name = "zmq_client",
srcs = ["zmq_client.go"],
# cdeps = [
# "@libzmq//:libzmq",
# ],
# cgo = True,
deps = [
# Important note, this library is not hermetic.
"@com_github_pebbe_zmq4//draft",
],
)
```

and the error I get:

```
pouyan@AAAAAA:~/Projects/bazel-cgo$ bazel build //:zmq_client --verbose_failures
INFO: Analyzed target //:zmq_client (101 packages loaded, 22097 targets configured).
ERROR: /home/pouyan/.cache/bazel/_bazel_pouyan/f8de22c6584dfaec9ace9c0897503f54/external/gazelle++go_deps+com_github_pebbe_zmq4/draft/BUILD.bazel:3:11: GoCompilePkg external/gazelle++go_deps+com_github_pebbe_zmq4/draft/draft.a failed: (Exit 1): builder failed: error executing GoCompilePkg command (from target @@gazelle++go_deps+com_github_pebbe_zmq4//draft:draft)
(cd /home/pouyan/.cache/bazel/_bazel_pouyan/f8de22c6584dfaec9ace9c0897503f54/sandbox/linux-sandbox/6/execroot/_main && \
exec env - \
CC=/usr/bin/gcc \
CGO_ENABLED=1 \
GOARCH=amd64 \
GODEBUG='winsymlink=0' \
GOEXPERIMENT=nocoverageredesign \
GOOS=linux \
GOPATH='' \
GOROOT=bazel-out/k8-fastbuild/bin/external/rules_go+/stdlib_ \
GOROOT_FINAL=GOROOT \
GOTOOLCHAIN=local \
PATH=/usr/bin:/bin \
ZERO_AR_DATE=1 \
bazel-out/k8-opt-exec-ST-d57f47055a04/bin/external/rules_go++go_sdk+bazel-cgo__download_0/builder_reset/builder compilepkg -sdk external/rules_go++go_sdk+bazel-cgo__download_0 -goroot bazel-out/k8-fastbuild/bin/external/rules_go+/stdlib_ -installsuffix linux_amd64 -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/auth.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/ctxoptions_unix.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/ctxoptions_windows.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/doc.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/errors.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/polling.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/reactor.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/socketget.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/socketget_unix.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/socketget_windows.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/socketset.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/utils.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/wrappers_unix.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/wrappers_windows.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq4.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq42draft.go -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/dummy.c -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq4.h -src external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq42draft.h -embedroot '' -embedroot bazel-out/k8-fastbuild/bin -embedlookupdir external/gazelle++go_deps+com_github_pebbe_zmq4/draft -importpath github.com/pebbe/zmq4/draft -p github.com/pebbe/zmq4/draft -package_list bazel-out/k8-opt-exec-ST-d57f47055a04/bin/external/rules_go++go_sdk+bazel-cgo__download_0/packages.txt -lo bazel-out/k8-fastbuild/bin/external/gazelle++go_deps+com_github_pebbe_zmq4/draft/draft.a -o bazel-out/k8-fastbuild/bin/external/gazelle++go_deps+com_github_pebbe_zmq4/draft/draft.x -gcflags '' -asmflags '' -cppflags '-I external/gazelle++go_deps+com_github_pebbe_zmq4/draft -iquote . -isystem bazel-out/k8-fastbuild/bin/external/libzmq+/libzmq/include' -cflags '-U_FORTIFY_SOURCE -fstack-protector -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fPIC' -cxxflags '-U_FORTIFY_SOURCE -fstack-protector -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++17 -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fPIC' -objcflags '-U_FORTIFY_SOURCE -fstack-protector -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fPIC' -objcxxflags '-U_FORTIFY_SOURCE -fstack-protector -Wunused-but-set-parameter -Wno-free-nonheap-object -fno-omit-frame-pointer -std=c++17 -fno-canonical-system-headers -Wno-builtin-macro-redefined -D__DATE__="redacted" -D__TIMESTAMP__="redacted" -D__TIME__="redacted" -fPIC' -ldflags 'bazel-out/k8-fastbuild/bin/external/libzmq+/libzmq/lib/libzmq.a -fuse-ld=gold -B/usr/bin -Wl,-no-as-needed -Wl,-z,relro,-z,now -pass-exit-codes -Wl,--push-state,-as-needed -lstdc++ -Wl,--pop-state -Wl,--push-state,-as-needed -lm -Wl,--pop-state')
# Configuration: e8494724f8baac23aca058d3505bda14b53b5306128c401b729b89c7aeb7b13c
# Execution platform: @@platforms//host:host

Use --sandbox_debug to see verbose messages from the sandbox and retain the sandbox build root for debugging
_main/external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq4.go:1165:34: could not determine kind of name for C.zmq_msg_group
_main/external/gazelle++go_deps+com_github_pebbe_zmq4/draft/zmq4.go:1160:34: could not determine kind of name for C.zmq_msg_routing_id
compilepkg: error running subcommand external/rules_go++go_sdk+bazel-cgo__download_0/pkg/tool/linux_amd64/cgo: exit status 2
Target //:zmq_client failed to build
INFO: Elapsed time: 2.525s, Critical Path: 0.78s
INFO: 2 processes: 13 action cache hit, 2 internal.
ERROR: Build did NOT complete successfully
pouyan@AAAAA:~/Projects/bazel-cgo$
```

The go build of the same file works without issue, I checked the zmq repo and they said building with the ENABLE_DRAFTS should be ok. When I check the binary file (libzmq.a) also I so the signatures for the `C.zmq_msg_group`. Is this a bug in the compilepkg? thanks for the help

Best regards,
Pouyan

Contributor guide

Open the contributing guide

Research direction

Reproduce `bazel build //:zmq_client --verbose_failures` using the shown MODULE.bazel, BUILD file, and `zmq.patch` configuration. Inspect the draft headers and the rules_go cgo compile step around `C.zmq_msg_group` and `C.zmq_msg_routing_id`, then verify that the same application builds successfully under Bazel as it does with `go build`.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.