-
Notifications
You must be signed in to change notification settings - Fork 284
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
Replace apparent_repo_name with repo rule wrappers #1650
Merged
liucijus
merged 1 commit into
bazelbuild:master
from
mbland:bzlmod-replace-apparent-repo-name-with-repo-rule-wrappers
Nov 27, 2024
Merged
Replace apparent_repo_name with repo rule wrappers #1650
liucijus
merged 1 commit into
bazelbuild:master
from
mbland:bzlmod-replace-apparent-repo-name-with-repo-rule-wrappers
Nov 27, 2024
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 bazelbuild#1482, and supersedes the addition of `apparent_repo_name` from bazelbuild#1621. Also adds the `_SCALA_IMPORT_RULE_LOAD` constant in `scala/scala_maven_import_external.bzl`, which also removes `@io_bazel_rules_scala` from the `load`ed file path. It now generates the correct path with a `Label` instead. --- 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: - https://github.com/bazelbuild/bazel/blob/9.0.0-pre.20241105.2/src/main/java/com/google/devtools/build/lib/cmdline/LabelParser.java#L99 Under `WORKSPACE`, the `repository_ctx.name` value served this purpose directly, as it would reflect the `name` value as provided by the caller. Under Bzlmod, this value now contains the canonical repo name, which is different than that provided by the `name` parameter. The `apparent_repo_name` utility was my first attempt to resolve this, by parsing the original `name` parameter from the canonicalized `repository_ctx.name`. I also created bazelbuild/bazel-skylib#548 to contribute `apparent_repo_name` to `bazel_skylib`, along with other proposed utilities. I quickly found better solutions to obviate the other utilities, but hung onto `apparent_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 adding `apparent_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: - https://bazelbuild.slack.com/archives/C014RARENH0/p1730909824656159 --- One small downside of this technique is that the macro can't be imported into a `MODULE.bazel` file directly via `use_repo_rule`. If you did want to use it in `MODULE.bazel`, you'd have to write a module extension to call it, or use `modules.as_extension` from `bazel_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 original `jvm_import_external` didn't have docstrings to begin with.
9ae5efe
to
1cd8245
Compare
simuons
approved these changes
Nov 26, 2024
liucijus
approved these changes
Nov 27, 2024
mbland
added a commit
to mbland/rules_scala
that referenced
this pull request
Feb 6, 2025
Prepares for `repository_ctx.original_name` support in Bazel 8.1.0 per bazelbuild/bazel#24467 and bumps Go to 1.23.6. Part of bazelbuild#1482 and bazelbuild#1652. The underlying repo rules will now use `repository_ctx.original_name` if it's available. I've confirmed that these changes use `repository_ctx.original_name` under Bazel 8.1.0rc2 after commenting out the wrappers and restoring the original repo rule symbols. --- This change ensures compatibility with future Bazel releases. New comments indicate where to simplify code after dropping support for Bazel versions that don't have `repository_ctx.original_name`. The Go 1.23.6 bump is a convenience update that isn't essential to the rest of the change. Under Bzlmod, `repository_ctx.name` contains the canonical repository name. This broke the `_alias_repository` and `jvm_import_external` repository rules, which used `repository_ctx.name` to generate default top level target names for their repos. bazelbuild#1650 added wrapper macros passing the original name as an extra attribute to the underlying repo rules as a portable workaround. The Bazel maintainers eventually recognized this as a common use case and added `repository_ctx.original_name` to eliminate the need for such wrappers (eventually). - https://bazel.build/rules/lib/builtins/repository_ctx#name - https://bazel.build/rules/lib/builtins/repository_ctx#original_name
mbland
added a commit
to mbland/rules_scala
that referenced
this pull request
Feb 6, 2025
Prepares for `repository_ctx.original_name` support in Bazel 8.1.0 per bazelbuild/bazel#24467 and bumps Go to 1.23.6. Part of bazelbuild#1482 and bazelbuild#1652. The underlying repo rules will now use `repository_ctx.original_name` if it's available. I've confirmed that these changes use `repository_ctx.original_name` under Bazel 8.1.0rc2 after commenting out the wrappers and restoring the original repo rule symbols. --- This change ensures compatibility with future Bazel releases. New comments indicate where to simplify code after dropping support for Bazel versions that don't have `repository_ctx.original_name`. The Go 1.23.6 bump is a convenience update that isn't essential to the rest of the change. Under Bzlmod, `repository_ctx.name` contains the canonical repository name. This broke the `_alias_repository` and `jvm_import_external` repository rules, which used `repository_ctx.name` to generate default top level target names for their repos. bazelbuild#1650 added wrapper macros passing the original name as an extra attribute to the underlying repo rules as a portable workaround. The Bazel maintainers eventually recognized this as a common use case and added `repository_ctx.original_name` to eliminate the need for such wrappers (eventually). - https://bazel.build/rules/lib/builtins/repository_ctx#name - https://bazel.build/rules/lib/builtins/repository_ctx#original_name
simuons
pushed a commit
that referenced
this pull request
Feb 13, 2025
Prepares for `repository_ctx.original_name` support in Bazel 8.1.0 per bazelbuild/bazel#24467 and bumps Go to 1.23.6. Part of #1482 and #1652. The underlying repo rules will now use `repository_ctx.original_name` if it's available. I've confirmed that these changes use `repository_ctx.original_name` under Bazel 8.1.0rc2 after commenting out the wrappers and restoring the original repo rule symbols. --- This change ensures compatibility with future Bazel releases. New comments indicate where to simplify code after dropping support for Bazel versions that don't have `repository_ctx.original_name`. The Go 1.23.6 bump is a convenience update that isn't essential to the rest of the change. Under Bzlmod, `repository_ctx.name` contains the canonical repository name. This broke the `_alias_repository` and `jvm_import_external` repository rules, which used `repository_ctx.name` to generate default top level target names for their repos. #1650 added wrapper macros passing the original name as an extra attribute to the underlying repo rules as a portable workaround. The Bazel maintainers eventually recognized this as a common use case and added `repository_ctx.original_name` to eliminate the need for such wrappers (eventually). - https://bazel.build/rules/lib/builtins/repository_ctx#name - https://bazel.build/rules/lib/builtins/repository_ctx#original_name
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.