Skip to content

Commit

Permalink
Bug 1880746 - Part 4: Throw error when same directory is added to DIR…
Browse files Browse the repository at this point in the history
…S twice. r=firefox-build-system-reviewers,glandium

Differential Revision: https://phabricator.services.mozilla.com/D202099
  • Loading branch information
arai-a committed Feb 27, 2024
1 parent 72f0d8a commit 44f9f3d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 17 deletions.
37 changes: 24 additions & 13 deletions python/mozbuild/mozbuild/frontend/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -831,7 +831,7 @@ def __init__(self, config, finder=default_finder):
self.config = config

self._log = logging.getLogger(__name__)
self._read_files = set()
self._read_files = {}
self._execution_stack = []
self.finder = finder

Expand Down Expand Up @@ -1113,25 +1113,27 @@ def _read_mozbuild(self, path, config, descend, metadata):
"Reading file: {path}".format(path=path),
)

if path in self._read_files:
log(
self._log,
logging.WARNING,
"read_already",
{"path": path},
"File already read. Skipping: {path}".format(path=path),
)
return

self._read_files.add(path)

time_start = time.monotonic()

topobjdir = config.topobjdir

relpath = mozpath.relpath(path, config.topsrcdir)
reldir = mozpath.dirname(relpath)

# NOTE: descend case is handled in the loop below.
if not descend:
if relpath in self._read_files:
log(
self._log,
logging.WARNING,
"read_already",
{"path": path},
"File already read. Skipping: {path}".format(path=path),
)
return

self._read_files[relpath] = (relpath, "")

if mozpath.dirname(relpath) == "js/src" and not config.substs.get(
"JS_STANDALONE"
):
Expand Down Expand Up @@ -1234,6 +1236,15 @@ def _read_mozbuild(self, path, config, descend, metadata):
if not descend:
continue

child_relpath = mozpath.relpath(child_path, self.config.topsrcdir)

if child_relpath in self._read_files:
(prev_parent, prev_path) = self._read_files[child_relpath]
raise Exception(
f"File already read. A directory should not be added to DIRS twice: {child_relpath} is referred from {prev_parent} as '{prev_path}', and {relpath} as '{path}'"
)
self._read_files[child_relpath] = (relpath, path)

for res in self.read_mozbuild(
child_path, context.config, metadata=child_metadata
):
Expand Down
16 changes: 12 additions & 4 deletions python/mozbuild/mozbuild/test/frontend/test_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,20 @@ def test_relative_dirs(self):
contexts = list(reader.read_topsrcdir())
self.assertEqual(len(contexts), 3)

def test_repeated_dirs_ignored(self):
# Ensure repeated directories are ignored.
def test_repeated_dirs_error(self):
reader = self.reader("traversal-repeated-dirs")

contexts = list(reader.read_topsrcdir())
self.assertEqual(len(contexts), 3)
with self.assertRaises(BuildReaderError) as bre:
list(reader.read_topsrcdir())

e = bre.exception
self.assertEqual(
e.actual_file, self.file_path("traversal-repeated-dirs", "bar", "moz.build")
)
self.assertIn(
"File already read. A directory should not be added to DIRS twice: foo/moz.build is referred from moz.build as 'foo', and bar/moz.build as '../foo'",
str(e),
)

def test_outside_topsrcdir(self):
# References to directories outside the topsrcdir should fail.
Expand Down

0 comments on commit 44f9f3d

Please sign in to comment.