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

feat: add a module extension for http_archive #341

Closed
wants to merge 1 commit into from
Closed
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
8 changes: 8 additions & 0 deletions e2e/bzlmod/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -39,3 +39,11 @@ diff_test(
file1 = "case_no_sources",
file2 = "expected",
)

# Exercise http module extension
genrule(
name = "bazel_lib_readme",
srcs = ["@bazel_lib_srcs//:README.md"],
outs = ["bazel_lib_README.md"],
cmd = "cp $(execpath @bazel_lib_srcs//:README.md) $@",
)
12 changes: 12 additions & 0 deletions e2e/bzlmod/MODULE.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,15 @@ local_path_override(
module_name = "aspect_bazel_lib",
path = "../..",
)

http = use_extension("@aspect_bazel_lib//lib:extensions.bzl", "http")

http.http_archive(
name = "bazel_lib_srcs",
build_file_content = """exports_files(["README.md"])""",
sha256 = "79623d656aa23ad3fd4692ab99786c613cd36e49f5566469ed97bc9b4c655f03",
strip_prefix = "bazel-lib-1.23.3",
urls = ["https://github.com/aspect-build/bazel-lib/archive/refs/tags/v1.23.3.tar.gz"],
)

use_repo(http, "bazel_lib_srcs")
45 changes: 45 additions & 0 deletions lib/extensions.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load(
"register_jq_toolchains",
"register_yq_toolchains",
)
load("@bazel_tools//tools/build_defs/repo:http.bzl", "http_archive")

def _toolchain_extension(_):
register_yq_toolchains(register = False)
Expand All @@ -16,3 +17,47 @@ def _toolchain_extension(_):
ext = module_extension(
implementation = _toolchain_extension,
)

def _http_extension(mctx):
for mod in mctx.modules:
for attr in mod.tags.http_archive:
http_archive(
name = attr.name,
build_file_content = attr.build_file_content,
sha256 = attr.sha256,
strip_prefix = attr.strip_prefix,
urls = attr.urls,
)

# Extension that can declare http repository rules, until there's a better way:
# https://github.com/bazelbuild/bazel/issues/17141
http = module_extension(
Copy link
Collaborator

Choose a reason for hiding this comment

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

It seems a little gross, but comments on that issue from Keith make me think that someone has to have that code somewhere, so bazel-lib is the best place we have.

@fmeum do you have any opinion about it?

Copy link
Member

@fmeum fmeum Jan 19, 2023

Choose a reason for hiding this comment

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

Yes, please don't do it :-) It suggests that something works that is inherently broken: The names provided to the extension tag need to be globally unique, which they will be in small projects and then break when composed with other modules.

It's fine if someone builds this for their own module (and thus have the generated repos scoped under this one-off extension), but having it available in a general purpose skylib-like library makes it more likely these collisions will happen in practice and break the "strict deps" promise of Bzlmod.

I think that we should just accept the overhead of wrapping a bunch of http_archives in a macro and making it a module extension for now and wait for a resolution to bazelbuild/bazel#17141.

implementation = _http_extension,
tag_classes = {
"http_archive": tag_class(attrs = {
"name": attr.string(),
"url": attr.string(),
"urls": attr.string_list(),
"sha256": attr.string(),
"integrity": attr.string(),
"netrc": attr.string(),
"auth_patterns": attr.string_dict(),
"canonical_id": attr.string(),
"strip_prefix": attr.string(),
"add_prefix": attr.string(),
"type": attr.string(),
"patches": attr.label_list(),
"remote_patches": attr.string_dict(),
"remote_patch_strip": attr.int(),
"patch_tool": attr.string(),
"patch_args": attr.string_list(),
"patch_cmds": attr.string_list(),
"patch_cmds_win": attr.string_list(),
"build_file": attr.label(),
"build_file_content": attr.string(),
"workspace_file": attr.label(),
"workspace_file_content": attr.string(),
}),
# TODO: http_file and http_jar
},
)