Skip to content

Commit

Permalink
Safeguard editable installs against build_py errors
Browse files Browse the repository at this point in the history
  • Loading branch information
abravalheri committed Aug 12, 2022
1 parent 37278c3 commit 048633a
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 3 deletions.
4 changes: 2 additions & 2 deletions setuptools/command/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class build(_build):
# copy to avoid sharing the object with parent class
sub_commands = _build.sub_commands[:]

def run(self):
def get_sub_commands(self):
subcommands = {cmd[0] for cmd in _build.sub_commands}
if subcommands - _ORIGINAL_SUBCOMMANDS:
msg = """
Expand All @@ -30,7 +30,7 @@ def run(self):
"""
warnings.warn(msg, SetuptoolsDeprecationWarning)
self.sub_commands = _build.sub_commands
super().run()
return super().get_sub_commands()


class SubCommand(Protocol):
Expand Down
45 changes: 44 additions & 1 deletion setuptools/command/editable_wheel.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
)

from setuptools import Command, SetuptoolsDeprecationWarning, errors, namespaces
from setuptools.command.build_py import build_py as build_py_cls
from setuptools.discovery import find_package_path
from setuptools.dist import Distribution

Expand Down Expand Up @@ -254,13 +255,55 @@ def _run_build_commands(
self, dist_name: str, unpacked_wheel: _Path, build_lib: _Path, tmp_dir: _Path
) -> Tuple[List[str], Dict[str, str]]:
self._configure_build(dist_name, unpacked_wheel, build_lib, tmp_dir)
self.run_command("build")
self._run_build_subcommands()
files, mapping = self._collect_build_outputs()
self._run_install("headers")
self._run_install("scripts")
self._run_install("data")
return files, mapping

def _run_build_subcommands(self):
"""
Issue #3501 indicates that some plugins/customizations might rely on:
1. ``build_py`` not running
2. ``build_py`` always copying files to ``build_lib``
However both these assumptions may be false in editable_wheel.
This method implements a temporary workaround to support the ecosystem
while the implementations catch up.
"""
# TODO: Once plugins/customisations had the chance to catch up, replace
# `self._run_build_subcommands()` with `self.run_command("build")`.
# Also remove _safely_run, TestCustomBuildPy. Suggested date: Aug/2023.
build: Command = self.get_finalized_command("build")
for name in build.get_sub_commands():
cmd = self.distribution.get_command_obj(name)
if name == "build_py" and type(cmd) != build_py_cls:
self._safely_run(name)
else:
self.run_command(name)

def _safely_run(self, cmd_name: str):
try:
return self.run_command(cmd_name)
except Exception:
msg = f"""{traceback.format_exc()}\n
If you are seeing this warning it is very likely that a setuptools
plugin or customization overrides the `build_py` command, without
tacking into consideration how editable installs run build steps
starting from v64.0.0.
Plugin authors and developers relying on custom build steps are encouraged
to update their `build_py` implementation considering the information in
https://setuptools.pypa.io/en/latest/userguide/extension.html
about editable installs.
For the time being `setuptools` will silence this error and ignore
the faulty command, but this behaviour will change in future versions.\n
"""
warnings.warn(msg, SetuptoolsDeprecationWarning, stacklevel=2)

def _create_wheel_file(self, bdist_wheel):
from wheel.wheelfile import WheelFile

Expand Down

0 comments on commit 048633a

Please sign in to comment.