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

Add go_deps.config(env = ...) as a replacement for gazelle_dependencies(go_env = ...) #1748

Merged
merged 2 commits into from
Mar 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ use_repo(
bazel_dep(name = "bazel_skylib_gazelle_plugin", version = "1.4.1", dev_dependency = True)
bazel_dep(name = "stardoc", version = "0.6.2", dev_dependency = True, repo_name = "io_bazel_stardoc")


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

# Known to exist since it is instantiated by rules_go itself.
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
6 changes: 5 additions & 1 deletion internal/bzlmod/non_module_deps.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ load(
"@go_host_compatible_sdk_label//:defs.bzl",
"HOST_COMPATIBLE_SDK",
)
load(
"@bazel_gazelle_go_repository_config//:go_env.bzl",
"GO_ENV",
)

visibility("//")

Expand All @@ -36,7 +40,7 @@ def _non_module_deps_impl(_):
name = "bazel_gazelle_go_repository_cache",
# Label.workspace_name is always a canonical name, so use a canonical label.
go_sdk_name = "@" + HOST_COMPATIBLE_SDK.workspace_name,
go_env = {},
go_env = GO_ENV,
)
go_repository_tools(
name = "bazel_gazelle_go_repository_tools",
Expand Down
7 changes: 7 additions & 0 deletions tests/bcr/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,18 @@ bazel_dep(name = "protobuf", version = "23.1", repo_name = "my_protobuf")
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")

# 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