Skip to content

linkcheck only emits warnings on HTTP redirects if linkcheck_allowed_redirects is non-empty #13439

@jameslamb

Description

@jameslamb

Describe the bug

The docs for led me to believe that HTTP redirects would result in warnings from the linkcheck builder, which I could then turn into a non-0 exit status (and therefore CI failures) via --fail-on-warning.

linkcheck_allowed_redirects

A dictionary that maps a pattern of the source URI to a pattern of the canonical URI

The linkcheck builder will emit a warning when it finds redirected links that don’t meet the rules above.
It can be useful to detect unexpected redirects when using the fail-on-warnings mode.

(my emphasis)

ref: https://www.sphinx-doc.org/en/master/usage/configuration.html#confval-linkcheck_allowed_redirects

I found that if linkcheck_allowed_redirects is empty or not specific in conf.py, HTTP redirects are reported in log output but not emitted as warnings.

As a result, redirects could silently make it through the linkcheck check. To work around this, I'm defining a placeholder like this to get the behavior "fail on any redirects".

linkcheck_allowed_redirects = {"abc": "def"}

How to Reproduce

With sphinx 8.2.3, installed from conda-forge, ran the quickstart.

mkdir testproject
cd ./testproject

sphinx-quickstart \
    --no-sep \
    --project testproject \
    --author james \
    --release v0.1.0 \
    --language en docs

Added a link that's guaranteed to redirect into index.rst.

echo 'See `this link <https://httpbin.org/redirect/1>`_ for details' >> ./docs/index.rst

(you can test this yourself with curl -i https://httpbin.org/redirect/1)

Saw linkcheck report the redirect in logs, but return a 0 exit status.

python -m sphinx -b linkcheck --fail-on-warning ./docs/ ./linkcheck_output
# build succeeded.

echo $?
# 0

Including a screenshot instead of text only because I think that the output color indicates something about the type of log message sphinx is omitting (a difference I don't understand, sorry).

Image

Added a nonsense linkcheck_allowed_redirects.

echo 'linkcheck_allowed_redirects = {"abc": "def"}' >> ./docs/conf.py

And tried again. This time, saw the redirect reported as a warning (in red this time), an a non-0 exit status.

python -m sphinx -b linkcheck --fail-on-warning ./docs/ ./linkcheck_output
# build finished with problems, 1 warning (with warnings treated as errors).

echo $?
# 1
Image

Environment Information

output of 'python -m sphinx --bug-report' (click me)
Platform:              darwin; (macOS-14.4.1-arm64-arm-64bit)
Python version:        3.12.8 | packaged by conda-forge | (main, Dec  5 2024, 14:19:53) [Clang 18.1.8 ])
Python implementation: CPython
Sphinx version:        8.2.3
Docutils version:      0.21.2
Jinja2 version:        3.1.6
Pygments version:      2.19.1

Sphinx extensions

None

Additional context

I'm reporting this as a "bug" because it seems based on the docs that this is unintentional. But if it is intentional, I'd be happy to submit a PR proposing a clarification to those docs explaining how to achieve "fail on any redirects".

I did look for other relevant issues and didn't find any specifically about this. Linking a few somewhat-related:

Thanks for your time and consideration.

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions