Skip to content

Conversation

@illicitonion
Copy link
Contributor

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:

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.

@google-cla google-cla bot added the cla: yes label Apr 30, 2021
@oquenchil oquenchil added the team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website label May 3, 2021
@illicitonion illicitonion force-pushed the build-workspace-directory branch from 2b36629 to 4f21494 Compare January 4, 2022 17:10
@illicitonion
Copy link
Contributor Author

@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!

@philwo philwo requested a review from Wyverald February 15, 2022 05:05
@philwo philwo assigned meteorcloudy and unassigned philwo Mar 22, 2022
@UebelAndre
Copy link
Contributor

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

@illicitonion illicitonion force-pushed the build-workspace-directory branch from 4f21494 to c9a6435 Compare April 19, 2022 10:59
@illicitonion
Copy link
Contributor Author

Rebased :)

@meteorcloudy
Copy link
Member

@Wyverald WDYT? I guess this should also work with Bzlmod?

}

@StarlarkMethod(
name = "build_workspace_directory",
Copy link
Member

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?

Copy link
Contributor

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done - thanks!

Copy link
Member

Choose a reason for hiding this comment

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

https://docs.bazel.build/versions/main/skylark/lib/Label.html#workspace_root

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?

Copy link
Member

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.

Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed the build :)

Copy link
Member

@meteorcloudy meteorcloudy Apr 19, 2022

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.

@illicitonion illicitonion force-pushed the build-workspace-directory branch from c9a6435 to dc03879 Compare April 19, 2022 15:31
@illicitonion illicitonion changed the title Add build_workspace_directory to repository_ctx Add build_workspace_root to repository_ctx Apr 19, 2022
@illicitonion illicitonion force-pushed the build-workspace-directory branch from dc03879 to 7ac28af Compare April 19, 2022 15:41
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.
@illicitonion illicitonion force-pushed the build-workspace-directory branch from 7ac28af to 948a2f4 Compare April 19, 2022 15:45
@bazel-io bazel-io closed this in 8edf6ab Apr 19, 2022
@brentleyjones
Copy link
Contributor

@bazel-io flag

@bazel-io bazel-io added the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Apr 19, 2022
@ckolli5
Copy link

ckolli5 commented Apr 21, 2022

@bazel-io fork 5.2.0

@bazel-io bazel-io removed the potential release blocker Flagged by community members using "@bazel-io flag". Should be added to a release blocker milestone label Apr 21, 2022
@illicitonion illicitonion deleted the build-workspace-directory branch April 21, 2022 16:00
ckolli5 added a commit that referenced this pull request May 10, 2022
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

cla: yes team-OSS Issues for the Bazel OSS team: installation, release processBazel packaging, website

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants