Skip to content

Commit 899cb5c

Browse files
committed
ENH: capture and report build errors when rebuilding editable wheels
When editable-verbose is not enabled, redirect the build output to a file. When the build fails, parse this file to look for the build error and append it to the ImportError exception message. Fixes #820.
1 parent 070c597 commit 899cb5c

2 files changed

Lines changed: 17 additions & 3 deletions

File tree

mesonpy/_editable.py

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,15 +332,28 @@ def _rebuild(self) -> Node:
332332
env[MARKER] = os.pathsep.join((env.get(MARKER, ''), self._build_path))
333333

334334
if self._verbose or bool(env.get(VERBOSE, '')):
335+
log_path = None
335336
# We want to show some output only if there is some work to do.
336337
if self._work_to_do(env):
337338
build_command = ' '.join(self._build_cmd)
338339
print(f'meson-python: building {self._name}: {build_command}', flush=True)
339340
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, check=True)
340341
else:
341-
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, stdout=subprocess.DEVNULL, check=True)
342+
# Redirect build log to file.
343+
log_path = os.path.join(self._build_path, 'meson-python-build-log.txt')
344+
with open(log_path, 'w') as log:
345+
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, stdout=log, check=True)
342346
except subprocess.CalledProcessError as exc:
343-
raise ImportError(f're-building the {self._name} meson-python editable wheel package failed') from exc
347+
msg = f're-building the {self._name} meson-python editable wheel package failed'
348+
if log_path:
349+
with open(log_path, 'r') as log:
350+
# Skip to the error.
351+
for line in log:
352+
if line.startswith('FAILED: '):
353+
break
354+
error = log.read()
355+
msg = f'{msg}:\n{error}'
356+
raise ImportError(msg) from exc
344357

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

tests/test_editable.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -335,9 +335,10 @@ def test_editable_rebuild_error(package_purelib_and_platlib, tmp_path, verbose):
335335
# Import module and trigger rebuild: the build fails and ImportErrror is raised
336336
stdout = io.StringIO()
337337
with redirect_stdout(stdout):
338-
with pytest.raises(ImportError, match='re-building the purelib-and-platlib '):
338+
with pytest.raises(ImportError, match='re-building the purelib-and-platlib ') as exc:
339339
import plat # noqa: F401
340340
assert not verbose or stdout.getvalue().startswith('meson-python: building ')
341+
assert verbose or 'ninja: build stopped: subcommand failed.' in exc.value.msg
341342

342343
finally:
343344
del sys.meta_path[0]

0 commit comments

Comments
 (0)