Skip to content
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

Add repository_ctx.original_name #25121

Closed
wants to merge 3 commits into from

Conversation

fmeum
Copy link
Collaborator

@fmeum fmeum commented Jan 29, 2025

This new attribute contains the original value of the name attribute at the instantiation site of the repo rule (e.g., rctx.original_name would be foo if rctx.name is +ext+foo).

Fixes #24467

@github-actions github-actions bot added team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file. awaiting-review PR is awaiting review from an assigned reviewer labels Jan 29, 2025
"The name that was originally specified as the 'name' attribute when this repository rule was instantiated. This name is not necessarily unique among external repositories (see 'name').")
public String getOriginalName() {
String originalName = (String) rule.getAttr("$original_name", Type.STRING);
// The original name isn't set for WORKSPACE-defined repositories as well as repositories
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these still a thing at HEAD? What's our strategy for backporting this, should I add new tests to the backports?

@fmeum
Copy link
Collaborator Author

fmeum commented Jan 29, 2025

@Wyverald @meteorcloudy Since rulesets still have to support WORKSPACE at this point, in which rctx.attr.name is a valid apparent repo name, I wonder whether we should instead make it so that:

  • rctx.name returns the canonical repo name (current behavior)
  • rctx.attr.name returns the original value of the name attribute (which equals ctx.name for WORKSPACE repos)

We wouldn't be able to cherry-pick the change, but for backwards compatibility users already need to resort to canonical repo name parsing where necessary (e.g. repo rules meant to be used with use_repo_rule). Advantages are that:

  • It's pretty self-explanatory that rctx.attr.name returns the value of the attribute unmodified.
  • We avoid introducing a third way to get a name for a repo rule.

Implementing this wouldn't be difficult, we could just monkey patch the StructImpl returned by rctx.attr to return the value of rctx.attr.$original_name for rctx.attr.name if set.

@meteorcloudy
Copy link
Member

I don't have strong preference on either way, I trust you and @Wyverald to make the best decision.

But we should definitely document the best practices somewhere, I can follow up on that.

@Wyverald
Copy link
Member

Since rulesets still have to support WORKSPACE at this point, in which rctx.attr.name is a valid apparent repo name, [...]

I don't quite follow this logic -- currently, in either Bzlmod or WORKSPACE, it's always true that rctx.name == rctx.attr.name. Changing the behavior of either one at this point would be breaking, and for unclear benefits.

Although TBH I agree that if we were to start from scratch, having the rctx.name vs rctx.attr.name dichotomy would've been nice.

@fmeum
Copy link
Collaborator Author

fmeum commented Jan 29, 2025

I don't quite follow this logic -- currently, in either Bzlmod or WORKSPACE, it's always true that rctx.name == rctx.attr.name

They are equal today, but I doubt anyone relies on that property. What I (badly) described above is that a repo rule today must be prepared to receive both foo (with WORKSPACE) and +ext+foo (with Bzlmod) in whichever of the two it uses. Changing one of them to be of the form foo all the time thus shouldn't be that breaking in practice.

If you don't think that's doable, I'm also happy to go with the approach that's currently implemented by this PR.

@Wyverald
Copy link
Member

What I meant is that, technically, a repo rule could be relying on the semantics that rctx.name (and rctx.attr.name) refer to the canonical name of the repo (which has always been true in Bzlmod or WORKSPACE). For such a repo rule, it doesn't matter whether rctx.name is foo or +repo_rules3_foo or 8dwfu91e; it just needs @@${rctx.name}//:blah to be a valid label referring to itself.

With that said, I am struggling to think of a realistic example of such a repo rule... but IMO introducing an explicit API could avoid some headache in the future. Not a super strong opinion.

@fmeum
Copy link
Collaborator Author

fmeum commented Jan 30, 2025

We aren't locking us in, we could still change the behavior of ctx.attr.name in the future. I'm okay with the PR as is.

@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 30, 2025
@fmeum
Copy link
Collaborator Author

fmeum commented Jan 30, 2025

@bazel-io fork 8.1.0

@iancha1992
Copy link
Member

@fmeum can you please resolve the conflicts? Thanks

fmeum added 2 commits January 31, 2025 09:33
This new attribute contains the original value of the `name` attribute at the instantiation site of the repo rule (e.g., `rctx.original_name` would be `foo` if `rctx.name` is `+ext+foo`).

# Conflicts:
#	src/test/java/com/google/devtools/build/lib/bazel/bzlmod/ModuleExtensionResolutionTest.java
@fmeum fmeum force-pushed the 24467-original-name branch from f992e42 to 6c65262 Compare January 31, 2025 08:38
@fmeum
Copy link
Collaborator Author

fmeum commented Jan 31, 2025

@iancha1992 Done!

@satyanandak
Copy link

@fmeum could you please take a look at the presubmit failures?

@fmeum fmeum force-pushed the 24467-original-name branch from 6c65262 to a50b755 Compare January 31, 2025 10:21
@fmeum
Copy link
Collaborator Author

fmeum commented Jan 31, 2025

@satyanandak Fixed

@copybara-service copybara-service bot closed this in 8bcfb06 Feb 3, 2025
@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 Feb 3, 2025
@fmeum fmeum deleted the 24467-original-name branch February 3, 2025 19:49
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 4, 2025
This new attribute contains the original value of the `name` attribute at the instantiation site of the repo rule (e.g., `rctx.original_name` would be `foo` if `rctx.name` is `+ext+foo`).

Fixes bazelbuild#24467

Closes bazelbuild#25121.

RELNOTES: Added `repository_ctx.original_name`, which contains the original value of the `name` attribute as specified at the repo rule call site.
PiperOrigin-RevId: 722731393
Change-Id: I2f7dada0c44b6bd4c0d2622fa1e97223382a8547
(cherry picked from commit 8bcfb06)
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 4, 2025
This new attribute contains the original value of the `name` attribute at the instantiation site of the repo rule (e.g., `rctx.original_name` would be `foo` if `rctx.name` is `+ext+foo`).

Fixes bazelbuild#24467

Closes bazelbuild#25121.

RELNOTES: Added `repository_ctx.original_name`, which contains the original value of the `name` attribute as specified at the repo rule call site.
PiperOrigin-RevId: 722731393
Change-Id: I2f7dada0c44b6bd4c0d2622fa1e97223382a8547
(cherry picked from commit 8bcfb06)
fmeum added a commit to fmeum/bazel that referenced this pull request Feb 4, 2025
This new attribute contains the original value of the `name` attribute at the instantiation site of the repo rule (e.g., `rctx.original_name` would be `foo` if `rctx.name` is `+ext+foo`).

Fixes bazelbuild#24467

Closes bazelbuild#25121.

RELNOTES: Added `repository_ctx.original_name`, which contains the original value of the `name` attribute as specified at the repo rule call site.
PiperOrigin-RevId: 722731393
Change-Id: I2f7dada0c44b6bd4c0d2622fa1e97223382a8547
(cherry picked from commit 8bcfb06)
github-merge-queue bot pushed a commit that referenced this pull request Feb 5, 2025
This new attribute contains the original value of the `name` attribute
at the instantiation site of the repo rule (e.g., `rctx.original_name`
would be `foo` if `rctx.name` is `+ext+foo`).

Fixes #24467

Closes #25121.

RELNOTES: Added `repository_ctx.original_name`, which contains the
original value of the `name` attribute as specified at the repo rule
call site.

PiperOrigin-RevId: 722731393
Change-Id: I2f7dada0c44b6bd4c0d2622fa1e97223382a8547 
(cherry picked from commit 8bcfb06)

Fixes #25147
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team-ExternalDeps External dependency handling, remote repositiories, WORKSPACE file.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add a repository_ctx field to accommodate default repo target names
5 participants