Skip to content

Commit 76e04d0

Browse files
committed
Bring back the assertion
1 parent f0558a1 commit 76e04d0

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

python/runfiles/runfiles.py

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ def lookup(self, source_repo: str, target_apparent: str) -> Optional[str]:
129129
# No mapping found
130130
return None
131131

132+
def is_empty(self) -> bool:
133+
"""Check if this repository mapping is empty (no exact or prefixed mappings).
134+
135+
Returns:
136+
True if there are no mappings, False otherwise
137+
"""
138+
return len(self._exact_mappings) == 0 and len(self._prefixed_mappings) == 0
139+
132140

133141
class _ManifestBased:
134142
"""`Runfiles` strategy that parses a runfiles-manifest to look up runfiles."""
@@ -286,7 +294,7 @@ def Rlocation(self, path: str, source_repo: Optional[str] = None) -> Optional[st
286294
if os.path.isabs(path):
287295
return path
288296

289-
if source_repo is None and self._repo_mapping is not None:
297+
if source_repo is None and not self._repo_mapping.is_empty():
290298
# Look up runfiles using the repository mapping of the caller of the
291299
# current method. If the repo mapping is empty, determining this
292300
# name is not necessary.
@@ -295,11 +303,20 @@ def Rlocation(self, path: str, source_repo: Optional[str] = None) -> Optional[st
295303
# Split off the first path component, which contains the repository
296304
# name (apparent or canonical).
297305
target_repo, _, remainder = path.partition("/")
298-
if not remainder:
299-
# path did not contain a slash and referred to a root symlink,
300-
# which should not be mapped.
306+
if not remainder or self._repo_mapping.lookup(source_repo, target_repo) is None:
307+
# One of the following is the case:
308+
# - not using Bzlmod, so the repository mapping is empty and
309+
# apparent and canonical repository names are the same
310+
# - target_repo is already a canonical repository name and does not
311+
# have to be mapped.
312+
# - path did not contain a slash and referred to a root symlink,
313+
# which also should not be mapped.
301314
return self._strategy.RlocationChecked(path)
302315

316+
assert (
317+
source_repo is not None
318+
), "BUG: if the `source_repo` is None, we should never go past the `if` statement above"
319+
303320
# Look up the target repository using the repository mapping
304321
if source_repo is not None:
305322
target_canonical = self._repo_mapping.lookup(source_repo, target_repo)

tests/runfiles/runfiles_test.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -683,6 +683,10 @@ def testRepositoryMappingLookup(self) -> None:
683683
# Test empty mapping
684684
empty_mapping = _RepositoryMapping({}, {})
685685
self.assertIsNone(empty_mapping.lookup("any", "repo"))
686+
687+
# Test is_empty() method
688+
self.assertFalse(repo_mapping.is_empty()) # Should have mappings
689+
self.assertTrue(empty_mapping.is_empty()) # Should be empty
686690

687691
def testCurrentRepository(self) -> None:
688692
# Under bzlmod, the current repository name is the empty string instead

0 commit comments

Comments
 (0)