Replace apparent_repo_name with repo rule wrappers #1650
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Description
This presents the same API, remains compatible with both
WORKSPACE
and Bzlmod, and avoids relying on the canonical repository name format at all. Part of #1482, and supersedes the addition ofapparent_repo_name
from #1621.Also adds the
_SCALA_IMPORT_RULE_LOAD
constant inscala/scala_maven_import_external.bzl
, which also removes@io_bazel_rules_scala
from theload
ed file path. It now generates the correct path with aLabel
instead.Motivation
The goal is to maintain the ability to generate default target names from a repository name under Bzlmod. While not documented on https://bazel.build, it is documented in the Bazel source itself, even under the current 9.0.0-pre.20241105.2 prerelease.
Under
WORKSPACE
, therepository_ctx.name
value served this purpose directly, as it would reflect thename
value as provided by the caller. Under Bzlmod, this value now contains the canonical repo name, which is different than that provided by thename
parameter. Theapparent_repo_name
utility was my first attempt to resolve this, by parsing the originalname
parameter from the canonicalizedrepository_ctx.name
.I also created bazelbuild/bazel-skylib#548 to contribute
apparent_repo_name
tobazel_skylib
, along with other proposed utilities. I quickly found better solutions to obviate the other utilities, but hung ontoapparent_repo_name
.After that pull request stagnated, I eventually realized a macro wrapper could generate a default target name for a repository_rule by duplicating the
name
attr. This isn't as easy as addingapparent_repo_name(repository_ctx.name)
to a repo rule's implementation, but it's also not that much more work. See also this thread from the #bzlmod channel in the Bazel Slack workspace about generating default repo names.One small downside of this technique is that the macro can't be imported into a
MODULE.bazel
file directly viause_repo_rule
. If you did want to use it inMODULE.bazel
, you'd have to write a module extension to call it, or usemodules.as_extension
frombazel_skylib
. I suppose that's a small price to pay for squashing a canonical repo name format dependency forever.The other small downside may be that the documentation from the original
repository_rule
doesn't automatically convey to the repo wrapper. In this case,_alias_repository
is already internal, and the originaljvm_import_external
didn't have docstrings to begin with.Regarding
_SCALA_IMPORT_RULE_LOAD
, on the one hand, it eliminates string constant duplication. On the other, it's part of my effort to opportunistically remove or replace internal uses of@io_bazel_rules_scala
. This will allow future Bzlmod users to importrules_scala
without having to setrepo_name = "io_bazel_rules_scala"
in thebazel_dep
call.