From e69e049def71c317d5cdc0653d2bb74d72fed497 Mon Sep 17 00:00:00 2001 From: Craig Ulliott Date: Wed, 30 Sep 2026 17:28:17 -0400 Subject: [PATCH] fix: import add-in modules relatively to avoid cross-add-in collisions Fusion runs every add-in in a single Python interpreter. The entry point added the add-in folder to sys.path and imported its code as the top-level package `lib`. Any other add-in with a `lib` package could therefore shadow ConstraintLens's modules, or have its own shadowed, depending on load order. When that happens one add-in runs the other's code: ConstraintLens's lifecycle.start() was observed running against another add-in's folder. Import from the add-in's own package instead (`from .lib import lifecycle`), matching Autodesk's add-in template, and leave sys.path untouched. Nothing under lib/ changes; it already imports relatively. The headless tests now import `ConstraintLens.lib` the way Fusion loads it, and test_entry.py checks the entry point resolves its own `lib` when another add-in's is already imported. --- CLAUDE.md | 1 + ConstraintLens/ConstraintLens.py | 12 +++++++----- tests/headless/_bootstrap.py | 4 +++- tests/headless/test_dimensions.py | 2 +- tests/headless/test_entry.py | 28 ++++++++++++++++++++++++++++ tests/headless/test_labels.py | 2 +- tests/headless/test_lifecycle.py | 2 +- tests/headless/test_scanner.py | 2 +- 8 files changed, 43 insertions(+), 10 deletions(-) create mode 100644 tests/headless/test_entry.py diff --git a/CLAUDE.md b/CLAUDE.md index f26ad52..ace2808 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -219,6 +219,7 @@ tests/ ``` ### Key conventions +- **Imports:** relative within the add-in (`from .lib import …`); nothing touches `sys.path`. Fusion loads the add-in folder as a package and runs every add-in in one interpreter, so an absolute `from lib import …` picks up whichever add-in's `lib` loaded first — this happened for real with a second add-in that also has a `lib` package. `tests/headless/test_entry.py` guards it. - **Collections:** `SketchCurveVector` (from `.parentCurves`, `.childCurves`, `.curves`) uses `len()` + iteration, not `.count`. `ObjectCollection` uses `.count` + `.item(i)`. - **Event handlers:** Always appended to `events._handlers` list (M-7). Never instantiate a handler without pinning it. - **Palette sends:** Always gated on `palette.isVisible` (M-8 guard in `messaging.send()`). diff --git a/ConstraintLens/ConstraintLens.py b/ConstraintLens/ConstraintLens.py index 26c3c60..4ec9b15 100644 --- a/ConstraintLens/ConstraintLens.py +++ b/ConstraintLens/ConstraintLens.py @@ -1,18 +1,20 @@ # ConstraintLens — Fusion 360 add-in entry point (see SPEC.md section 4). # # Owns only run(context) / stop(context). All logic lives under ./lib. +# +# Fusion loads each add-in folder as a package, so the import is relative. Fusion +# runs every add-in in one Python interpreter: an absolute `from lib import …` +# (with the folder added to sys.path) would share a module name with any other +# add-in that has a `lib` package, and whichever loaded first would win. import os -import sys import traceback import adsk.core -_ADDIN_DIR = os.path.dirname(os.path.realpath(__file__)) -if _ADDIN_DIR not in sys.path: - sys.path.insert(0, _ADDIN_DIR) +from .lib import lifecycle -from lib import lifecycle # noqa: E402 +_ADDIN_DIR = os.path.dirname(os.path.realpath(__file__)) def run(context): diff --git a/tests/headless/_bootstrap.py b/tests/headless/_bootstrap.py index 044101b..47d2ef5 100644 --- a/tests/headless/_bootstrap.py +++ b/tests/headless/_bootstrap.py @@ -2,6 +2,8 @@ Imported first by every test module here. The stubs directory has to come before anything else so `import adsk.core` finds the fake rather than failing. +Fusion loads the add-in folder as a package, so tests import +`ConstraintLens.lib…` the same way. """ import os @@ -10,6 +12,6 @@ _HERE = os.path.dirname(os.path.abspath(__file__)) _REPO_ROOT = os.path.dirname(os.path.dirname(_HERE)) -for _path in (os.path.join(_HERE, "stubs"), os.path.join(_REPO_ROOT, "ConstraintLens")): +for _path in (os.path.join(_HERE, "stubs"), _REPO_ROOT): if _path not in sys.path: sys.path.insert(0, _path) diff --git a/tests/headless/test_dimensions.py b/tests/headless/test_dimensions.py index 1ab81ae..1416cd1 100644 --- a/tests/headless/test_dimensions.py +++ b/tests/headless/test_dimensions.py @@ -5,7 +5,7 @@ import _bootstrap # noqa: F401 import fakes -from lib import dispatch, labels, scanner +from ConstraintLens.lib import dispatch, labels, scanner def _linear(expression, value, unit="mm", entities=None): diff --git a/tests/headless/test_entry.py b/tests/headless/test_entry.py new file mode 100644 index 0000000..e1b02c2 --- /dev/null +++ b/tests/headless/test_entry.py @@ -0,0 +1,28 @@ +"""ConstraintLens.py — the entry point loads its own code whatever else Fusion has loaded.""" + +import importlib +import sys +import types +import unittest + +import _bootstrap # noqa: F401 + + +class EntryPointTest(unittest.TestCase): + def test_uses_its_own_lib_when_another_add_in_s_lib_is_already_imported(self): + # Fusion runs every add-in in one interpreter. Another add-in that did + # `from lib import lifecycle` leaves its own `lib` package behind. + other = types.ModuleType("lib") + other.lifecycle = types.ModuleType("lib.lifecycle") + sys.modules.update({"lib": other, "lib.lifecycle": other.lifecycle}) + sys.modules.pop("ConstraintLens.ConstraintLens", None) # import it afresh + try: + entry = importlib.import_module("ConstraintLens.ConstraintLens") + finally: + del sys.modules["lib"], sys.modules["lib.lifecycle"] + + self.assertEqual(entry.lifecycle.__name__, "ConstraintLens.lib.lifecycle") + + +if __name__ == "__main__": + unittest.main() diff --git a/tests/headless/test_labels.py b/tests/headless/test_labels.py index ef14518..bc16bfe 100644 --- a/tests/headless/test_labels.py +++ b/tests/headless/test_labels.py @@ -5,7 +5,7 @@ import _bootstrap # noqa: F401 import fakes -from lib import labels +from ConstraintLens.lib import labels class LabelerTest(unittest.TestCase): diff --git a/tests/headless/test_lifecycle.py b/tests/headless/test_lifecycle.py index 9169bd8..b42ba58 100644 --- a/tests/headless/test_lifecycle.py +++ b/tests/headless/test_lifecycle.py @@ -5,7 +5,7 @@ import _bootstrap # noqa: F401 import fakes -from lib import lifecycle +from ConstraintLens.lib import lifecycle class DerivedUnitFormattingTest(unittest.TestCase): diff --git a/tests/headless/test_scanner.py b/tests/headless/test_scanner.py index 263a44c..17812d6 100644 --- a/tests/headless/test_scanner.py +++ b/tests/headless/test_scanner.py @@ -5,7 +5,7 @@ import _bootstrap # noqa: F401 import fakes -from lib import labels, scanner +from ConstraintLens.lib import labels, scanner def _line(token):