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

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

Open
tyler-french opened this issue Sep 25, 2024 · 6 comments

Comments

@tyler-french
Copy link
Contributor

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
Copy link
Member

fmeum commented Sep 25, 2024

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
Copy link
Contributor Author

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
Copy link
Member

fmeum commented Sep 30, 2024

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
Copy link

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
Copy link
Member

fmeum commented Nov 13, 2024

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
Copy link

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
)

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