Skip to content

Conversation

@fmeum
Copy link
Collaborator

@fmeum fmeum commented Dec 26, 2025

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).

@fmeum fmeum requested review from a team, Wyverald and meteorcloudy as code owners December 26, 2025 11:05
@fmeum fmeum requested review from dabanki and removed request for a team December 26, 2025 11:05
@github-actions github-actions bot added team-Configurability platforms, toolchains, cquery, select(), config transitions team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Dec 26, 2025
@fmeum
Copy link
Collaborator Author

fmeum commented Dec 26, 2025

FYI @dzbarsky, this is the missing piece for path mapping support

@fmeum
Copy link
Collaborator Author

fmeum commented Dec 26, 2025

@bazel-io fork 9.0.0

Copy link

@gemini-code-assist gemini-code-assist bot left a 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.

@Wyverald
Copy link
Member

Wyverald commented Jan 5, 2026

The answer is probably obvious, but I'm guessing we decided "native.package_relative_label + rule initializer" is not good enough?

@fmeum
Copy link
Collaborator Author

fmeum commented Jan 5, 2026

The answer is probably obvious, but I'm guessing we decided "native.package_relative_label + rule initializer" is not good enough?

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 attr.string_dict attribute of the rule.

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.""",
Copy link
Member

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.

Copy link
Collaborator Author

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.

@fmeum fmeum requested review from a team, fweikert, gregestren and lberki as code owners January 6, 2026 10:41
fmeum and others added 2 commits January 6, 2026 11:42
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>
@fmeum fmeum force-pushed the ctx.package_relative_label branch from c535b8f to 00d7f4c Compare January 6, 2026 10:42
@fmeum fmeum force-pushed the ctx.package_relative_label branch from 5fdcdc3 to 18f6d10 Compare January 6, 2026 10:52
@fmeum fmeum requested a review from Wyverald January 6, 2026 10:52
@Wyverald Wyverald added awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally and removed awaiting-review PR is awaiting review from an assigned reviewer labels Jan 6, 2026
@copybara-service copybara-service bot closed this in 78265cc Jan 7, 2026
@github-actions github-actions bot removed the awaiting-PR-merge PR has been approved by a reviewer and is ready to be merge internally label Jan 7, 2026
Wyverald pushed a commit that referenced this pull request Jan 7, 2026
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
Wyverald pushed a commit that referenced this pull request Jan 7, 2026
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
github-merge-queue bot pushed a commit that referenced this pull request Jan 8, 2026
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 <fabian@meumertzhe.im>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

team-Configurability platforms, toolchains, cquery, select(), config transitions team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants