bazel-skylib icon indicating copy to clipboard operation
bazel-skylib copied to clipboard

Proposal: Function to get runfiles path for `rlocation`

Open fmeum opened this issue 4 years ago • 0 comments

Now that Bazel ships libraries for all major languages that make it easy to access runfiles by their "runfiles-root-relative path" via the rlocation function, it would be very useful if skylib had a function that provided this path for a given runfile.

While such a function is quick to implement (see below), it would save developers from having to understand the subtle difference between the runfiles root (relative to which rlocation expects its argument) and the working directory of the binary (relative to which File.short_path should be interpreted). This has caused quite a bit of confusion (see e.g. https://github.com/bazelbuild/bazel/issues/1462, https://github.com/bazelbuild/bazel/issues/8598 and https://groups.google.com/g/bazel-discuss/c/1lX3aiTZX3Y/m/7pkXO7OqBwAJ) and outdated information (https://groups.google.com/g/bazel-discuss/c/uvwAQqWthaE/m/dMfV9f01FQAJ) already.

I think that an implementation of this function could look as follows (more readable):

def rlocation_path(ctx, file):
  return paths.normalize(ctx.workspace_name + "/" + file.short_path)

or like this (potentially more efficient):

def rlocation_path(ctx, file):
  if file.short_path.startswith("../"):
    return file.short_path[3:]
  else
    return ctx.workspace_name + "/" + file.short_path

Such a function may fit into paths, but could of course also become part of a new module (perhaps runfiles or files). If you think that this would make for a valuable addition to skylib, I would happily prepare a PR with docs and tests.

fmeum avatar Jun 03 '21 18:06 fmeum