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

rules_cc & rules_java pulled in by github.com/envoyproxy/protoc-gen-validate/validate BUILD file cause build to fail #1980

Closed
fishy opened this issue Nov 14, 2024 · 5 comments

Comments

@fishy
Copy link

fishy commented Nov 14, 2024

What version of gazelle are you using?

0.40.0

What version of rules_go are you using?

0.50.1

What version of Bazel are you using?

7.4.1

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

Those are latest releases

What operating system and processor architecture are you using?

linux/amd64

What did you do?

When github.com/envoyproxy/protoc-gen-validate/validate is imported by some go code, its BUILD file references to rules_cc and rules_java, which would cause build to fail if I disable gazelle:proto, even if I added them to MODULES.bazel. here is a minimal reproducer:

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

# gazelle:proto disable
gazelle(name = "gazelle")

go_library(
    name = "test-gazelle_lib",
    srcs = ["main.go"],
    importpath = "github.com/fishy/test-gazelle",
    visibility = ["//visibility:private"],
    deps = ["@com_github_envoyproxy_protoc_gen_validate//validate"],
)

go_binary(
    name = "test-gazelle",
    embed = [":test-gazelle_lib"],
    pure = "on",
    visibility = ["//visibility:public"],
)

$ cat go.mod 
module github.com/fishy/test-gazelle

go 1.23

require github.com/envoyproxy/protoc-gen-validate v1.1.0

require (
        github.com/google/go-cmp v0.6.0 // indirect
        google.golang.org/protobuf v1.34.2 // indirect
)

$ cat main.go 
package main

import (
        "fmt"

        _ "github.com/envoyproxy/protoc-gen-validate/validate"
)

func main() {
        fmt.Println("Hello")
}

$ cat MODULE.bazel
bazel_dep(name = "rules_go", version = "0.50.1")
bazel_dep(name = "gazelle", version = "0.40.0")
bazel_dep(name = "rules_cc", version = "0.1.0")
bazel_dep(name = "rules_java", version = "8.3.2")

go_sdk = use_extension("@rules_go//go:extensions.bzl", "go_sdk")
go_sdk.download(
    name = "go_sdk",
    version = "1.23.3",
)

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
go_deps.config(check_direct_dependencies = "error")
go_deps.gazelle_default_attributes(
    directives = [
        "gazelle:proto disable",
    ],
)
use_repo(
    go_deps,
    "com_github_envoyproxy_protoc_gen_validate",
)
$ bazel build :test-gazelle
ERROR: error loading package '@@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate//validate': Unable to find package for @@[unknown repo 'rules_java' requested from @@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate]//java:defs.bzl: The repository '@@[unknown repo 'rules_java' requested from @@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate]' could not be resolved: No repository visible as '@rules_java' from repository '@@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate'.
ERROR: /home/fishy/work/test-gazelle/BUILD.bazel:7:11: error loading package '@@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate//validate': Unable to find package for @@[unknown repo 'rules_java' requested from @@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate]//java:defs.bzl: The repository '@@[unknown repo 'rules_java' requested from @@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate]' could not be resolved: No repository visible as '@rules_java' from repository '@@gazelle~~go_deps~com_github_envoyproxy_protoc_gen_validate'. and referenced by '//:test-gazelle_lib'
ERROR: Analysis of target '//:test-gazelle' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.133s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully

We only need to go lib there, I was hoping there's a way to ignore rules_cc and rules_java that's not really used by the go_library

What did you expect to see?

What did you see instead?

@fishy
Copy link
Author

fishy commented Nov 19, 2024

For vendored go dependencies this can be address by tricks like #1982 (comment) to force gazelle to regenerate all build files, but for non-vendored mode, I think we need an option for gazelle to always regenerate build files for the go dependencies (e.g. ignore existing build files and treat all go dependencies as they don't have existing build files).

@fmeum
Copy link
Member

fmeum commented Nov 20, 2024

This exists as build_file_generation mode clean, could you give that a try?

@fishy
Copy link
Author

fishy commented Nov 20, 2024

Verified that setting build_file_generation to clean fixes this issue.

@fishy fishy closed this as completed Nov 20, 2024
@bennettzhu1
Copy link

bennettzhu1 commented Nov 26, 2024

@fishy Can you provide the relevant gazelle override/gazelle default overrides which worked for you? Getting the same error and something like this does not work for me.

go_deps.gazelle_override(
    build_file_generation = "clean",
    directives = [
        "gazelle:build_file_name BUILD.bazel",
    ],
    path = "github.com/envoyproxy/protoc-gen-validate",
)

It only works when the gazelle_default_attributes is set to build_file_generation = "clean"

@fishy
Copy link
Author

fishy commented Nov 26, 2024

@bennettzhu1 something like this in MODULE.bazel:

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.from_file(go_mod = "//:go.mod")
go_deps.gazelle_default_attributes(
    build_file_generation = "clean",
)

use_repo(
    go_deps,
    ...,
)

see https://github.com/bazel-contrib/rules_go/blob/master/docs/go/core/bzlmod.md#gazelle-directives

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

3 participants