Skip to content

Build: fail PDF command (latexmk) if exit code != 0 #10113

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

Merged
merged 5 commits into from
Mar 7, 2023
Merged
Show file tree
Hide file tree
Changes from 3 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
51 changes: 11 additions & 40 deletions readthedocs/doc_builder/backends/sphinx.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,6 @@
from readthedocs.projects.utils import safe_write

from ..base import BaseBuilder
from ..constants import PDF_RE
from ..environments import BuildCommand, DockerBuildCommand
from ..exceptions import BuildUserError
from ..signals import finalize_sphinx_context_data

Expand Down Expand Up @@ -488,30 +486,6 @@ def _post_build(self):
)


class LatexBuildCommand(BuildCommand):

"""Ignore LaTeX exit code if there was file output."""

def run(self):
super().run()
# Force LaTeX exit code to be a little more optimistic. If LaTeX
# reports an output file, let's just assume we're fine.
if PDF_RE.search(self.output):
self.exit_code = 0


class DockerLatexBuildCommand(DockerBuildCommand):

"""Ignore LaTeX exit code if there was file output."""

def run(self):
super().run()
# Force LaTeX exit code to be a little more optimistic. If LaTeX
# reports an output file, let's just assume we're fine.
if PDF_RE.search(self.output):
self.exit_code = 0


class PdfBuilder(BaseSphinx):

"""Builder to generate PDF documentation."""
Expand Down Expand Up @@ -589,11 +563,6 @@ def _build_latexmk(self, cwd):
cwd=self.absolute_host_output_dir,
)

if self.build_env.command_class == DockerBuildCommand:
latex_class = DockerLatexBuildCommand
else:
latex_class = LatexBuildCommand

cmd = [
'latexmk',
'-r',
Expand All @@ -610,16 +579,18 @@ def _build_latexmk(self, cwd):
'-interaction=nonstopmode',
]

cmd_ret = self.build_env.run_command_class(
cls=latex_class,
cmd=cmd,
warn_only=True,
cwd=self.absolute_host_output_dir,
)

self.pdf_file_name = f'{self.project.slug}.pdf'
try:
cmd_ret = self.run(
*cmd,
cwd=self.absolute_host_output_dir,
)
self.pdf_file_name = f"{self.project.slug}.pdf"
return cmd_ret.successful

return cmd_ret.successful
# Catch the exception and re-raise it with a specific message
except BuildUserError:
raise BuildUserError(BuildUserError.PDF_COMMAND_FAILED)
return False

def _post_build(self):
"""Internal post build to cleanup PDF output directory and leave only one .pdf file."""
Expand Down
3 changes: 0 additions & 3 deletions readthedocs/doc_builder/constants.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,12 @@

"""Doc build constants."""

import re

import structlog
from django.conf import settings

log = structlog.get_logger(__name__)

PDF_RE = re.compile('Output written on (.*?)')

# Docker
DOCKER_SOCKET = settings.DOCKER_SOCKET
DOCKER_VERSION = settings.DOCKER_VERSION
Expand Down
7 changes: 7 additions & 0 deletions readthedocs/doc_builder/exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,13 @@ class BuildUserError(BuildBaseException):
"and it is not currently supported. "
'Please, remove all the files but the "{artifact_type}" your want to upload.'
)
PDF_COMMAND_FAILED = gettext_noop(
"PDF generation failed. "
"Please double check the command's output looking for errors and fix them. "
"Note it's possible the PDF has been always failing silently in your project, "
"but due to latest changes, "
"Read the Docs now makes it explicit and fail the whole build."
)


class BuildUserSkip(BuildUserError):
Expand Down
7 changes: 0 additions & 7 deletions readthedocs/projects/tests/mockers.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,6 @@ def _mock_artifact_builders(self):
"project-slug.pdf",
)

self.patches['builder.pdf.LatexBuildCommand.run'] = mock.patch(
'readthedocs.doc_builder.backends.sphinx.LatexBuildCommand.run',
return_value=mock.MagicMock(output='stdout', successful=True)
)
# self.patches['builder.pdf.LatexBuildCommand.output'] = mock.patch(
# 'readthedocs.doc_builder.backends.sphinx.LatexBuildCommand.output',
# )
self.patches['builder.pdf.glob'] = mock.patch(
'readthedocs.doc_builder.backends.sphinx.glob',
return_value=['output.file'],
Expand Down
20 changes: 20 additions & 0 deletions readthedocs/projects/tests/test_build_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -723,6 +723,18 @@ def test_build_commands_executed(
bin_path=mock.ANY,
),
mock.call("cat", "latexmkrc", cwd=mock.ANY),
mock.call(
"latexmk",
"-r",
"latexmkrc",
"-pdf",
"-f",
"-dvi-",
"-ps-",
"-jobname=project",
"-interaction=nonstopmode",
cwd=mock.ANY,
),
# NOTE: pdf `mv` commands and others are not here because the
# PDF resulting file is not found in the process (`_post_build`)
mock.call(
Expand Down Expand Up @@ -794,6 +806,14 @@ def test_build_commands_executed(
record=False,
demux=True,
),
mock.call(
"test",
"-x",
"_build/html",
record=False,
demux=True,
cwd=mock.ANY,
),
]
)

Expand Down