Building BadgerDB v3 with Bazel fails

What version of Go are you using (go version)?

$ go version
go version go1.14.7 linux/amd64

What operating system are you using?

Linux code-0 5.4.0-62-generic #70-Ubuntu SMP Tue Jan 12 12:45:47 UTC 2021 x86_64 GNU/Linux

What version of Badger are you using?

v3@head

Does this issue reproduce with the latest master?

Yes

Steps to Reproduce the issue

Import BadgerDB via Bazel in another Go program, like so:

load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")

go_library(
    name = "database",
    srcs = ["database.go"],
    importpath = "your/import/path", # Masked for this issue/bug report.
    visibility = ["//visibility:public"],
    deps = ["@com_github_dgraph_io_badger_v3//:badger"],
)

What Badger options were set?

None/NA

What did you do?

I built Badger as a dep via Bazel using bazel build package:target, i.e. bazel build database:database

What did you expect to see?

A successful build.

What did you see instead?

Use --sandbox_debug to see verbose messages from the sandbox builder failed: error executing command bazel-out/host/bin/external/go_sdk/builder compilepkg -sdk external/go_sdk -installsuffix linux_amd64 -src external/com_github_dgraph_io_badger_v3/backup.go -src ... (remaining 93 argument(s) skipped)

Use --sandbox_debug to see verbose messages from the sandbox
external/com_github_dgraph_io_badger_v3/batch.go:115:15: kv.Unmarshal undefined (type *pb.KV has no field or method Unmarshal)
external/com_github_dgraph_io_badger_v3/key_registry.go:198:18: dataKey.Unmarshal undefined (type *pb.DataKey has no field or method Unmarshal)
external/com_github_dgraph_io_badger_v3/key_registry.go:407:18: k.Marshal undefined (type *pb.DataKey has no field or method Marshal)
external/com_github_dgraph_io_badger_v3/stream.go:466:15: kv.Unmarshal undefined (type *pb.KV has no field or method Unmarshal)
external/com_github_dgraph_io_badger_v3/stream.go:476:29: kv.Size undefined (type *pb.KV has no field or method Size)
external/com_github_dgraph_io_badger_v3/stream.go:477:10: not enough arguments in call to y.Check2
external/com_github_dgraph_io_badger_v3/stream.go:477:13: kv.MarshalToSizedBuffer undefined (type *pb.KV has no field or method MarshalToSizedBuffer)
external/com_github_dgraph_io_badger_v3/stream_writer.go:99:15: kv.Unmarshal undefined (type pb.KV has no field or method Unmarshal)
compilepkg: error running subcommand external/go_sdk/pkg/tool/linux_amd64/compile: exit status 2
Target //cmd:cmd failed to build
1 Like

@Cidan any progress on this? Did you maybe find a workaround/fix?

Ran into the same issue.
Setting build_file_proto_mode = "disable" stops gazelle from generating the proto library rules and uses the checked in gogo generated file and seems to fix the issue.

Here’s the complete go_repository rule we are using:

go_repository(
    name = "com_github_dgraph_io_badger_v3",
    build_file_proto_mode = "disable",
    importpath = "github.com/dgraph-io/badger/v3",
    sum = "h1:Hmyof0WMEF/QtutX5SQHzIMnJQxb/IrSzhjckV2SD6g=",
    version = "v3.2011.1",
)
1 Like

@vjm thank you very much. That solved it!

Alternatively, you can do:

gazelle(
    name = "gazelle",
    args = [
        "-go_proto_compiler=@io_bazel_rules_go//proto:gogofaster_proto",
    ],
    prefix = "github.com/dgraph-io/badger/v3",
)

and then bazel run //:gazelle fix. This will make these protobufs actually generated from source.

Or if you want to use bundled pre-compiled protobuf – replace the content of pb/BUILD.bazel with:

# gazelle:exclude **/**.proto

PS. Just in case it will be useful for someone: here is the bazelification of the bager (w/ VSCode integration as a bonus.)