Skip to content

Commit

Permalink
Update runfiles lib (#982)
Browse files Browse the repository at this point in the history
* runfiles: Find runfiles in directories that are themselves runfiles

Cherry-picks a fix added to the Bazel version of the runfiles library in
bazelbuild/bazel@486d153

* runfiles: Add tests from Bazel
  • Loading branch information
fmeum authored Jan 12, 2023
1 parent 70cce26 commit 7e2d4ec
Show file tree
Hide file tree
Showing 3 changed files with 398 additions and 1 deletion.
17 changes: 16 additions & 1 deletion python/runfiles/runfiles.py
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,22 @@ def __init__(self, path):

def RlocationChecked(self, path):
# type: (str) -> Optional[str]
return self._runfiles.get(path)
"""Returns the runtime path of a runfile."""
exact_match = self._runfiles.get(path)
if exact_match:
return exact_match
# If path references a runfile that lies under a directory that
# itself is a runfile, then only the directory is listed in the
# manifest. Look up all prefixes of path in the manifest and append
# the relative path from the prefix to the looked up path.
prefix_end = len(path)
while True:
prefix_end = path.rfind("/", 0, prefix_end - 1)
if prefix_end == -1:
return None
prefix_match = self._runfiles.get(path[0:prefix_end])
if prefix_match:
return prefix_match + "/" + path[prefix_end + 1 :]

@staticmethod
def _LoadRunfiles(path):
Expand Down
7 changes: 7 additions & 0 deletions tests/runfiles/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
load("@rules_python//python:defs.bzl", "py_test")

py_test(
name = "runfiles_test",
srcs = ["runfiles_test.py"],
deps = ["//python/runfiles"],
)
Loading

0 comments on commit 7e2d4ec

Please sign in to comment.