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

Writing resolved Go modules as K:V JSON for external bookkeeping #1906

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
5 changes: 5 additions & 0 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -592,6 +592,7 @@ def _go_deps_impl(module_ctx):
),
)

resolved_go_modules = {}
for path, module in module_resolutions.items():
if hasattr(module, "module_name"):
# Do not create a go_repository for a Go module provided by a bazel_dep.
Expand Down Expand Up @@ -643,8 +644,12 @@ def _go_deps_impl(module_ctx):

go_repository_args.update(repo_args)

if "version" in go_repository_args:
resolved_go_modules[go_repository_args["importpath"]] = go_repository_args["version"]

go_repository(**go_repository_args)

module_ctx.file("./resolved_go_modules.json", content=json.encode_indent(resolved_go_modules))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Files written by module extensions are emitted into an unspecified temporary directory that should be removed after execution. We would need to instantiate a repo with this info or accept an absolute path via an environment variable.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, I'm propagating this similar to config.json used by rules_go. I was able to verify that I can access this file as @bazel_gazelle_go_repository_config//:resolved_deps.json. Would this approach work?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can also expose this via a separate repo rule if that's preferable. On a related note, while I can pull this into my workspace as @bazel_gazelle_go_repository_config//:resolved_deps.json, bazel mod tidy still thinks this repo is an indirect dependency and removes it from MODULE.bazel (I'm explicitly passing it in as inputs to both a build rule and a sh_binary rule). Is that perhaps a bug? If not, can I add a directive to inform bazel mod tidy that I intend to keep that use_repo call?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unsure why Windows build of rules_go is failing but interestingly I see Bazel warn that the repo is an indirect dependency in Gazelle's own MODULE.bazel file:

(16:03:24) WARNING: C:/b/bk-windows-xmds/bazel/bazel-gazelle/tests/bcr/go_work/MODULE.bazel:27:24: The module extension go_deps defined in @gazelle//:extensions.bzl reported incorrect imports of repositories via use_repo():
--
  |  
  | Imported, but reported as indirect dependencies by the extension:
  | bazel_gazelle_go_repository_config, org_golang_google_protobuf

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the failure was intermittent and unrelated to the change

# Create a synthetic WORKSPACE file that lists all Go repositories created
# above and contains all the information required by Gazelle's -repo_config
# to generate BUILD files for external Go modules. This skips the need to
Expand Down