-
Notifications
You must be signed in to change notification settings - Fork 4.1k
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
Allow http_archive directly in MODULE.bazel #17141
Comments
I have a suggestion that could potentially handle both single
The important property of a static module extension is that since it doesn't have access to Assuming this can be made to work, using it would look as follows:
It would allow for direct reuse of existing WORKSPACE macros. While direct invocations of repository rules such as If there are concerns about implicit import-all breaking builds due to changes in transitive dependencies, the following additional check could be added to 3.: 3*. If a module extension usage is labeled as import-all, check whether the module containing the @Wyverald Do you think that this could work with the current evaluation model? |
I don't think this part is true -- depending on other module extensions is as simple as Compared to the duplication of repo names in Thinking about it a bit more, any form of
I'm still pondering whether bare |
That's a great point, I didn't think of
Wouldn't that apply even with 3*? If module A uses its own extension, the What I didn't really understand before I thought about this in more detail: the root cause of the performance problems seems to be that This means that any
I don't think these can be balanced reasonably at all since already direct loads from module repos trigger source archive downloads.
I just thought it would help users recognize the
For any kind of
Following the "typed loads are fine, untyped loads are bad" philosophy from above, couldn't we allow something like this in
It should behave equivalently to
Since we know that If we tell users that |
Backed by experience with this approach in the context of rules_go and after a discussion with @Wyverald, I believe that the following API for # MODULE.bazel
bazel_dep(name = "http", version = "1.0.0")
http.archive(name = "my_archive", url = "https://...", sha256 = "...")
http.file(name = "my_file", url = "https://...", sha256 = "...")
...
use_repo(http, "http")
# BUILD.bazel
load("@http//:def.bzl", "http")
java_binary(
name = "main",
...
data = [
http.file("my_file"),
http.archive("my_archive", "//path/to/pkg:target"),
],
....
) The @keith @alexeagle @kormide What do you think about this? Do you consider it more usable than the handcrafted one-of module extension? |
This looks good to me. So then It may need an optional argument in case they rename the repo in |
The technique would be the same as for the
Which repo? If users do |
Thank you for contributing to the Bazel repository! This issue has been marked as stale since it has not had any activity in the last 30 days. It will be closed in the next 7 days unless any other activity occurs or one of the following labels is added: "not stale", "awaiting-bazeler". |
@bazelbuild/triage Not stale |
With 6a47481 merged, the following helper function should simplify the job of porting a WORKSPACE macro to Bzlmod by turning it into a module extension and using This snippet currently requires using def wrap_macro(macro, *, dev_dependency = False):
def impl(module_ctx):
macro()
return module_ctx.extension_metadata(
root_module_direct_deps = [] if dev_dependency else "all",
root_module_direct_dev_deps = "all" if dev_dependency else [],
)
return module_extension(impl) |
Now that Starting from @SalmaSamy's #16000, one would need to:
If anybody wants to give this a try, please let me know and I can offer support and a (pre-)review. |
Fabian and I revisited this over chat. It appears to me that there's no major argument against something like # MODULE.bazel
http_archive = use_repo_rule("@bazel_tools//:http.bzl", "http_archive")
http_archive(name="foo", ...)
git_repository = use_repo_rule("@bazel_tools//:git.bzl", "git_repository")
git_repository(name="bar", ...) This would be equivalent to today's: # MODULE.bazel
one_off_ext = use_extension(":one_off.bzl", "one_off_ext")
use_repo(one_off_ext, "foo", "bar")
# one_off.bzl
load("@bazel_tools//:http.bzl", "http_archive")
load("@bazel_tools//:git.bzl", "git_repository")
def _one_off_ext_impl(ctx):
http_archive(name="foo", ...)
git_repository(name="bar", ...)
one_off_ext = module_extension(_one_off_ext_impl) Off the top of my head, I can think of some (weak) arguments against this:
So I'm inclined to just say "yes" to |
One thing I realized while implementing this: we need to be rather strict that the thing referred to by |
Ignoring for the moment whether we should support this, what is the technical reason for this restriction? Couldn't we put arbitrary restrictions on the fake tag usage of the repo rule (e.g. enforce attr types only), let it generate arbitrarily many repos for which we then validate the final attribute values, turning them into RepoSpecs, and then only bring the one with the expected |
Hmm... we could indeed do the thing you said. What I'm doing right now is just the straightforward way -- name a repo rule, call it, and we'll create a repo for you. If the thing can be a macro, then we'd need to additionally worry about name conflicts within a single call, and whether it generates a repo with the expected name, etc. That does indeed sound doable, just a bit more complicated. I'm thinking that the majority use case of this would be |
This CL introduces a new directive in the MDOULE.bazel file, `use_repo_rule`, which is a convenient way to declare repos that are only visible to the current module. See #17141 (comment) for example usage. Under the hood, this is implemented as an "innate" module extension for each module that uses `use_repo_rule`. The ID of this extension is a fake one: with the bzl file label of `@@name~version//:MODULE.bazel` and name of `__innate`. Each tag of this extension corresponds to a repo. The name of the tag is the string `${repo_rule_bzl_label}%${repo_rule_name}`, and the attributes of the tag are the attributes of the repo. Fixes #17141. PiperOrigin-RevId: 567722226 Change-Id: I16b8dc562dd20a676118867d831d3b2f5631f767
Not that I am aware of, I think that supporting pure repo rules only is very reasonable - supporting |
This CL introduces a new directive in the MDOULE.bazel file, `use_repo_rule`, which is a convenient way to declare repos that are only visible to the current module. See #17141 (comment) for example usage. Under the hood, this is implemented as an "innate" module extension for each module that uses `use_repo_rule`. The ID of this extension is a fake one: with the bzl file label of `@@name~version//:MODULE.bazel` and name of `__innate`. Each tag of this extension corresponds to a repo. The name of the tag is the string `${repo_rule_bzl_label}%${repo_rule_name}`, and the attributes of the tag are the attributes of the repo. Fixes #17141. PiperOrigin-RevId: 567722226 Change-Id: I16b8dc562dd20a676118867d831d3b2f5631f767
A temporary workaround; see bazelbuild/bazel#17141
A temporary workaround; see bazelbuild/bazel#17141
fails for me on
Shouldn't this case be covered now? Sorry if I misunderstood the point of |
|
Here is the correct incarnation: |
@Wyverald could |
No, as you can't |
But isn't skylib a bzlmod now? If we cannot load, we can just bazel_dep(name = "bazel_skylib", version = "...")
compat = use_extension("@bazel_skylib//compat:extensions.bzl", "use_repo_rule")
compat.use_repo_rule(...) right? |
There is one odd thing with this change that makes it not 100% equivalent. If you need to have any dependencies as |
Indeed, however the canonical repo name isn't any API users should depend on, you can access data files using runfiles library. |
Just like many things with Bazel, things are not as simple, as sometimes what it says in one place is different than what it says somewhere else. Say you have a dependency
Say that Then you have a
Now, if you were to follow
then I would expect for And a third alternative is if you were to follow https://bazel.build/reference/be/c-cpp#cc_test.data where it states
Where is this library? |
Thanks for pointing those issues! I'm certainly not proud of our documentation, it looks like some docs are only meant for Blaze and should be updated for Bazel. Do you mind filing an issue for this problem? |
When attempting to build our codebase with bzlmod, one pattern I noticed came up a lot was one-off
http_archive
s that download binaries or other source repos that don't need to be part of the bzlmod graph because they're either too simple, or one offs etc. In this case I often found myself creating a dead simple module extension that called the macro that we were previously calling from our WORKSPACE, which works fine. The 2 downsides I see to this are there's some unnecessary feeling overhead, and you have to mirror the names of the workspaces in thehttp_archive
name, as well as in theuse_repo
call, and you have to keep those in sync as you add things.I think one solution to this would be to allow
http_archive
s in the MODULE.bazel, but that might have some other major downsides as well.The text was updated successfully, but these errors were encountered: