From a1a8a56da9fa5b7ed5c6050a700aec8b1558f4c1 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Sat, 4 May 2024 19:19:01 -0500 Subject: [PATCH] pybricksdev/compile.py: Add better error message When compiling multi-file projects that contain a namespace pacakge (folder without __init__.py), we hit Python bug python/cpython#84530. This adds a try/except block to catch the error and raise a more helpful error message. Issue: https://github.com/pybricks/support/issues/1602 --- CHANGELOG.md | 1 + pybricksdev/compile.py | 8 +++++++- 2 files changed, 8 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d6cbc3d..0cd9271 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Changed - Use relative paths when compiling multi-file projects. +- Better error message when hitting Python bug when compiling multi-file projects. ### Fixed - Fixed `pybricksdev` BLE commands not working on Windows when `pythoncom` diff --git a/pybricksdev/compile.py b/pybricksdev/compile.py index 9320698..6cde349 100644 --- a/pybricksdev/compile.py +++ b/pybricksdev/compile.py @@ -116,7 +116,13 @@ async def compile_multi_file(path: str, abi: Union[int, Tuple[int, int]]): # compile files using Python to find imports contained within the same directory as path search_path = [os.path.dirname(path)] finder = ModuleFinder(search_path) - finder.run_script(path) + + try: + finder.run_script(path) + except AttributeError as e: + raise RuntimeError( + "ModuleFinder doesn't currently handle implicit namespace packages. Did you forget to put an __init__.py file in one of your subdirectories? See https://github.com/pybricks/support/issues/1602" + ) from e # we expect missing modules, namely builtin MicroPython packages like pybricks.* logger.debug("missing modules: %r", finder.any_missing())