Skip to content

Warn when upstream mkdocs is installed alongside mkdocs-ng - #114

Closed
shenxianpeng wants to merge 1 commit into
mainfrom
fix/detect-upstream-mkdocs-conflict
Closed

shenxianpeng wants to merge 1 commit into
mainfrom
fix/detect-upstream-mkdocs-conflict

Conversation

@shenxianpeng

Copy link
Copy Markdown
Member

Almost every plugin and theme (Material for MkDocs included) depends on the mkdocs distribution. MkDocs NG ships the same mkdocs/ package under the name mkdocs-ng, so pip install mkdocs-ng mkdocs-material also installs upstream MkDocs 1.6.1. Both write the same files (169 of them overlap): whichever is installed last wins, and pip uninstall mkdocs, which the README recommended, deletes the mkdocs command and most of MkDocs NG along with it. The users affected most are the ones adopting MkDocs NG, and nothing tells them.

This PR does the short-term part: detect it at runtime, and document installs that work.

Changes

Runtime warning (mkdocs/utils/dist_conflict.py)

  • The mkdocs CLI group, mkdocs.build() and mkdocs.serve() look up the mkdocs distribution with importlib.metadata. If its files are in the same place as the running mkdocs package, they log one WARNING with the upstream version, why it's a problem, and the repair command. The command is uv pip … when the upstream package's INSTALLER is uv, otherwise pip …, plus a link to the new docs section.
  • Strict mode: the check runs before the config is loaded and before build() attaches its strict-mode counter, so the warning never aborts a --strict build. It runs once per process (functools.cache), so mkdocs build (CLI + API) and serve rebuilds report it once.
  • No false positives: only mkdocs-ng installed → no warning. Upstream mkdocs installed somewhere else (another site-packages, or this repo's own hatch run docs:…, which puts the checkout first on PYTHONPATH) → no warning, since nothing is overwritten. A distribution whose metadata name isn't mkdocs is ignored.
  • Blind spot: if upstream was installed last, its __main__.py/__init__.py are the ones running, and no check in MkDocs NG can run. The docs therefore tell users to check mkdocs --version (1.6.x = overwritten).

Docs

  • docs/user-guide/installation.md: new section Installing plugins and themes. It covers the uv approach ([tool.uv] override-dependencies = ["mkdocs; sys_platform == 'never'"], exclude-dependencies for uv ≥ 0.9.8, and --overrides for uv pip, uvx and uv tool install), the pip workaround with its limitations, CI with a pinned requirements file, and switching from upstream MkDocs.
  • README: replaced the pip uninstall mkdocs && pip install -U mkdocs-ng instruction with a short callout and the working commands. The same one-liner in fixed-upstream-issues.md is updated too.
  • Release note under 2.0.0.

Before / After

1. pip: plugin installed next to MkDocs NG

Before (main, mkdocs-ng 1.8.0 from PyPI):

$ pip install -q mkdocs-ng mkdocs-material mkdocs-minify-plugin
$ pip list | grep -E '^mkdocs( |-ng )'
mkdocs                     1.6.1
mkdocs-ng                  1.8.0
$ mkdocs build --strict 2>&1 | grep -v '^INFO'; echo "exit=${PIPESTATUS[0]}"
exit=0                                  # nothing tells the user
$ pip uninstall -q -y mkdocs            # what the README said to do
$ mkdocs --version
bash: mkdocs: command not found
$ python -c "import mkdocs.commands.build"
ModuleNotFoundError: No module named 'mkdocs.commands'

After (this branch, wheel built from it):

$ pip install -q mkdocs-material mkdocs-minify-plugin
$ pip install -q --force-reinstall --no-deps ./mkdocs_ng-1.8.0-py3-none-any.whl
$ pip list | grep -E '^mkdocs( |-ng )'
mkdocs                     1.6.1
mkdocs-ng                  1.8.0
$ mkdocs build --strict 2>&1 | grep -v '^INFO'; echo "exit=${PIPESTATUS[0]}"
WARNING -  The upstream 'mkdocs' package (version 1.6.1) is installed in the same environment as 'mkdocs-ng'. Both install the same 'mkdocs/' files, so MkDocs NG may have been partly overwritten, and 'pip uninstall mkdocs' would remove MkDocs NG too. It's usually pulled in by a plugin or theme that depends on 'mkdocs'. To repair this environment, run:

    pip uninstall -y mkdocs && pip install --force-reinstall --no-deps mkdocs-ng

To keep it from coming back, see https://mkdocs-ng.github.io/mkdocs/user-guide/installation/#installing-plugins-and-themes
exit=0                                  # strict build still passes
$ pip uninstall -q -y mkdocs && pip install -q --force-reinstall --no-deps ./mkdocs_ng-1.8.0-py3-none-any.whl
$ pip list | grep -E '^mkdocs( |-ng )'
mkdocs-ng                  1.8.0
$ mkdocs build --strict 2>&1 | grep -v '^INFO'; echo "exit=${PIPESTATUS[0]}"
exit=0                                  # repaired, no warning

With uv as the installer, the suggested command is uv pip uninstall mkdocs && uv pip install --reinstall --no-deps mkdocs-ng. I ran that end to end as well.

2. uv project: documented override

# pyproject.toml: dependencies = ["mkdocs-ng", "mkdocs-material", "mkdocs-minify-plugin"]
$ uv sync && uv pip list | grep -E '^mkdocs( |-ng )'
mkdocs                     1.6.1        # before: upstream pulled in
mkdocs-ng                  1.8.0

# + [tool.uv] override-dependencies = ["mkdocs; sys_platform == 'never'"]
$ rm -rf .venv && uv sync && uv pip list | grep -E '^mkdocs( |-ng )'
mkdocs-ng                  1.8.0        # after: only MkDocs NG
$ uv run mkdocs build --strict
INFO    -  Documentation built in 0.32 seconds

3. Behavior of the warning

Environment Before After
only mkdocs-ng no output no output
mkdocs-ng files + upstream mkdocs metadata (same site-packages) silent one WARNING with repair command, exit 0 even with --strict
upstream mkdocs in a different location silent silent (nothing overwritten)
upstream installed last (its files win) silently runs upstream 1.6.1 same; can't be detected from inside, so the docs tell users to check mkdocs --version
mkdocs build via CLI (group + mkdocs.build() both check) – warning printed once

How every documented command was tested

Each one ran in a fresh temporary venv (Python 3.12, macOS) against a Material + minify site built with --strict:

  • override-dependencies in a uv project: uv sync, uv run mkdocs build --strict, a later uv add mkdocs-minify-plugin, and uv sync --locked from an empty venv. The upstream package is never installed. Also checked with uv 0.4.0, 0.6.0 and 0.9.28.
  • exclude-dependencies = ["mkdocs"]: works with uv 0.9.8 and 0.9.28. uv 0.9.7 and older warn unknown field exclude-dependencies and install upstream anyway. That's why the docs recommend override-dependencies and give the version.
  • uv pip install -r requirements.txt --overrides overrides.txt, uvx --from mkdocs-ng --with mkdocs-material --overrides overrides.txt mkdocs build, and uv tool install mkdocs-ng --with mkdocs-material --overrides overrides.txt: no upstream package, and the strict build passes.
  • pip (real pip, PyPI mkdocs-ng): the three-step install, the CI variant with mkdocs-ng==1.8.0 pinned in requirements.txt, and switching from an existing mkdocs + Material + minify environment. The limitations listed in the docs were reproduced: pip check reports mkdocs-material 9.7.7 requires mkdocs, which is not installed., and a later pip install mkdocs-redirects brings upstream 1.6.1 back.
  • The project docs build with hatch run docs:mkdocs build --strict. The new anchors (#installing-plugins-and-themes, #with-uv-recommended, #with-pip, #switching-from-upstream-mkdocs) match the links and the URL in the warning.
  • Unit tests: 12 new tests in mkdocs/tests/utils/dist_conflict_tests.py. They use real *.dist-info directories on a patched sys.path, so importlib.metadata name matching (mkdocs vs mkdocs_ng) is exercised for real. They cover: only NG, upstream at the same or another location, the name guard, the pip/uv/no-INSTALLER commands, once per process, mkdocs.build() with strict: true, mkdocs.serve(), and mkdocs build --strict through the CLI warning once with exit 0. Full suite: 918 tests OK on Python 3.10 and 3.14. uvx prek run -a passes.

Research: should MkDocs NG move to its own import name, like ProperDocs?

I installed properdocs 1.6.7 and read how it works:

  • It ships a properdocs/ package and a properdocs command. It doesn't depend on mkdocs, but plugins still do, so upstream mkdocs 1.6.1 is still installed next to it. The two just no longer share files.
  • properdocs/replacement.py, imported by its CLI, inserts a sys.meta_path finder that maps every mkdocs.* import to properdocs.* and sets sys.modules['mkdocs'] = properdocs. Checked: inside the process, Material's BasePlugin is properdocs.plugins.BasePlugin. A plain python -c "import mkdocs" still gets upstream 1.6.1.
  • It reads both mkdocs.themes/mkdocs.plugins and properdocs.* entry point groups, skipping entries from the mkdocs distribution. It looks for properdocs.yml first, then mkdocs.yml.

Pros

  • The file conflict goes away for good: no overwrite, and pip uninstall mkdocs is harmless. No installer tricks needed.
  • It stays safe when upstream ships a final mkdocs 2.0. PyPI already has 2.0.dev2 … 2.0.dev6. Once a final release is out, plugins whose mkdocs requirement has no upper bound will pull 2.0, which isn't compatible, over MkDocs NG's files. That's the strongest argument for a rename.

Cons

  • The command can't stay mkdocs. The console script is a file too (bin/mkdocs), and upstream would still be installed for the plugins, so keeping the name brings the same last-writer-wins conflict back. A real fix needs a new command (mkdocs-ng build), and every CI script, Makefile, gh-deploy doc and tutorial has to change. That breaks the "drop-in, nothing changes" promise in the README.
  • Python API users break. import mkdocs; mkdocs.build() outside our CLI gets upstream (or nothing). The only way to alias at interpreter start is a .pth file, which runs in every Python process in the environment. That's widely disliked, and fragile.
  • Two copies of the classes. Correctness depends on the alias being installed before anything imports upstream mkdocs. If something imports it first (a .pth, a test runner, a plugin's own tooling), there are two Config/BasePlugin hierarchies, and the isinstance failures are hard to debug.
  • Logger names and strict mode. Plugins log to mkdocs.plugins.<name> (directly, or via get_plugin_logger). Strict-mode counting and CLI output would have to cover both the mkdocs and the new logger trees. I didn't check how ProperDocs handles loggers that plugins name directly.
  • Metadata lies. importlib.metadata.version("mkdocs") keeps returning the upstream version (1.6.1), so tools that check the installed distribution see the wrong thing. Upstream's dependencies (e.g. mergedeep) stay installed as dead weight.

Effort estimate (rough): mechanical rename of the package plus ~120 internal import/logger references and ~530 test references, about 1 day. Alias finder, dual entry-point groups that ignore the upstream distribution's built-ins, logger bridging, strict-mode tests, about 2–3 days. New command name, deprecation path for the mkdocs command, migration docs, about 1–2 days. Compatibility runs against popular plugins (Material, mkdocstrings, awesome-nav, redirects, minify, macros…), about 2 days. Total ≈ 1–1.5 weeks, plus ongoing support for alias edge cases.

Recommendation: not now. With this PR, users have a warning plus installs that work (uv override is clean, pip is workable), and the drop-in promise holds. Revisit when upstream publishes a non-pre-release mkdocs 2.0 on PyPI. At that point, a pip-only user's next plugin install could replace MkDocs NG with an incompatible release, and an own import name + compatibility layer becomes worth its cost. Until then, a cheaper follow-up is asking major plugins to add an upper bound (mkdocs<2), which limits the damage either way.

Plugins and themes depend on the 'mkdocs' distribution, so installing
one also installs upstream MkDocs, which writes the same 'mkdocs/' files
and makes 'pip uninstall mkdocs' delete MkDocs NG as well.

The CLI, mkdocs.build() and mkdocs.serve() now log a warning (once per
process, before the build, so strict mode isn't affected) when the
upstream distribution is installed in the same location, with the
commands to repair the environment.

Document how to keep upstream MkDocs out with uv's override-dependencies
or --overrides, and the pip workaround with its limitations.
@github-actions github-actions Bot added the bug Something isn't working label Oct 20, 2026
@codspeed

codspeed Bot commented Oct 20, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 28 untouched benchmarks


Comparing fix/detect-upstream-mkdocs-conflict (49b2868) with main (11e7d4a)

Open in CodSpeed

@shenxianpeng
shenxianpeng deleted the fix/detect-upstream-mkdocs-conflict branch October 20, 2026 17:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant