Skip to content

Connect to HTML-builder-relevant Sphinx event instead of all builders

Lynn Root requested to merge roguelynn/sphinx-reredirects:fix-emit into master

Currently, when using this extension, it fails for non-HTML-based Sphinx builders. For instance, when running the following:

$ sphinx-build -T -b linkcheck src build/linkcheck

even if linkcheck succeeds, sphinx-reredirects fails the build:

# snip linkcheck output
build succeeded.

Look for any errors in the above output or in build/linkcheck/output.txt

Traceback (most recent call last):
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx/events.py", line 110, in emit
    results.append(listener.handler(self.app, *args))
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx_reredirects/__init__.py", line 50, in init
    create_redirect_file(redirect_template, redirect_file_path, new_target)
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx_reredirects/__init__.py", line 60, in create_redirect_file
    at_path.write_text(content)
  File "/Users/lynn/.pyenv/versions/3.7.8/lib/python3.7/pathlib.py", line 1240, in write_text
    with self.open(mode='w', encoding=encoding, errors=errors) as f:
  File "/Users/lynn/.pyenv/versions/3.7.8/lib/python3.7/pathlib.py", line 1208, in open
    opener=self._opener)
  File "/Users/lynn/.pyenv/versions/3.7.8/lib/python3.7/pathlib.py", line 1063, in _opener
    return self._accessor.open(self, flags, mode)
FileNotFoundError: [Errno 2] No such file or directory: '/Users/lynn/Dev/spotify/sigint/klio-v2/docs/build/linkcheck/userguide/intro.html'

The above exception was the direct cause of the following exception:

Traceback (most recent call last):
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx/cmd/build.py", line 280, in build_main
    app.build(args.force_all, filenames)
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx/application.py", line 385, in build
    self.events.emit('build-finished', None)
  File "/Users/lynn/.pyenv/versions/3.7.8/envs/klio-v2-docs37/lib/python3.7/site-packages/sphinx/events.py", line 118, in emit
    (listener.handler, name), exc) from exc
sphinx.errors.ExtensionError: Handler <function init at 0x10d51d320> for event 'build-finished' threw an exception (exception: [Errno 2] No such file or directory: '/Users/lynn/Dev/spotify/sigint/klio-v2/docs/build/linkcheck/userguide/intro.html')

Extension error:
Handler <function init at 0x10d51d320> for event 'build-finished' threw an exception (exception: [Errno 2] No such file or directory: '/Users/lynn/Dev/spotify/sigint/klio-v2/docs/build/linkcheck/userguide/intro.html')

The extension is looking for HTML files when none have been created by the particular builder. This is seen in other non-HTML builders like -b latex and -b epub.

This change updates the event to which this extension listens to html-collect-pages which should be the appropriate one (Sphinx docs and code are a little unclear to me). I've tested with -b html, -b htmlhelp, -b latex, and -b epub with success.

Merge request reports