Skip to content

Commit d780fe4

Browse files
authored
Merge pull request #196 from Peter554/fix-issue-195
Fix handling of within-descendant-imports when squashing modules
2 parents f6c537e + 650eb7e commit d780fe4

File tree

3 files changed

+29
-5
lines changed

3 files changed

+29
-5
lines changed

CHANGELOG.rst

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
Changelog
33
=========
44

5+
Unreleased
6+
----------
7+
8+
* Fixed handling of within-descendant-imports when squashing modules (see `Issue 195 <https://github.com/seddonym/grimp/issues/195>`_).
9+
510
3.7 (2025-03-07)
611
----------------
712

rust/src/graph/graph_manipulation.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,6 @@ impl Graph {
178178
})
179179
.collect();
180180

181-
// Remove any descendants.
182-
for descendant in descendants {
183-
self.remove_module(descendant);
184-
}
185-
186181
// Add descendants and imports to parent module.
187182
for imported in modules_imported_by_descendants {
188183
self.add_import(module, imported);
@@ -192,6 +187,11 @@ impl Graph {
192187
self.add_import(importer, module);
193188
}
194189

190+
// Remove any descendants.
191+
for descendant in descendants {
192+
self.remove_module(descendant);
193+
}
194+
195195
self.mark_module_squashed(module);
196196
}
197197
}

tests/unit/adaptors/graph/test_manipulation.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -447,3 +447,22 @@ def test_raises_module_not_present_if_no_module(self):
447447

448448
with pytest.raises(ModuleNotPresent):
449449
graph.squash_module("foo")
450+
451+
def test_correctly_handles_imports_within_descendants(self):
452+
graph = ImportGraph()
453+
454+
graph.add_module("animals")
455+
graph.add_module("food")
456+
graph.add_import(importer="animals.dog", imported="food.chicken")
457+
graph.add_import(importer="app.cli", imported="animals.dog")
458+
# We want to check that this import within the descendants of `animals` does
459+
# not cause problems. If this import is not properly removed then the imports map can
460+
# become corrupted, since it will contain an import to modules that no longer exist.
461+
# See https://github.com/seddonym/grimp/issues/195 for more details.
462+
graph.add_import(importer="animals.dog", imported="animals.base")
463+
464+
graph.squash_module("animals")
465+
466+
# If the `animals` module was squashed correctly then the following calls should not panic.
467+
assert graph.find_modules_directly_imported_by("animals") == {"food.chicken"}
468+
assert graph.find_modules_that_directly_import("animals") == {"app.cli"}

0 commit comments

Comments
 (0)