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

go_deps package visibility #2022

Open
bstoll opened this issue Jan 30, 2025 · 1 comment
Open

go_deps package visibility #2022

bstoll opened this issue Jan 30, 2025 · 1 comment

Comments

@bstoll
Copy link

bstoll commented Jan 30, 2025

I am trying to use the go_deps extension to import dependencies from go.mod files and am running into a case that works with Go's dependency resolution but does not work so well with Gazelle/Bazel.

I have an example repository at https://github.com/bstoll/import-visibility-parent and https://github.com/bstoll/import-visibility-child. The parent depends on the child, which depends on the parent. In this scenario, Go's module system allows this to work:

[bstoll@bstoll-ct:parent]$ go run github.com/bstoll/import-visibility-parent/cmd
Hello from main v2
Hello from child v2
Hello from parent v2

However, with Bazel this fails:

[bstoll@bstoll-ct:parent]$ bazel run //cmd:cmd
ERROR: no such package '@@[unknown repo 'com_github_bstoll_import_visibility_parent' requested from @@gazelle++go_deps+com_github_bstoll_import_visibility_child]//': The repository '@@[unknown repo 'com_github_bstoll_import_visibility_parent' requested from @@gazelle++go_deps+com_github_bstoll_import_visibility_child]' could not be resolved: No repository visible as '@com_github_bstoll_import_visibility_parent' from repository '@@gazelle++go_deps+com_github_bstoll_import_visibility_child'
ERROR: /usr/local/google/home/bstoll/.cache/bazel/_bazel_bstoll/2e7b003f152a6b9486f33cb666da68af/external/gazelle++go_deps+com_github_bstoll_import_visibility_child/BUILD.bazel:6:11: no such package '@@[unknown repo 'com_github_bstoll_import_visibility_parent' requested from @@gazelle++go_deps+com_github_bstoll_import_visibility_child]//': The repository '@@[unknown repo 'com_github_bstoll_import_visibility_parent' requested from @@gazelle++go_deps+com_github_bstoll_import_visibility_child]' could not be resolved: No repository visible as '@com_github_bstoll_import_visibility_parent' from repository '@@gazelle++go_deps+com_github_bstoll_import_visibility_child' and referenced by '@@gazelle++go_deps+com_github_bstoll_import_visibility_child//:import-visibility-child'
ERROR: Analysis of target '//cmd:cmd' failed; build aborted: Analysis failed
INFO: Elapsed time: 0.783s, Critical Path: 0.00s
INFO: 1 process: 1 internal.
ERROR: Build did NOT complete successfully
ERROR: Build failed. Not running target

The issue seems to be that the parent package is not made available as com_github_bstoll_import_visibility_parent to go_deps. There are some knobs I found that force this into working by adding this to MODULE.bazel:

go_deps_dev = use_extension("@bazel_gazelle//:extensions.bzl", "go_deps", dev_dependency = True)
go_deps_dev.gazelle_override(
    directives = [
        "gazelle:resolve go github.com/bstoll/import-visibility-parent @import-visibility-parent//:import-visibility-parent",  # keep
    ],
    path = "github.com/bstoll/import-visibility-child",
)
inject_repo(go_deps_dev, "import-visibility-parent")

I am not sure if this is an ideal solution to this problem. While unwinding this is probably ideal, can the go_deps extension make the parent repository available for children to import from?

@fmeum
Copy link
Member

fmeum commented Feb 8, 2025

Fixing this may be as easy as running the code in this else block unconditionally:

# Register this Bazel module as providing the specified Go module. It participates

Could you give that a try?

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