Skip to content

Conversation

@ckolli5
Copy link

@ckolli5 ckolli5 commented 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:

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

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
@ckolli5 ckolli5 merged commit 8d510ec into bazelbuild:release-5.2.0 May 10, 2022
coeuvre added a commit to coeuvre/bazel that referenced this pull request May 10, 2022
coeuvre added a commit to coeuvre/bazel that referenced this pull request May 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants