From 7916a0f0876a1835ea155e388b57e7aecb8a0e3f Mon Sep 17 00:00:00 2001 From: Buzz-Lightyear Date: Thu, 29 Aug 2024 09:30:29 -0400 Subject: [PATCH 1/5] Writing resolved Go modules as K:V JSON for external bookkeeping --- internal/bzlmod/go_deps.bzl | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index dcd0db37c..c44da63f6 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -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. @@ -643,8 +644,10 @@ def _go_deps_impl(module_ctx): go_repository_args.update(repo_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)) # 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 From dfc4ad6b0a859fe04540e5e2ad4509644acb67c3 Mon Sep 17 00:00:00 2001 From: Buzz-Lightyear Date: Thu, 29 Aug 2024 09:50:03 -0400 Subject: [PATCH 2/5] Only adding K:V for modules that have resolved versions --- internal/bzlmod/go_deps.bzl | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index c44da63f6..3b52fcfed 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -644,7 +644,9 @@ def _go_deps_impl(module_ctx): go_repository_args.update(repo_args) - resolved_go_modules[go_repository_args["importpath"]] = go_repository_args["version"] + 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)) From f11a8a6c5d8d5e84c605438e3d93ca133d9789cd Mon Sep 17 00:00:00 2001 From: Buzz-Lightyear Date: Fri, 30 Aug 2024 11:27:12 -0400 Subject: [PATCH 3/5] Trying to explose resolved deps via bazel_gazelle_go_repository_config --- internal/bzlmod/go_deps.bzl | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index 3b52fcfed..4a68eeb14 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -268,6 +268,9 @@ def _go_repository_config_impl(ctx): "dep_files": ctx.attr.dep_files, })) + # For bookkeeping by upstream systems that may want this info + ctx.file("resolved_deps.json", content = json.encode_indent(ctx.attr.resolved_deps)) + _go_repository_config = repository_rule( implementation = _go_repository_config_impl, attrs = { @@ -275,6 +278,7 @@ _go_repository_config = repository_rule( "module_names": attr.string_dict(mandatory = True), "build_naming_conventions": attr.string_dict(mandatory = True), "go_env": attr.string_dict(mandatory = True), + "resolved_deps": attr.string_dict(mandatory = True), "dep_files": attr.string_list(), }, ) @@ -649,7 +653,6 @@ def _go_deps_impl(module_ctx): go_repository(**go_repository_args) - module_ctx.file("./resolved_go_modules.json", content=json.encode_indent(resolved_go_modules)) # 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 @@ -674,6 +677,7 @@ def _go_deps_impl(module_ctx): }), go_env = go_env, dep_files = dep_files, + resolved_deps = resolved_go_modules, ) return extension_metadata( From 367d6127b78527600f0aeb615d29303f7070a206 Mon Sep 17 00:00:00 2001 From: Buzz-Lightyear Date: Fri, 30 Aug 2024 11:36:36 -0400 Subject: [PATCH 4/5] For internal modules, version is None, so we're excluding it --- internal/bzlmod/go_deps.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index 4a68eeb14..3c28d9769 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -648,7 +648,7 @@ def _go_deps_impl(module_ctx): go_repository_args.update(repo_args) - if "version" in go_repository_args: + if "version" in go_repository_args and go_repository_args["version"]: resolved_go_modules[go_repository_args["importpath"]] = go_repository_args["version"] go_repository(**go_repository_args) From aa09427531ac0570f0072430ee755d62cfbcb6df Mon Sep 17 00:00:00 2001 From: Buzz-Lightyear Date: Fri, 30 Aug 2024 11:43:14 -0400 Subject: [PATCH 5/5] Exporting resolved_deps.json --- internal/bzlmod/go_deps.bzl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/bzlmod/go_deps.bzl b/internal/bzlmod/go_deps.bzl index 3c28d9769..b7e08c99a 100644 --- a/internal/bzlmod/go_deps.bzl +++ b/internal/bzlmod/go_deps.bzl @@ -259,7 +259,7 @@ def _go_repository_config_impl(ctx): )) ctx.file("WORKSPACE", "\n".join(repos)) - ctx.file("BUILD.bazel", "exports_files(['WORKSPACE', 'config.json'])") + ctx.file("BUILD.bazel", "exports_files(['WORKSPACE', 'config.json', 'resolved_deps.json'])") ctx.file("go_env.bzl", content = "GO_ENV = " + repr(ctx.attr.go_env)) # For use by @rules_go//go.