Skip to content

Fix anchor validation silently disabled when verbose mode is active - #4160

Open
amitmishra11 wants to merge 1 commit into
mkdocs:masterfrom
amitmishra11:fix-anchor-validation-verbose-mode
Open

amitmishra11 wants to merge 1 commit into
mkdocs:masterfrom
amitmishra11:fix-anchor-validation-verbose-mode

Conversation

@amitmishra11

Copy link
Copy Markdown

Fixes #3991.

Bug

Page.render() gated anchor-link collection on log.getEffectiveLevel() > logging.DEBUG. When verbose mode (mkdocs build -v) is active the MkDocs logger is set to DEBUG, so that condition becomes False, self.links_to_anchors stays None, and validate_anchor_links silently returns early.

The observable consequence: mkdocs build --strict aborts due to anchor warnings, but mkdocs build -v --strict succeeds with exit code 0 despite those same warnings being configured.

Root cause analysis

The intent was to skip expensive anchor-link collection when the validation.links.anchors config is ignore (which maps to logging.DEBUG). The implementation used log.getEffectiveLevel() as a proxy for that config value, but they are not equivalent. The verbose flag affects the runtime log level independently of the configured validation severity.

Fix

Replace the runtime log-level check with a check against the configured validation level:

- if log.getEffectiveLevel() > logging.DEBUG:
+ if config.validation.links.anchors > logging.DEBUG:

This skips anchor-link collection only when the user has explicitly set validation.links.anchors: ignore, and is unaffected by whether verbose mode is active.

Testing

Added test_anchor_warning_not_suppressed_in_verbose_mode which sets the mkdocs logger to DEBUG before calling build.build() and asserts that anchor warnings are still emitted. All 6 anchor-related tests pass.

render() gated anchor-link collection on log.getEffectiveLevel() > logging.DEBUG,
which meant enabling verbose mode (which sets the logger to DEBUG) caused
self.links_to_anchors to remain None, silently skipping all anchor validation.
The result: mkdocs build --strict would abort, but mkdocs build -v --strict
would succeed and exit 0 regardless of configured anchor warnings.

Fix: check config.validation.links.anchors > logging.DEBUG instead. This
preserves the intended skip when validation.links.anchors is 'ignore' (mapped
to DEBUG), while remaining unaffected by the runtime logger level.

Adds a regression test that sets the mkdocs logger to DEBUG before build and
verifies anchor warnings still fire.

Fixes mkdocs#3991

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Verbose flag overrides --strict flag.

1 participant