Skip to content

Commit 4f270a5

Browse files
larsonerAA-Turner
andauthored
Tell users when a template causes build invalidation (#12746)
Co-authored-by: Adam Turner <9087854+AA-Turner@users.noreply.github.com>
1 parent 586c0cd commit 4f270a5

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

sphinx/builders/html/__init__.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
from sphinx.theming import HTMLThemeFactory
4141
from sphinx.util import isurl, logging
4242
from sphinx.util._timestamps import _format_rfc3339_microseconds
43+
from sphinx.util.console import bold
4344
from sphinx.util.display import progress_message, status_iterator
4445
from sphinx.util.docutils import new_document
4546
from sphinx.util.fileutil import copy_asset
@@ -389,8 +390,9 @@ def math_renderer_name(self) -> str | None:
389390
return None
390391

391392
def get_outdated_docs(self) -> Iterator[str]:
393+
build_info_fname = self.outdir / '.buildinfo'
392394
try:
393-
with open(path.join(self.outdir, '.buildinfo'), encoding="utf-8") as fp:
395+
with open(build_info_fname, encoding="utf-8") as fp:
394396
buildinfo = BuildInfo.load(fp)
395397

396398
if self.build_info != buildinfo:
@@ -405,6 +407,21 @@ def get_outdated_docs(self) -> Iterator[str]:
405407

406408
if self.templates:
407409
template_mtime = int(self.templates.newest_template_mtime() * 10**6)
410+
try:
411+
old_mtime = _last_modified_time(build_info_fname)
412+
except Exception:
413+
pass
414+
else:
415+
# Let users know they have a newer template
416+
if template_mtime > old_mtime:
417+
logger.info(
418+
bold("building [html]: ") +
419+
__(
420+
"template %s has been changed since the previous build, "
421+
"all docs will be rebuilt"
422+
),
423+
self.templates.newest_template_name(),
424+
)
408425
else:
409426
template_mtime = 0
410427
for docname in self.env.found_docs:

sphinx/jinja2glue.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -204,8 +204,14 @@ def render_string(self, source: str, context: dict) -> str:
204204
return self.environment.from_string(source).render(context)
205205

206206
def newest_template_mtime(self) -> float:
207+
return self._newest_template_mtime_name()[0]
208+
209+
def newest_template_name(self) -> str:
210+
return self._newest_template_mtime_name()[1]
211+
212+
def _newest_template_mtime_name(self) -> tuple[float, str]:
207213
return max(
208-
os.stat(os.path.join(root, sfile)).st_mtime_ns / 10**9
214+
(os.stat(os.path.join(root, sfile)).st_mtime_ns / 10**9, sfile)
209215
for dirname in self.pathchain
210216
for root, _dirs, files in os.walk(dirname)
211217
for sfile in files

0 commit comments

Comments
 (0)