Add `package_path` method to File type.
- Dominant language
- Java
- Stars
- 25.8k
- Forks
- 4.6k
- Avg merge
- 2d 20h
- Merged PRs (30d)
- 72
Description
### 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:
```starlark
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.
Contributor guide
Research direction
Start by locating Bazel's Starlark File type and the existing short_path, owner, and owner.package behavior. Add the requested package-relative path behavior, including the no-owner fallback described in the issue, then find or add focused coverage for both cases and verify the Starlark API result.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- build-system
- Issue type
- Feature
- Difficulty
- 3/5
- Estimated time
- 1-2 days
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 63/100