please icon indicating copy to clipboard operation
please copied to clipboard

proto_library for go requires `//third_party/go:protobuf` as dependency

Open mateuszlewko opened this issue 3 years ago • 6 comments

I have the following rule for generating go from proto.

proto_library(
    name = "books_proto",
    srcs = ["books.proto"],
    labels = ["codegen"],
    languages = ["go"],
    visibility = ["PUBLIC"],
)

Building this rule correctly generates books.pb.go, however, building fails with the following error:

Build stopped after 60ms. 2 targets failed:
    //third_party/go:protobuf
Target //third_party/go:protobuf (referenced by //proto:_books_proto#go) doesn't exist
...
Couldn't find dependency //third_party/go:protobuf

I have protoc and protoc-gen-go installed and I don't have any custom configuration for proto or go rules. Do I have to add any rules to third_party/?

mateuszlewko avatar May 07 '22 10:05 mateuszlewko

Yep, you do. protoc and protoc-gen-go generate the code, //third_party/go;protobuf is a go_module that provides the google.golang.org/protobuf dependency for the generated code.

peterebden avatar May 07 '22 10:05 peterebden

Thanks for quick response! I added the relevant rules:

go_mod_download(
    name = "protobuf_download",
    licences = ["BSD-3-Clause"],
    module = "github.com/golang/protobuf",
    version = "v1.5.2",
)

go_module(
    name = "protobuf",
    download = ":protobuf_download",
    exported_deps = [
        ":protobuf-go",
    ],
    install = ["..."],
    module = "github.com/golang/protobuf",
)

go_module(
    name = "protobuf-go",
    install = ["..."],
    module = "google.golang.org/protobuf",
    version = "v1.25.0",
    deps = [
        ":cmp",
    ],
)

go_module(
    name = "cmp",
    install = [
        "cmp/...",
    ],
    module = "github.com/google/go-cmp",
    version = "f144a35ed4ac538fae93fa3783175108738f71b9",
)

Unfortunately building "cmp" as a dependency fails with

Build stopped after 4.7s. 1 target failed: //third_party/go:_cmp#a_rule Error building target //third_party/go:_cmp#a_rule: exit status 1 find: /opt/homebrew/Cellar/go/1.18.1/libexec/pkg/darwin_amd64: No such file or directory

I'm on M1 Macbook. Any ideas how to fix / debug that?

mateuszlewko avatar May 07 '22 11:05 mateuszlewko

Hmmmm it's looking for darwin_amd64 and should be looking for darwin_arm64 on your machine (I assume that directory would exist). I'm not sure why it's doing that though.

What does plz query print //third_party/go:_cmp#a_rule produce?

peterebden avatar May 07 '22 11:05 peterebden

Yes, the darwin_arm64 exists.

This is the output:

plz query print "//third_party/go:_cmp#a_rule"
# //third_party/go:_cmp#a_rule:
build_rule(
    name = '_cmp#a_rule',
    cmd = 'export GOPATH=$TMP_DIR && export GOROOT=$("$TOOLS_GO" env GOROOT) && find "$GOROOT"/pkg/darwin_amd64 -name "*.a" | sed -e s=^"$GOROOT"/pkg/darwin_amd64/== | sed -e s="\.a\$"== | xargs -I{} echo "packagefile {}="$GOROOT"/pkg/darwin_amd64/{}.a" | sort -u > goroot.importconfig && find . -name "*.importconfig" | xargs -I{} cat {} | sort -u >> importconfig && $TOOLS_PLEASE_GO install  --trim_path $TMP_DIR --src_root=$(location :_cmp#get) --module_name=github.com/google/go-cmp --importcfg=importconfig --go_tool=$TOOLS_GO --cc_tool=$TOOLS_CC --out=pkg/darwin_amd64 github.com/google/go-cmp/cmp/... && cat LD_FLAGS',
    srcs = ['//third_party/go:_cmp#get'],
    outs = ['pkg/darwin_amd64/github.com/google/go-cmp/cmp'],
    tools = {
        'cc': ['gcc'],
        'go': ['go'],
        'please_go': ['//_please:please_go'],
    },
    labels = [
        'link:plz-out/go',
        'go',
    ],
    building_description = 'Compiling...',
    needs_transitive_deps = True,
    build_timeout = 600,
    post_build = '<function _add_ld_flags>',
    requires = ['go'],
)

mateuszlewko avatar May 07 '22 11:05 mateuszlewko

Hmmmm right so it has picked up amd64 from somewhere; that should be coming from runtime.GOARCH, although it's a bit indirect.

Can you confirm whether this is actually the darwin_arm64 binary? I would expect it to have to be on an M1 but I'm not very familiar with them, wondering if there's some kind of emulation layer that could explain it...

peterebden avatar May 08 '22 09:05 peterebden

This issue has been automatically marked as stale because it has not had any recent activity in the past 90 days. It will be closed if no further activity occurs. If you require additional support, please reply to this message. Thank you for your contributions.

stale[bot] avatar Aug 10 '22 06:08 stale[bot]