-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add ctx.package_relative_label
#28102
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
|
FYI @dzbarsky, this is the missing piece for path mapping support |
|
@bazel-io fork 9.0.0 |
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 introduces ctx.package_relative_label, a new function for creating Label objects relative to the current rule's package, which is a valuable addition for Starlark rule authors. The implementation is generally sound and the accompanying tests are thorough. However, I've identified a potential issue where a missing mutability check could lead to a NullPointerException if the context is accessed after its intended lifecycle. I've provided a comment with a suggested fix for this.
src/main/java/com/google/devtools/build/lib/analysis/starlark/StarlarkRuleContext.java
Show resolved
Hide resolved
|
The answer is probably obvious, but I'm guessing we decided " |
That combination could be used to parse all relevant attributes once to convert all user-specified labels to canonical form, then forward this mapping via a special public I wouldn't want to encourage this pattern though as it seems overly tricky and unnecessarily inefficient (it parses the attrs twice). The new API has such small scope and mirrors a method that exists in a different context that I believe it's warranted. |
| <p><i>Usage note:</i> The difference between this function and \ | ||
| <a href='../builtins/Label.html#Label'>Label()</a></code> is \ | ||
| that <code>Label()</code> uses the context of the package of the <code>.bzl</code> file \ | ||
| that called it, not the package of the target currently being analyzed.""", |
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.
could you add a note here mentioning that this is effectively the same as native.package_relative_label? and vice versa for the docs of native.package_relative_label, and maybe look at Label() docs where it mentions native.package_relative_label to make it mention both instead.
all in all, it would be nice if our documentation treated the two as equals, instead of one being mentioned everywhere and the other being an afterthought.
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.
I added references to Label, Label#relative and native.package_relative_label.
This is necessary to permit Starlark implementations of `ctx.expand_location`, which can be more memory efficient, support path mapping and provide better defaults. RELNOTES: The new `package_relative_label` function on the rule context (`ctx`) can be used to turn a user-provided label string into a `Label` relative to the target that is currently being analyzed (where `Label(...)` would return a `Label` relative to the `.bzl` file containing the call).
…StarlarkRuleContext.java Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
c535b8f to
00d7f4c
Compare
5fdcdc3 to
18f6d10
Compare
This is necessary to permit Starlark implementations of `ctx.expand_location`, which can be more memory efficient, support path mapping and provide better defaults. RELNOTES: The new `package_relative_label` function on the rule context (`ctx`) can be used to turn a user-provided label string into a `Label` relative to the target that is currently being analyzed (where `Label(...)` would return a `Label` relative to the `.bzl` file containing the call). Closes #28102. PiperOrigin-RevId: 853170854 Change-Id: Ibecff3b0b599da0be2fbbba707c15ed540c6c38c
This is necessary to permit Starlark implementations of `ctx.expand_location`, which can be more memory efficient, support path mapping and provide better defaults. RELNOTES: The new `package_relative_label` function on the rule context (`ctx`) can be used to turn a user-provided label string into a `Label` relative to the target that is currently being analyzed (where `Label(...)` would return a `Label` relative to the `.bzl` file containing the call). Closes #28102. PiperOrigin-RevId: 853170854 Change-Id: Ibecff3b0b599da0be2fbbba707c15ed540c6c38c
This is necessary to permit Starlark implementations of `ctx.expand_location`, which can be more memory efficient, support path mapping and provide better defaults. RELNOTES: The new `package_relative_label` function on the rule context (`ctx`) can be used to turn a user-provided label string into a `Label` relative to the target that is currently being analyzed (where `Label(...)` would return a `Label` relative to the `.bzl` file containing the call). Closes #28102. PiperOrigin-RevId: 853170854 Change-Id: Ibecff3b0b599da0be2fbbba707c15ed540c6c38c Co-authored-by: Fabian Meumertzheim <[email protected]>
This is necessary to permit Starlark implementations of
ctx.expand_location, which can be more memory efficient, support path mapping and provide better defaults.RELNOTES: The new
package_relative_labelfunction on the rule context (ctx) can be used to turn a user-provided label string into aLabelrelative to the target that is currently being analyzed (whereLabel(...)would return aLabelrelative to the.bzlfile containing the call).