bazel-contrib / bazel-gazelle

Gazelle is a Bazel build file generator for Bazel projects. It natively supports Go and protobuf, and it may be extended to support new languages and custom rule sets.
Apache License 2.0
1.21k stars 381 forks source link

bzlmod/go_deps: bazel mod tidy adds repos that are loaded as bazel modules #1935

Open tyler-french opened 1 month ago

tyler-french commented 1 month ago

To reproduce:

MODULE.bazel

bazel_dep(name = "flatbuffers", version = "24.3.25", repo_name = "com_github_google_flatbuffers")

go.mod (includes direct dep)

    github.com/google/flatbuffers v2.0.8+incompatible

bazel mod tidy:

ported incorrect imports of repositories via use_repo():

Not imported, but reported as direct dependencies by the extension (may cause the build to fail):
    com_github_google_flatbuffers

Fix the use_repo calls by running 'bazel mod tidy'.
INFO: Updated use_repo calls for @bazel_gazelle//:extensions.bzl%go_deps

Build failure caused:

tfrench@go(tfrench.devpod-us-va) ~/go-code
 % bazel mod --enable_bzlmod tidy
INFO: Invocation ID: ff5667e5-9cef-425c-b978-77434bfebe7d
ERROR: Traceback (most recent call last):
        File "/home/user/go-code/MODULE.bazel", line 209, column 8, in <toplevel>
                include("//third_party:go_deps.MODULE.bazel")
        File "<builtin>", in include
        File "/home/user/go-code/third_party/go_deps.MODULE.bazel", line 17, column 9, in <toplevel>
                use_repo(
Error in use_repo: The repo name 'com_github_google_flatbuffers' is already being used by a bazel_dep at /home/user/go-code/MODULE.bazel:40:10
ERROR: error executing MODULE.bazel file for <root>. Type 'bazel help mod' for syntax and help.
fmeum commented 1 month ago

In Bazel 7.4.0 and 8, you will be able to have go_deps reuse the bazel_dep via override_repo. I will add a test case to verify that bazel mod tidy behaves correctly in that case.

tyler-french commented 1 month ago

I wonder if for now we can just look at all the repo_name of the bazel_dep in the root MODULE.bazel, and then skip adding these to direct dependencies?

fmeum commented 1 month ago

The Gazelle extension can't do this as it doesn't have access to MODULE files.

Having looked into this more, I think that it's actually a feature (but not with a good error message): You have the same dep show up under two different names in your dep tree, which can result in linker errors. Instead, when Bazel 7.4.0 lands, you should use override_repo to replace the extension repo with the Bazel dep.

AlejoAsd commented 1 week ago

I am facing a similar problem, but the project I am working on uses Bazel 7.0.2, and this is something I don't have control over.

Is there a way to manually tell Gazelle not to manage a specific dependency in older Bazel versions that don't have override_repo?

fmeum commented 1 week ago

Could you rename the conflicting dep via the repo_name attribute of bazel_dep? We can add a new knob, I would just like to make sure it's the right one and actually needed.

AlejoAsd commented 1 week ago

This is the solution I ended up going with, but it's not ideal because I am virtually importing the same dependency twice.

bazel_dep("grpc_gateway", version = "2.23.0") # removed `, repo_name = "com_github_grpc_ecosystem_grpc_gateway_v2"`

use_repo(
  go_deps,
  "com_github_grpc_ecosystem_grpc_gateway_v2", # added via go.mod
)