Add support for mapped repository names in use_repo_add#1427
Conversation
Summary of ChangesHello @malt3, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request significantly enhances the Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
The pull request enhances the use_repo_add command in buildozer to support specifying repositories using simple names, valid keyword arguments (e.g., foo=bar), and invalid keyword arguments (e.g., foo.2=bar) which are handled via dictionary unpacking. This involved a significant refactoring of the AddRepoUsages function in bzlmod.go to parse these different formats, manage conflicts by replacing existing mappings, and correctly generate the use_repo call arguments. New helper functions (parseRepoMapping, isValidIdentifier, addOrExtendDictUnpack) were introduced, and the README.md was updated to document these new capabilities. Corresponding unit tests were added to cover these new scenarios, including mixed argument types, extending existing dict unpacking, and conflict resolution. Review comments indicate that the refactored AddRepoUsages function is still too complex and suggest further extraction of logic into smaller helper functions, and also point out redundant fields (isDictUnpacking) and unused variables (positionalValuesToAdd) that should be removed.
949021f to
a0739fd
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces support for mapped repository names in use_repo_add, a valuable enhancement for buildozer. The implementation is well-structured, breaking down the logic into several helper functions, and includes comprehensive tests and documentation updates. My review identifies a couple of critical issues that could lead to a panic, a high-severity issue where invalid Starlark code could be generated, and a few medium-severity suggestions for improving code clarity, robustness against malformed input, and efficiency.
d6caed2 to
fdd17c3
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request introduces valuable support for mapped repository names in use_repo_add, which is a significant improvement. The implementation is well-designed, breaking down the complex logic of AddRepoUsages into smaller, understandable helper functions. The handling of simple names, mapped names with valid identifiers, and mapped names with invalid identifiers (using dictionary unpacking) is comprehensive. The logic for removing conflicting repository definitions before adding new ones is also well-executed. The test coverage is extensive, and the documentation updates are clear and helpful. I have one suggestion to enhance the validation of Starlark identifiers.
fdd17c3 to
3466e1b
Compare
|
/gemini review |
There was a problem hiding this comment.
Code Review
This pull request adds support for mapped repository names in use_repo_add, including simple names, mapped names with valid identifiers, and mapped names with invalid identifiers using dictionary unpacking. The implementation is well-structured, breaking down the logic into smaller, manageable functions. The accompanying tests are comprehensive, covering a wide range of scenarios. I've added a couple of suggestions to improve input validation for robustness. Overall, this is a great enhancement.
3466e1b to
6e3ad9a
Compare
197adae to
0cd76d8
Compare
|
@oreflow gentle ping. This could unblock a change in Bazel (see bazelbuild/bazel#27891). |
There was a problem hiding this comment.
Starting off with high level topics before going into code details.
**kwargs support bar=baz -> use_repo(ext, bar = "baz")
This part of the change looks good overall, but there's an implicit assumption added that if calling use_repo_add with an existing key, the argument is replaced, which is inconsistent with other buildozer behaviors as add and dict_add both state that "If the key was already present, it will not be overwritten." (see difference between dict_set and dict_add).
foo.2=foo -> use_repo(ext, **{"foo.2": "foo"})
This part of the change adds a fair bit of complexity in parsing and validation, which also in their implementation require embedding part of the Starlark language specification in this repo. Just as an example, the list of reserved Bazel keywords does not seem to match https://github.com/bazelbuild/starlark/blob/master/spec.md#built-in-constants-and-functions, and it would be infeasible for us to maintain this in the buildtools repo as the starlark language evolves. So if this is kept we should look into using some authorative source to import on this rather than hardcoding it in this codebase.
Do you have a specific use case in mind where you would need to add repos with names matching matches reserved keywords? It seems a bit like this should just be discouraged, even if it would be possible using dict unpacking it seems better if this would return an error.
Additionally behaviors of Buildozer are generally not trying to be smart, so the conditional conversion of reserved / invalid identifiers into dict unpacking might be confusing to users. It might be simpler to place this in the hands of the users, e.g. if a user really wants to add dict-unpacked-values with use_repo_add, it might be more intuitive to support a syntax like use_repo_add {"foo": "bar"}, rather than doing a conversion based on logic here.
|
Thank you for taking a look!
I agree that this is inconsistent. That being said, replacing existing keys is the behavior we want from
I was merely trying to support the full set of possible repo names according to Bazel's documentation. I do agree that this introduces a lot of complexity. I'll simplify and adapt this PR based on your feedback. |
I would go with
I believe the mapping addition to
I think the Error approach is good, however this would still require keeping the parsing and validation, I would even go as far as just allowing users to set mappings with invalid names, as mentioned Buildozer doesn't really deal with validation / opinionated / smart functionality. So if someone explicitly tells Buildozer to do something which will be invalid, I believe we can simply allow them to do so to keep the functionality simple and intuitive, in general Buildozer is more just for the convenience of automated edits, rather than validation. Sidenote, If we wanted validation it might make sense to add a Buildifier warning for any re-use of reserved keywords / invalid identifiers (IIRC this doesn't exist already). That way it would also support a lot more use cases. But not really tied to this PR Thanks for following up so actively and working on this :) |
Repositories can now be imported under a custom local name using the "local=exported" syntax, e.g. `use_repo_add ext bar=baz` generates `use_repo(ext, bar = "baz")`. A mapping whose local name equals its value (`foo=foo`) is simplified to the positional form. Add a `use_repo_set` command that replaces existing imports sharing a local name or repository value with one of the given repos (e.g. to rename a mapping), while `use_repo_add` leaves already-present arguments untouched, consistent with the `add`/`dict_add` commands. Both delegate to a single internal implementation. Buildozer does not validate that local names are valid Starlark identifiers; invalid names are emitted verbatim rather than converted to dict-unpacking syntax.
0cd76d8 to
4ac326a
Compare
|
Hi @oreflow. Apologies for dropping the ball on this. |
Add support for importing repositories under a custom local name, and add a
new use_repo_set command.
Repositories can be specified as:
repository baz under the local name bar. A mapping whose local name equals
its value (foo=foo) is simplified to foo.
use_repo_add leaves already-present arguments untouched (existing mappings are
never overwritten), consistent with the add/dict_add commands. use_repo_set
replaces any existing import that shares a local name or repository value with
one of the given repos (e.g. to rename a mapping). Both delegate to a single
internal implementation.
Buildozer does not validate that local names are valid Starlark identifiers;
invalid names are emitted verbatim.