From 3fe4f21943ededd7a8d816f3f7a46edd1bf88a50 Mon Sep 17 00:00:00 2001 From: Shengyu Zhang Date: Wed, 26 Jun 2024 23:05:13 +0800 Subject: [PATCH] fix(template): Cleanup symlink causes unnecessary rebuild in next build --- src/sphinxnotes/any/template.py | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/src/sphinxnotes/any/template.py b/src/sphinxnotes/any/template.py index a953173..2dc93b8 100644 --- a/src/sphinxnotes/any/template.py +++ b/src/sphinxnotes/any/template.py @@ -64,14 +64,13 @@ def _on_builder_inited(cls, app:Sphinx): cls._srcdir = path.join(app.srcdir, reldir) cls._reldir = path.join('/', reldir) # abspath relatived to srcdir - # Cleanup possible residual symlink. - if path.islink(cls._srcdir): - os.unlink(cls._srcdir) - if path.exists(cls._srcdir): - os.remove(cls._srcdir) - # Link them. ensuredir(cls._outdir) - os.symlink(cls._outdir, cls._srcdir) + # Link srcdir -> outdir when needed. + if not path.exists(cls._srcdir): + os.symlink(cls._outdir, cls._srcdir) + elif not path.islink(cls._srcdir): + os.remove(cls._srcdir) + os.symlink(cls._outdir, cls._srcdir) logger.debug(f'[any] srcdir: {cls._srcdir}') logger.debug(f'[any] outdir: {cls._outdir}') @@ -79,7 +78,13 @@ def _on_builder_inited(cls, app:Sphinx): @classmethod def _on_build_finished(cls, app:Sphinx, exception): - os.unlink(cls._srcdir) + # NOTE: no need to clean up the symlink, it will cause unnecessary + # rebuild because file is mssiing from Sphinx's srcdir. Logs like: + # + # [build target] changed 'docname' missing dependency 'xxx.jpg' + # + # os.unlink(cls._srcdir) + pass def __init__(self, *args, **kwargs):