bazel icon indicating copy to clipboard operation
bazel copied to clipboard

Add `package_path` method to File type.

Open tmccombs opened this issue 2 years ago • 0 comments

Description of the feature request:

I would like a simple way to get the path of a file relative to the package it comes from.

Something like a method on File, that return the path relative to package directory that owns the file, or returns short_path if there is no owner.

Which category does this issue belong to?

Core

What underlying problem are you trying to solve with this feature?

The problem I ran into that caused me to want this is I am trying to create a zip file containing a python package. And I want the directory structure of the zip file to match the directory structure underneath the package where they python sources are contained. So I would like a way to get the path of a File relative to its owning package.

I think that there are probably also many other use cases for this as well .

Which operating system are you running Bazel on?

linux

What is the output of bazel info release?

release 6.3.2

If bazel info release returns development version or (@non-git), tell us how you built Bazel.

No response

What's the output of git remote get-url origin; git rev-parse master; git rev-parse HEAD ?

No response

Have you found anything relevant by searching the web?

not really, although it is kind of hard to search for.

Any other information, logs, or outputs that you want to share?

It is possible to accomplish this in starlark with something like:

path = f.short_path
if f.owner and path.startswith(f.owner.package + "/"):
    path = path[len(f.owner.package) + 1:]

But that is very verbose, somewhat complicated, and not very discoverable. It took me quite a while to find that solution.

tmccombs avatar Oct 09 '23 17:10 tmccombs