-
Notifications
You must be signed in to change notification settings - Fork 4.4k
Add build_workspace_root to repository_ctx #13417
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 build_workspace_root to repository_ctx #13417
Conversation
2b36629 to
4f21494
Compare
|
@philwo I've just pushed a rebase, if it's possible to get a review that's be really handy, it's a small but very handy PR! |
|
@illicitonion would you be able to resolve the conflicts here? @philwo @Wyverald @meteorcloudy could someone take a look at the change? It'd be nice to get this in. |
4f21494 to
c9a6435
Compare
|
Rebased :) |
|
@Wyverald WDYT? I guess this should also work with Bzlmod? |
| } | ||
|
|
||
| @StarlarkMethod( | ||
| name = "build_workspace_directory", |
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.
this concept is usually referred to as the "workspace root". Could you update the name throughout this PR?
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 what it's worth, build_workspace_directory makes sense to me and matches the related environment variable that's set on executable targets:
https://docs.bazel.build/versions/main/user-manual.html#run
The following extra environment variables are also available to the binary:
BUILD_WORKSPACE_DIRECTORY: the root of the workspace where the build was run.
BUILD_WORKING_DIRECTORY: the current working directory where Bazel was run from.
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.
Done - thanks!
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.
Obviously we have a disagreement in Bazel itself, but I think it's more important to keep it consistent among Bazel's Starlark APIs. Can you rename it to just workspace_root?
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.
sorry to be nitpicky -- is there a reason why "build" is in the name? I kind of thought ctx.workspace_root would work well.
Also thanks @UebelAndre for the context -- I wasn't aware of the env variables. It makes more sense that those names include "BUILD", since env variables aren't Bazel-specific; but with the ctx object, we're obviously in Bazel.
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.
@meteorcloudy Oh boy, Label.workspace_root has a completely different meaning, though... I think we should deprecate the Label.workspace_* fields and replace them with Label.repo_* or something. The terms "repo" and "workspace" used to be used interchangeably but now we mostly think of the "workspace" as the overall space we work in, housing the main repo and the various external repos.
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.
Removed the build :)
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.
Label.workspace_root has a completely different meaning, though...
Indeed, it's actually the workspace root (which contains the WORKSPACE file) of the repo of the label, repo_root is much better.
c9a6435 to
dc03879
Compare
dc03879 to
7ac28af
Compare
There are a number of use-cases for this, and I've seen it come up in
`bazel-discuss` and similar. It is possible to abuse this information,
but this information is currently available by looking up something like:
```python
repository_ctx.path(Label("@//:WORKSPACE")).dirname
```
But this is unreliable as `WORKSPACE.bazel` was added as an alternative
to `WORKSPACE`, and trying to look up a label which doesn't exist is a
fatal error.
We're currently using this to work around the fact that labels can't
exist if the file they point at doesn't already exist.
In repository rules we want users to be able to set an attribute to
point at a lockfile, which may not yet exist, and which our rule may
create as a side-effect of running. The nicest way we've worked out how
to do this is to use a `attr.string`, and use `repository_ctx.execute`
to test for the existence of a file, but that relies on being able to
find the path to the root repository.
It may be interesting to try to introduce some kind of fallible
label/path lookup, but that feels like a much bigger change for the
future.
7ac28af to
948a2f4
Compare
|
@bazel-io flag |
|
@bazel-io fork 5.2.0 |
There are a number of use-cases for this, and I've seen it come up in
`bazel-discuss` and similar. It is possible to abuse this information,
but this information is currently available by looking up something like:
```python
repository_ctx.path(Label("@//:WORKSPACE")).dirname
```
But this is unreliable as `WORKSPACE.bazel` was added as an alternative
to `WORKSPACE`, and trying to look up a label which doesn't exist is a
fatal error.
We're currently using this to work around the fact that labels can't
exist if the file they point at doesn't already exist.
In repository rules we want users to be able to set an attribute to
point at a lockfile, which may not yet exist, and which our rule may
create as a side-effect of running. The nicest way we've worked out how
to do this is to use a `attr.string`, and use `repository_ctx.execute`
to test for the existence of a file, but that relies on being able to
find the path to the root repository.
It may be interesting to try to introduce some kind of fallible
label/path lookup, but that feels like a much bigger change for the
future.
Closes #13417.
PiperOrigin-RevId: 442839850
Co-authored-by: Daniel Wagner-Hall <[email protected]>
There are a number of use-cases for this, and I've seen it come up in
bazel-discussand similar. It is possible to abuse this information,but this information is currently available by looking up something like:
But this is unreliable as
WORKSPACE.bazelwas added as an alternativeto
WORKSPACE, and trying to look up a label which doesn't exist is afatal error.
We're currently using this to work around the fact that labels can't
exist if the file they point at doesn't already exist.
In repository rules we want users to be able to set an attribute to
point at a lockfile, which may not yet exist, and which our rule may
create as a side-effect of running. The nicest way we've worked out how
to do this is to use a
attr.string, and userepository_ctx.executeto test for the existence of a file, but that relies on being able to
find the path to the root repository.
It may be interesting to try to introduce some kind of fallible
label/path lookup, but that feels like a much bigger change for the
future.