Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gazelle:proto disable does not propagate into external dependencies with bzlmod #1854

Open
fishy opened this issue Aug 5, 2024 · 2 comments

Comments

@fishy
Copy link

fishy commented Aug 5, 2024

What version of gazelle are you using?

0.38.0

What version of rules_go are you using?

0.48.1

What version of Bazel are you using?

7.2.1

Does this issue reproduce with the latest releases of all the above?

Those are latest versions

What operating system and processor architecture are you using?

linux/amd64

What did you do?

let's say we have an external go library, github.com/foo/bar, that does not have any bazel build files (it does not use bazel to build)

this repo contains a proto file that imports google/rpc/status.proto:

github.com/foo/bar/proto/bar.proto:

syntax = "proto3";

package barpb;

import "google/protobuf/struct.proto";
import "google/rpc/status.proto";

...

and it contains protoc generated go files committed into source control:

github.com/foo/bar/proto/barpb/bar.pb.go
github.com/foo/bar/proto/barpb/bar_grpc.pb.go

now, a project using bazel using bzlmod depends on that external go library:

in go.mod:

...
require (
  ...
  github.com/foo/bar v...
  ...
)
...

in MODULES.bazel:

...
use_repo(
    go_deps,
    ...
    "com_github_foo_bar",
    ...
)
...

In top-level BUILD.bazel I added gazelle:proto disable instruction (the resolve instruction is likely not needed but I added it just in case):

load("@gazelle//:def.bzl", "gazelle")

# gazelle:proto disable
# gazelle:prefix ...
# gazelle:resolve go github.com/foo/bar/proto/barpb @com_github_foo_bar//proto/barpb
gazelle(name = "gazelle")

when building the project via bazel, bazel complains about google/rpc:

ERROR: no such package '@@gazelle~~go_deps~com_github_foo_bar//google/rpc': BUILD file not found in directory 'google/rpc' of external repository @@gazelle~~go_deps~com_github_foo_bar. Add a BUILD file to a directory to mark it as a package.

The build file it refers to is generated for the proto file:

load("@io_bazel_rules_go//go:def.bzl", "go_library")
load("@io_bazel_rules_go//proto:def.bzl", "go_proto_library")
load("@rules_proto//proto:defs.bzl", "proto_library")

proto_library(
    name = "barpb_proto",
    srcs = ["bar.proto"],
    visibility = ["//visibility:public"],
    deps = [
        "//google/rpc:rpc_proto",
        "@com_google_protobuf//:struct_proto",
    ],
)

go_proto_library(
    name = "barpb_go_proto",
    compilers = ["@io_bazel_rules_go//proto:go_grpc"],
    importpath = "github.com/foo/bar/proto/barpb",
    proto = ":barpb_proto",
    visibility = ["//visibility:public"],
    deps = ["//google/rpc:status_proto"],
)

go_library(
    name = "barpb",
    embed = [":barpb_go_proto"],
    importpath = ".com/foo/bar/proto/barpb",
    visibility = ["//visibility:public"],
)

alias(
    name = "go_default_library",
    actual = ":barpb",
    visibility = ["//visibility:public"],
)

there seems to be 2 issues here:

  1. the proto import of google/protobuf/struct.proto seems to be handled correctly but google/rpc/status.proto is not
  2. with gazelle:proto disable instruction the build file for that external proto file should not be generated at all, and nothing should reference it?

What did you expect to see?

What did you see instead?

@fmeum
Copy link
Member

fmeum commented Aug 6, 2024

gazelle:proto disable not propagating to external deps is intentional, directives generally only apply to the subtree of the directory they appear in.

See https://github.com/bazelbuild/rules_go/blob/master/docs/go/core/bzlmod.md#gazelle-directives for how to add directives to a single external module. You can also use gazelle_default_attributes to add directives to all external modules.

If that doesn't help, please share a complete reproducer.

@fishy
Copy link
Author

fishy commented Aug 6, 2024

Thanks. I added this to my MODULE.bazel and it worked:

go_deps.gazelle_default_attributes(
    directives = [
        "gazelle:proto disable",
    ],
)

a note is that when I try to search for gazelle_default_attributes in this repo on github, I only found 2 usages of it under tests/bcr, and the code defining _process_gazelle_default_attributes. it did not find any docs to describe how it's used. both example uses both directives and also build_file_generation = "on", makes me think build_file_generation would default to off and if I don't explicitly set it here it will not work, but looks like removing that still works. I'd suggest improve the documentation on that :)

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

No branches or pull requests

2 participants