-
Notifications
You must be signed in to change notification settings - Fork 4.4k
[9.0.0] Support {name} and {version} placeholders in use_repo
#28016
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
Conversation
This allows module extensions to reliably namespace repos created by individual modules, even in the presence of `multiple_version_override`, without causing additional churn for users during version bumps. In particular, projects with a publishing workflow that patches in the `version` attribute of the `module` function don't need to modify that workflow to e.g. patch a global constant or also search for the old version in `use_repo` call arguments. Context: https://bazelbuild.slack.com/archives/C09E58X3AQ7/p1764934858912079 RELNOTES: The values of keyword arguments passed to `use_repo` can now contain the special substrings `{name}` and `{version}`, which are treated as equivalent to the corresponding attributes of the current module. Closes bazelbuild#27890. PiperOrigin-RevId: 845156030 Change-Id: Ic5d777645bf6a5ca67fa6cb577f426c96beda894
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request adds support for {name} and {version} placeholders in use_repo keyword arguments. This is a useful feature for namespacing repositories. The implementation is straightforward and well-tested. I've suggested extending this support to positional arguments for consistency.
| for (String arg : Sequence.cast(args, String.class, "args")) { | ||
| extensionProxy.addImport(arg, arg, "by a use_repo() call", stack); | ||
| } | ||
| String moduleName = context.getModuleBuilder().getName(); | ||
| String moduleVersion = context.getModuleBuilder().getVersion().normalized(); | ||
| for (Map.Entry<String, String> entry : | ||
| Dict.cast(kwargs, String.class, String.class, "kwargs").entrySet()) { | ||
| extensionProxy.addImport(entry.getKey(), entry.getValue(), "by a use_repo() call", stack); | ||
| extensionProxy.addImport( | ||
| entry.getKey(), | ||
| entry.getValue().replace("{name}", moduleName).replace("{version}", moduleVersion), | ||
| "by a use_repo() call", | ||
| stack); | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency, the placeholder substitution for {name} and {version} should also be applied to positional arguments in use_repo. Currently, it's only done for keyword arguments. This would make the feature more complete and predictable for users.
If you apply this change, please also consider adding a test case for positional arguments to the useRepo_placeholders test.
String moduleName = context.getModuleBuilder().getName();
String moduleVersion = context.getModuleBuilder().getVersion().normalized();
for (String arg : Sequence.cast(args, String.class, "args")) {
String substitutedArg = arg.replace("{name}", moduleName).replace("{version}", moduleVersion);
extensionProxy.addImport(substitutedArg, substitutedArg, "by a use_repo() call", stack);
}
for (Map.Entry<String, String> entry :
Dict.cast(kwargs, String.class, String.class, "kwargs").entrySet()) {
extensionProxy.addImport(
entry.getKey(),
entry.getValue().replace("{name}", moduleName).replace("{version}", moduleVersion),
"by a use_repo() call",
stack);
}8330f7d
This allows module extensions to reliably namespace repos created by individual modules, even in the presence of
multiple_version_override, without causing additional churn for users during version bumps. In particular, projects with a publishing workflow that patches in theversionattribute of themodulefunction don't need to modify that workflow to e.g. patch a global constant or also search for the old version inuse_repocall arguments.Context: https://bazelbuild.slack.com/archives/C09E58X3AQ7/p1764934858912079
RELNOTES: The values of keyword arguments passed to
use_repocan now contain the special substrings{name}and{version}, which are treated as equivalent to the corresponding attributes of the current module.Closes #27890.
PiperOrigin-RevId: 845156030
Change-Id: Ic5d777645bf6a5ca67fa6cb577f426c96beda894
Commit 7ca1159