click_extra.sphinx.fail_on_warnings module¶

Fail a Sphinx build on the warning types a project lists, and only on those.

Sphinx fails a build on warnings all or nothing: --fail-on-warning counts every warning, and suppress_warnings can hide a type but never make one fatal. A project that lives with some warnings, like the unresolved references autodoc produces for third-party names, then cannot stop the build on the ones it does not accept.

The main case is myst.xref_missing. myst-parser resolves every `text <#anchor>`_ and `text <page.md#anchor>`_ link against what the build produced. For a link it cannot place, it logs that warning and ships the link as written anyway, so a dead fragment reaches the published site behind a green build.

A handler on the sphinx logger records each warning whose type the project lists in FAIL_ON_WARNINGS_CONFIG. Once the build is over, it reports them in one error and sets the exit status to 1. The build itself runs to its end, so every offending warning is reported, not only the first.

Todo

Contribute this upstream as a Sphinx config value, on sphinx-doc/sphinx#7949, which asks for the same feature and has waited for a design since 2020.

Sphinx already holds both halves. sphinx.util.logging.is_suppressed_warning is the matching this module reuses, and the exit status --fail-on-warning sets is the outcome. A config value read where sphinx.util.logging.WarningSuppressor already counts each warning would replace this module.

Should it land, keep this module as a shim for the Sphinx releases that predate it, then drop it once click-extra’s Sphinx floor moves past them.

click_extra.sphinx.fail_on_warnings.FAIL_ON_WARNINGS_CONFIG = 'click_extra_fail_on_warnings'¶

Name of the conf.py value listing the warning types that fail the build.

Entries use the syntax of suppress_warnings: myst covers every subtype, myst.xref_missing only that one. Empty by default, which leaves Sphinx’s behavior untouched: which warnings a project can live with is its own call.

class click_extra.sphinx.fail_on_warnings.ListedWarningCollector(app)[source]¶

Bases: Handler

Collect the warnings whose type the project listed as fatal.

Attached to the sphinx logger rather than to Sphinx’s warning stream, so it sees every warning, whatever --quiet does to the console. A warning that suppress_warnings hides is skipped, the same way --fail-on-warning skips it.

Initializes the instance - basically setting the formatter to None and the filter list to empty.

matched: dict[LogRecord, str]¶

The type.subtype label of each matched record.

Sphinx buffers the warnings of a build phase and replays them through every handler when the phase ends, after this handler saw them live. A record hashes by identity, so keying on it counts each one once.

emit(record)[source]¶

Keep record if its type is listed and not suppressed.

Return type:

None

finish(app, exception)[source]¶

Detach, then fail the build if a listed warning was logged.

Return type:

None

click_extra.sphinx.fail_on_warnings.setup(app)[source]¶

Register the config value and attach a collector for app.

Called from click_extra.sphinx.setup() so projects only need to list "click_extra.sphinx" in their extensions. The collector reports at priority 900, after the default 500 of other build-finished handlers, so a warning one of them logs still counts.

Return type:

None