Skip to content

bazel_gomock: _gomock_archive rule broken with rules_go 0.61 #303

@jmhodges

Description

@jmhodges

Actual behavior

Any Bazel gomock(...) target that mocks a compiled library (i.e. uses the _gomock_archive rule rather than _gomock_source) fails at analysis time when built with rules_go 0.61.x:

ERROR: .../BUILD.bazel:48:7: in _gomock_archive rule //go/svc/fe:mock_sess:
Traceback (most recent call last):
    File ".../bazel_gomock+/rules/gomock.bzl", line 251, column 24, in _gomock_archive_impl
        go = go_ctx.go.path,
Error: 'struct' value has no field or method 'go'
Available attributes: _ctx, actions, archive, binary, ..., sdk, stdlib, tool_args, toolchain

rules_go 0.61 removed the deprecated .go shortcut field from the go_context struct, but _gomock_archive_impl still reads go_ctx.go.path (bazel/rules/gomock.bzl, line 256 on main / line 251 in the v0.2.0 release):

https://github.com/uber-go/mock/blob/main/bazel/rules/gomock.bzl#L256

""".format(
    go = go_ctx.go.path,            # <-- removed in rules_go 0.61
    goroot = go_ctx.sdk.root_file.dirname,
    cmd = ctx.executable.mockgen_tool.path,
),

Expected behavior

The _gomock_archive rule should build successfully against rules_go 0.61.x and generate the mock, the same as it does with rules_go 0.60.x.

To Reproduce Steps to reproduce the behavior

  1. In a Bazel module using bazel_dep(name = "bazel_gomock", version = "0.2.0") and bazel_dep(name = "rules_go", version = "0.61.1"), declare a gomock target whose library is a compiled Go library (not source):

    load("@bazel_gomock//rules:gomock.bzl", "gomock")
    
    gomock(
        name = "mock_sess",
        out = "mock_sess_test.go",
        interfaces = ["SessionsClient"],
        library = "//proto/sessions/sessproto",
        package = "main",
    )
  2. Build it: bazel build //path/to:mock_sess

  3. Analysis fails with Error: 'struct' value has no field or method 'go'.

Additional Information

  • gomock mode (reflect or source): reflect (the _gomock_archive code path)
  • gomock version or git ref: bazel_gomock v0.2.0 (latest on the Bazel Central Registry); also reproduces on main
  • golang version: Go 1.26.3 (via rules_go go_sdk)

Triage Notes for the Maintainers

The Go binary is already reachable via the SDK, and go_ctx.sdk.go is used two lines above in the same rule's tools list. Routing the path lookup through the SDK fixes it and is valid on both rules_go 0.60 and 0.61:

-            go = go_ctx.go.path,
+            go = go_ctx.sdk.go.path,

Note that _gomock_source_impl already calls go_context(ctx, include_deprecated_properties = False) and uses sdk.go, so only the archive path needs this change.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions