Skip to content

Commit

Permalink
go_deps.config
Browse files Browse the repository at this point in the history
  • Loading branch information
fmeum committed Mar 6, 2024
1 parent b131367 commit 8dc6565
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 9 deletions.
4 changes: 2 additions & 2 deletions MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ bazel_dep(name = "rules_go", version = "0.44.0", repo_name = "io_bazel_rules_go"
bazel_dep(name = "rules_proto", version = "4.0.0")

go_sdk = use_extension("@io_bazel_rules_go//go:extensions.bzl", "go_sdk")

# Known to exist since it is instantiated by rules_go itself.
use_repo(
go_sdk,
# Known to exist since they are instantiated by rules_go itself.
"go_host_compatible_sdk_label",
"io_bazel_rules_go_go_env",
)

non_module_deps = use_extension("//internal/bzlmod:non_module_deps.bzl", "non_module_deps")
Expand Down
25 changes: 23 additions & 2 deletions internal/bzlmod/go_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,9 @@ def _is_dev_dependency(module_ctx, tag):
# not available.
return module_ctx.is_dev_dependency(tag) if hasattr(module_ctx, "is_dev_dependency") else False

def _intersperse_newlines(tags):
return [tag for p in zip(tags, len(tags) * ["\n"]) for tag in p]

# This function processes the gazelle_default_attributes tag for a given module and returns a struct
# containing the attributes from _GAZELLE_ATTRS that are defined in the tag.
def _process_gazelle_default_attributes(module_ctx):
Expand Down Expand Up @@ -260,14 +263,19 @@ def _go_repository_config_impl(ctx):
))

ctx.file("WORKSPACE", "\n".join(repos))
ctx.file("BUILD.bazel", "exports_files(['WORKSPACE'])")
ctx.file("BUILD.bazel", "exports_files(['WORKSPACE', 'config.json'])")
ctx.file("go_env.bzl", content = "GO_ENV = " + repr(ctx.attr.go_env))

# For use by @rules_go//go.
ctx.file("config.json", content = json.encode_indent(ctx.attr.go_env))

_go_repository_config = repository_rule(
implementation = _go_repository_config_impl,
attrs = {
"importpaths": attr.string_dict(mandatory = True),
"module_names": attr.string_dict(mandatory = True),
"build_naming_conventions": attr.string_dict(mandatory = True),
"go_env": attr.string_dict(mandatory = True),
},
)

Expand Down Expand Up @@ -301,11 +309,19 @@ def _go_deps_impl(module_ctx):
root_module_direct_deps = {}
root_module_direct_dev_deps = {}

if module_ctx.modules[0].name == "gazelle":
first_module = module_ctx.modules[0]
if first_module.is_root and first_module.name in ["gazelle", "rules_go"]:
root_module_direct_deps["bazel_gazelle_go_repository_config"] = None

outdated_direct_dep_printer = print
go_env = {}
for module in module_ctx.modules:
if len(module.tags.config) > 1:
fail(
"Multiple \"go_deps.config\" tags defined in module \"{}\":\n".format(module.name),
*_intersperse_newlines(module.tags.config)
)

# Parse the go_deps.config tag of the root module only.
for mod_config in module.tags.config:
if not module.is_root:
Expand All @@ -317,6 +333,7 @@ def _go_deps_impl(module_ctx):
outdated_direct_dep_printer = print
elif check_direct_deps == "error":
outdated_direct_dep_printer = fail
go_env = mod_config.go_env

_process_overrides(module_ctx, module, "gazelle_override", gazelle_overrides, _process_gazelle_override)
_process_overrides(module_ctx, module, "module_override", module_overrides, _process_module_override, archive_overrides)
Expand Down Expand Up @@ -533,6 +550,7 @@ def _go_deps_impl(module_ctx):
)
for path, module in module_resolutions.items()
}),
go_env = go_env,
)

return _extension_metadata(
Expand Down Expand Up @@ -572,6 +590,9 @@ _config_tag = tag_class(
"check_direct_dependencies": attr.string(
values = ["off", "warning", "error"],
),
"go_env": attr.string_dict(
doc = "The environment variables to use when fetching Go dependencies or running the `@rules_go//go` tool.",
),
},
)

Expand Down
2 changes: 1 addition & 1 deletion internal/bzlmod/non_module_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ load(
"HOST_COMPATIBLE_SDK",
)
load(
"@io_bazel_rules_go_go_env//:go_env.bzl",
"@bazel_gazelle_go_repository_config//:go_env.bzl",
"GO_ENV",
)

Expand Down
9 changes: 5 additions & 4 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ module(
name = "gazelle_bcr_tests",
)


bazel_dep(name = "gazelle", version = "")
local_path_override(
module_name = "gazelle",
path = "../..",
)

local_path_override(module_name = "rules_go", path = "../../../rules_go")

bazel_dep(name = "test_dep", version = "1.0.0")
local_path_override(
module_name = "test_dep",
Expand All @@ -22,13 +19,17 @@ bazel_dep(name = "rules_go", version = "0.42.0", repo_name = "my_rules_go")
bazel_dep(name = "rules_proto", version = "6.0.0-rc2", repo_name = "my_rules_proto")

go_sdk = use_extension("@my_rules_go//go:extensions.bzl", "go_sdk")
go_sdk.env(name = "GOPRIVATE", value = "example.com")

# This bazel_dep provides the Go dependency github.com/cloudflare/circl, which requires custom
# patches beyond what Gazelle can generate.
bazel_dep(name = "circl", version = "1.3.7")

go_deps = use_extension("@gazelle//:extensions.bzl", "go_deps")
go_deps.config(
go_env = {
"GOPRIVATE": "example.com/*",
},
)

# Validate a go.mod replace directive works.
go_deps.from_file(go_mod = "//:go.mod")
Expand Down

0 comments on commit 8dc6565

Please sign in to comment.