Skip to content

Print meson output in case of error during editable rebuild #750

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 7 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions mesonpy/_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,11 +340,28 @@ def _rebuild(self) -> Node:
if self._work_to_do(env):
build_command = ' '.join(self._build_cmd)
print(f'meson-python: building {self._name}: {build_command}', flush=True)
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, check=True)
subprocess.run(
self._build_cmd,
cwd=self._build_path,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
env=env,
check=True,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This now hides output that was directed to standard output before.

else:
subprocess.run(self._build_cmd, cwd=self._build_path, env=env, stdout=subprocess.DEVNULL, check=True)
subprocess.run(
self._build_cmd,
cwd=self._build_path,
env=env,
stdout=subprocess.PIPE,
stderr=subprocess.STDOUT,
check=True,
)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The compilation output can be very long. Buffering it all in memory does not seems a very good idea to me.

except subprocess.CalledProcessError as exc:
raise ImportError(f're-building the {self._name} meson-python editable wheel package failed') from exc
output = exc.output.decode(errors='replace') if exc.output is not None else 'No error details available'
raise ImportError(
f're-building the {self._name} meson-python editable wheel package failed with: \n{output}'
) from exc

install_plan_path = os.path.join(self._build_path, 'meson-info', 'intro-install_plan.json')
with open(install_plan_path, 'r', encoding='utf8') as f:
Expand Down
47 changes: 45 additions & 2 deletions tests/test_editable.py
Original file line number Diff line number Diff line change
Expand Up @@ -336,9 +336,52 @@ def test_editable_rebuild_error(package_purelib_and_platlib, tmp_path, verbose):
# Import module and trigger rebuild: the build fails and ImportErrror is raised
stdout = io.StringIO()
with redirect_stdout(stdout):
with pytest.raises(ImportError, match='re-building the purelib-and-platlib '):
with pytest.raises(
ImportError, match=r're-building the purelib-and-platlib (?s:.)*error: expected identifier'
):
import plat # noqa: F401
assert not verbose or stdout.getvalue().startswith('meson-python: building ')
if verbose:
assert stdout.getvalue().startswith('meson-python: building ')
else:
assert not stdout.getvalue()

finally:
del sys.meta_path[0]
sys.modules.pop('pure', None)
path.write_text(code)


@pytest.mark.parametrize('verbose', [False, True], ids=('', 'verbose'))
def test_editable_meson_file_rebuild_error(package_purelib_and_platlib, tmp_path, verbose):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are you adding this test? Why do you expect that a failure in running meson produces a different outcome that a failure in running a compilation tool?

with mesonpy._project({'builddir': os.fspath(tmp_path)}) as project:
finder = _editable.MesonpyMetaFinder(
project._metadata.name,
{'plat', 'pure'},
os.fspath(tmp_path),
project._build_command,
verbose=verbose,
)
path = package_purelib_and_platlib / 'meson.build'
code = path.read_text()

try:
# Install editable hooks
sys.meta_path.insert(0, finder)

# Insert invalid code in the meson build file
path.write_text('<not valid')

# Import module and trigger rebuild: the build fails and ImportErrror is raised
stdout = io.StringIO()
with redirect_stdout(stdout):
with pytest.raises(
ImportError, match=r're-building the purelib-and-platlib (?s:.)*ERROR: Invalid source tree:'
):
import plat # noqa: F401
if verbose:
assert stdout.getvalue().startswith('meson-python: building ')
else:
assert not stdout.getvalue()

finally:
del sys.meta_path[0]
Expand Down