Skip to content

Commit

Permalink
feat: beautify the build error (#1501)
Browse files Browse the repository at this point in the history
  • Loading branch information
frostming authored Nov 8, 2022
1 parent 332ff64 commit 669096c
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 14 deletions.
1 change: 1 addition & 0 deletions news/1491.feature.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Beautify the error message of build errors. Default to showing the last 10 lines of the build output.
30 changes: 24 additions & 6 deletions src/pdm/builders/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import threading
from logging import Logger
from pathlib import Path
from typing import TYPE_CHECKING, Any, Iterable, Mapping
from typing import TYPE_CHECKING, Any, Iterable, Mapping, cast

from pep517.wrappers import Pep517HookCaller

Expand Down Expand Up @@ -42,6 +42,7 @@ def __init__(self, logger: Logger, level: int) -> None:
self.fd_read, self.fd_write = os.pipe()

self.start()
self._output_buffer: list[str] = []

def fileno(self) -> int:
return self.fd_write
Expand All @@ -57,12 +58,32 @@ def run(self) -> None:

def _write(self, message: str) -> None:
self.logger.log(self.level, message)
self._output_buffer.append(message)
self._output_buffer[:-10] = []

def stop(self) -> None:
os.close(self.fd_write)
self.join()


def build_error(e: subprocess.CalledProcessError) -> BuildError:
"""Get a build error with meaningful error message
from the subprocess output.
"""
output = cast("list[str]", e.output)
errors: list[str] = []
if output[-1].strip().startswith("ModuleNotFoundError"):
package = output[-1].strip().split()[-1]
errors.append(
f"Module {package} is missing, please make sure it is specified in the "
"'build-system.requires' section. If it is not possible, "
"add it to the project and use '--no-isolation' option."
)
errors.extend(["Showing the last 10 lines of the build output:", *output])
error_message = "\n".join(errors)
return BuildError(f"Build backend raised error: {error_message}")


def log_subprocessor(
cmd: list[str],
cwd: str | Path | None = None,
Expand All @@ -81,11 +102,8 @@ def log_subprocessor(
stderr=subprocess.STDOUT,
)
except subprocess.CalledProcessError as e:
raise BuildError(
f"Call command {cmd} return non-zero status({e.returncode}). "
"Make sure the package is PEP 517-compliant, or you can add "
"`--no-isolation` to the command."
) from None
e.output = outstream._output_buffer
raise build_error(e) from None
finally:
outstream.stop()

Expand Down
5 changes: 1 addition & 4 deletions src/pdm/project/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,10 +264,7 @@ def environment(self, value: Environment) -> None:

@property
def python_requires(self) -> PySpecSet:
try:
return PySpecSet(self.pyproject.metadata.get("requires-python", ""))
except ProjectError:
return PySpecSet()
return PySpecSet(self.pyproject.metadata.get("requires-python", ""))

def get_dependencies(self, group: str | None = None) -> dict[str, Requirement]:
metadata = self.pyproject.metadata
Expand Down
5 changes: 1 addition & 4 deletions src/pdm/project/project_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@

from tomlkit import TOMLDocument, items

from pdm.exceptions import ProjectError
from pdm.project.toml_file import TOMLBase


Expand Down Expand Up @@ -41,9 +40,7 @@ def is_valid(self) -> bool:

@property
def metadata(self) -> items.Table:
if not self.is_valid:
raise ProjectError("No [project] table found in pyproject.toml")
return self._data["project"]
return self._data.setdefault("project", {})

@property
def settings(self) -> items.Table:
Expand Down

0 comments on commit 669096c

Please sign in to comment.