diff --git a/mesonpy/__init__.py b/mesonpy/__init__.py index 7098c1004..44a05b021 100644 --- a/mesonpy/__init__.py +++ b/mesonpy/__init__.py @@ -604,6 +604,8 @@ def build_editable(self, directory: Path, verbose: bool = False) -> pathlib.Path hook_module_name = f'_mesonpy_hook_{self.normalized_name.replace(".", "_")}' hook_install_code = textwrap.dedent(f''' MesonpyFinder.install( + project_name={_as_python_declaration(self._project.name)}, + hook_name={_as_python_declaration(hook_module_name)}, project_path={_as_python_declaration(self._source_dir)}, build_path={_as_python_declaration(self._build_dir)}, import_paths={_as_python_declaration(import_paths)}, diff --git a/mesonpy/_editable.py b/mesonpy/_editable.py index f452f1aa8..f3690c3ec 100644 --- a/mesonpy/_editable.py +++ b/mesonpy/_editable.py @@ -59,6 +59,8 @@ class MesonpyFinder(importlib.abc.MetaPathFinder): def __init__( self, + project_name: str, + hook_name: str, project_path: str, build_path: str, import_paths: List[str], @@ -66,6 +68,8 @@ def __init__( rebuild_commands: List[List[str]], verbose: bool = False, ) -> None: + self._project_name = project_name + self._hook_name = hook_name self._project_path = project_path self._build_path = build_path self._import_paths = import_paths @@ -73,6 +77,18 @@ def __init__( self._rebuild_commands = rebuild_commands self._verbose = verbose + for path in (self._project_path, self._build_path): + if not os.path.isdir(path): + raise ImportError( + f'{path} is not a directory, but it is required to rebuild ' + f'"{self._project_name}", which is installed in editable ' + 'mode. Please reinstall the project to get it back to ' + 'working condition. If there are any issues uninstalling ' + 'this installation, you can manually remove ' + f'{self._hook_name} and {os.path.basename(__file__)}, ' + f'located in {os.path.dirname(__file__)}.' + ) + def __repr__(self) -> str: return f'{self.__class__.__name__}({self._project_path})' @@ -128,6 +144,8 @@ def find_spec( @classmethod def install( cls, + project_name: str, + hook_name: str, project_path: str, build_path: str, import_paths: List[str], @@ -140,7 +158,16 @@ def install( if os.environ.get('MESONPY_EDITABLE_VERBOSE', ''): verbose = True # install our finder - finder = cls(project_path, build_path, import_paths, top_level_modules, rebuild_commands, verbose) + finder = cls( + project_name, + hook_name, + project_path, + build_path, + import_paths, + top_level_modules, + rebuild_commands, + verbose, + ) if finder not in sys.meta_path: # prepend our finder to sys.meta_path, so that it is queried before # the normal finders, and can trigger a project rebuild