Skip to content

Commit 07e4ddb

Browse files
lestevednicolodi
authored andcommitted
ENH: in editable mode raise ImportError when the build command fails
1 parent e5a180c commit 07e4ddb

File tree

2 files changed

+49
-14
lines changed

2 files changed

+49
-14
lines changed

mesonpy/_editable.py

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -327,20 +327,23 @@ def _work_to_do(self, env: dict[str, str]) -> bool:
327327

328328
@functools.lru_cache(maxsize=1)
329329
def _rebuild(self) -> Node:
330-
# skip editable wheel lookup during rebuild: during the build
331-
# the module we are rebuilding might be imported causing a
332-
# rebuild loop.
333-
env = os.environ.copy()
334-
env[MARKER] = os.pathsep.join((env.get(MARKER, ''), self._build_path))
335-
336-
if self._verbose or bool(env.get(VERBOSE, '')):
337-
# We want to show some output only if there is some work to do
338-
if self._work_to_do(env):
339-
build_command = ' '.join(self._build_cmd)
340-
print(f'meson-python: building {self._name}: {build_command}', flush=True)
341-
subprocess.run(self._build_cmd, cwd=self._build_path, env=env)
342-
else:
343-
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, stdout=subprocess.DEVNULL)
330+
try:
331+
# Skip editable wheel lookup during rebuild: during the build
332+
# the module we are rebuilding might be imported causing a
333+
# rebuild loop.
334+
env = os.environ.copy()
335+
env[MARKER] = os.pathsep.join((env.get(MARKER, ''), self._build_path))
336+
337+
if self._verbose or bool(env.get(VERBOSE, '')):
338+
# We want to show some output only if there is some work to do.
339+
if self._work_to_do(env):
340+
build_command = ' '.join(self._build_cmd)
341+
print(f'meson-python: building {self._name}: {build_command}', flush=True)
342+
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, check=True)
343+
else:
344+
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, stdout=subprocess.DEVNULL, check=True)
345+
except subprocess.CalledProcessError as exc:
346+
raise ImportError(f're-building the {self._name} meson-python editable wheel package failed') from exc
344347

345348
install_plan_path = os.path.join(self._build_path, 'meson-info', 'intro-install_plan.json')
346349
with open(install_plan_path, 'r', encoding='utf8') as f:

tests/test_editable.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,3 +308,35 @@ def test_editable_verbose(venv, package_complex, editable_complex, monkeypatch):
308308

309309
# Another import without file changes should not show any output
310310
assert venv.python('-c', 'import complex') == ''
311+
312+
313+
@pytest.mark.parametrize('verbose', [False, True], ids=('', 'verbose'))
314+
def test_editable_rebuild_error(package_purelib_and_platlib, tmp_path, verbose):
315+
with mesonpy._project({'builddir': os.fspath(tmp_path)}) as project:
316+
317+
finder = _editable.MesonpyMetaFinder(
318+
project._metadata.name, {'plat', 'pure'},
319+
os.fspath(tmp_path), project._build_command,
320+
verbose=verbose,
321+
)
322+
path = package_purelib_and_platlib / 'plat.c'
323+
code = path.read_text()
324+
325+
try:
326+
# Install editable hooks
327+
sys.meta_path.insert(0, finder)
328+
329+
# Insert invalid code in the extension module source code
330+
path.write_text('return')
331+
332+
# Import module and trigger rebuild: the build fails and ImportErrror is raised
333+
stdout = io.StringIO()
334+
with redirect_stdout(stdout):
335+
with pytest.raises(ImportError, match='re-building the purelib-and-platlib '):
336+
import plat # noqa: F401
337+
assert not verbose or stdout.getvalue().startswith('meson-python: building ')
338+
339+
finally:
340+
del sys.meta_path[0]
341+
sys.modules.pop('pure', None)
342+
path.write_text(code)

0 commit comments

Comments
 (0)