Reload hook modules when config reloads by marko1olo · Pull Request #4141 · mkdocs/mkdocs · GitHub
Skip to content

Reload hook modules when config reloads#4141

Open
marko1olo wants to merge 1 commit into
mkdocs:masterfrom
marko1olo:fix-serve-hook-reload
Open

Reload hook modules when config reloads#4141
marko1olo wants to merge 1 commit into
mkdocs:masterfrom
marko1olo:fix-serve-hook-reload

Conversation

@marko1olo

@marko1olo marko1olo commented Jun 6, 2026

Copy link
Copy Markdown

Fixes #3993.

mkdocs serve reloads the configuration for rebuilds, but hook modules were cached by Hooks._load_hook(). If a watched hook file changed, the subsequent config validation reused the old module object instead of executing the updated hook file.

This removes the cross-validation cache for hook modules and executes the current hook source on each config load, while preserving the existing module metadata, sys.modules registration, and temporary hook-directory sys.path handling. The regression test also covers a same-size/same-mtime rewrite so reloads are not dependent on Python bytecode-cache freshness.

Tests run:

  • python -m unittest mkdocs.tests.config.config_options_tests.HooksTest.test_hooks_reload_when_file_changes
  • python -m unittest mkdocs.tests.config.config_options_tests.HooksTest
  • python -m unittest mkdocs.tests.config.config_options_tests
  • python -m unittest mkdocs.tests.config.config_options_legacy_tests.HooksTest
  • python -m unittest discover -s mkdocs\tests\config -p "*_tests.py"
  • ruff check mkdocs\config\config_options.py mkdocs\tests\config\config_options_tests.py
  • python -X utf8 -m isort --check-only --diff mkdocs\config\config_options.py mkdocs\tests\config\config_options_tests.py
  • python -X utf8 -m black --workers 1 -q --check --diff --line-ranges 1-8 --line-ranges 1186-1208 mkdocs\config\config_options.py
  • python -X utf8 -m black --workers 1 -q --check --diff --line-ranges 2374-2406 mkdocs\tests\config\config_options_tests.py
  • git diff --check

@marko1olo marko1olo force-pushed the fix-serve-hook-reload branch from 3133b14 to 6c96acf Compare June 6, 2026 23:08
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.

Hooks aren't reloading when using the dev server

1 participant