Skip to content

Install the mkdocs package out of upstream MkDocs' way - #115

Merged
shenxianpeng merged 4 commits into
mainfrom
feature/isolated-install
Nov 1, 2026
Merged

shenxianpeng merged 4 commits into
mainfrom
feature/isolated-install

Conversation

@shenxianpeng

@shenxianpeng shenxianpeng commented Oct 20, 2026 •

Copy link
Copy Markdown
Member

Almost every plugin and theme (Material for MkDocs included) depends on the mkdocs distribution. Installing one alongside MkDocs NG therefore also installs upstream MkDocs into site-packages/mkdocs/, the same directory MkDocs NG installs to. Whichever package is written last wins, often silently: a plain pip install mkdocs-ng mkdocs-material ends up running upstream 1.6.1. pip uninstall mkdocs, which the README recommended, deletes most of MkDocs NG along with it.

This PR fixes the root cause. MkDocs NG no longer shares files with upstream MkDocs. It supersedes #114, whose warning and pip workarounds aren't needed once the files are separate.

How it works

  • The wheel installs the package to site-packages/mkdocs_ng/mkdocs/ instead of site-packages/mkdocs/ (hatch sources rewrite). The source tree is unchanged.

  • mkdocs_ng.start (Python 3.15+, PEP 829) and mkdocs_ng.pth (older versions) run a tiny bootstrap (mkdocs_ng/__init__.py, ~80 lines, stdlib only) at interpreter startup. It inserts a meta path finder at the front of sys.meta_path that resolves the top-level mkdocs package from mkdocs_ng/. Submodules then resolve through mkdocs.__path__, so they are never mixed with upstream files.

  • The mkdocs console script now points to mkdocs_ng:main, which installs the finder itself. The command therefore works even where .pth files are skipped. If upstream's launcher was written last, it runs from mkdocs.__main__ import cli, which the hook routes to MkDocs NG.

  • The module name stays mkdocs, so mkdocs.plugins, logger names (mkdocs.plugins.*, which strict mode counts), class __module__s, mkdocs.yml, the mkdocs command and the mkdocs.plugins/mkdocs.themes entry point groups are all unchanged. There is nothing to migrate for users or plugin authors.

  • Builtin themes: get_themes() only counted themes registered by the mkdocs distribution as builtin. It now also counts mkdocs-ng's, so a third-party package can't silently replace mkdocs/readthedocs when upstream isn't installed. The same themes registered by both distributions are no longer reported as a duplicate.

  • Editable installs: hatch can't map a prefix rewrite in dev mode, so dev-mode-dirs = ["."] adds the repo root to sys.path. mkdocs_ng.pth is force-included in the editable wheel as well, and the bootstrap finds <repo>/mkdocs. The checkout therefore wins over an installed upstream package, and the PYTHONPATH = "{root}" workarounds in the docs and integration hatch environments are removed.

  • Startup cost of the .pth hook is about 0.4 ms (-X importtime). It only looks up PathFinder on sys.meta_path when mkdocs is actually imported, so it doesn't import importlib.machinery or warnings at startup.

  • Python 3.15+ (PEP 829): 3.15 deprecates import lines in .pth files, and 3.18 will ignore them silently. With the matching mkdocs_ng.start (mkdocs_ng:install), 3.15.0 reports "import lines in mkdocs_ng.pth are suppressed due to matching mkdocs_ng.start file" and runs the entry point instead. Older versions ignore the .start file. The .pth keeps the path line plus the PEP's recommended straddling import line.

  • Precedent: setuptools ships the same mechanism, distutils-precedence.pth + _distutils_hack.DistutilsMetaFinder, to make import distutils load its own copy.

Why not a ProperDocs-style rename

ProperDocs ships a properdocs package plus a properdocs command, and aliases mkdocs.* imports only inside its own CLI process. To leave upstream's files alone it has to drop the mkdocs command, because bin/mkdocs is also a shared file. That breaks every CI script, Makefile and tutorial. import mkdocs in user scripts also still gets upstream. Here the mkdocs launcher may be shared because both launchers run the same code, the import hook covers every process, and nothing gets renamed.

Before / After

Same Material + search + minify site with strict: true, fresh venv, pip. Before = mkdocs-ng==1.8.0 from PyPI; after = a wheel built from this branch (version bumped to 2.0.0.dev0 so upgrades can be tested).

Before:

$ pip install -q mkdocs-ng==1.8.0 mkdocs-material mkdocs-minify-plugin
$ pip list | grep -E '^mkdocs( |-ng )'
mkdocs                     1.6.1
mkdocs-ng                  1.8.0
$ mkdocs --version
mkdocs, version 1.6.1 from site-packages/mkdocs (Python 3.12)      # upstream runs, silently
$ mkdocs build -q && echo 'strict build: OK'
strict build: OK
# upgrade/reinstall a plugin, which reinstalls upstream mkdocs
$ pip install -q --force-reinstall --no-deps mkdocs==1.6.1
$ mkdocs --version
mkdocs, version 1.6.1 from site-packages/mkdocs (Python 3.12)
$ pip uninstall -q -y mkdocs
$ python -m mkdocs --version
.../bin/python: No module named mkdocs.__main__; 'mkdocs' is a package and cannot be directly executed

After:

$ pip install -q ./mkdocs_ng-2.0.0.dev0-py3-none-any.whl mkdocs-material mkdocs-minify-plugin
$ pip list | grep -E '^mkdocs( |-ng )'
mkdocs                     1.6.1
mkdocs-ng                  2.0.0.dev0
$ mkdocs --version
mkdocs, version 2.0.0.dev0 from site-packages/mkdocs_ng/mkdocs (Python 3.12)
$ mkdocs build -q && echo 'strict build: OK'
strict build: OK
# upgrade/reinstall a plugin, which reinstalls upstream mkdocs
$ pip install -q --force-reinstall --no-deps mkdocs==1.6.1
$ mkdocs --version
mkdocs, version 2.0.0.dev0 from site-packages/mkdocs_ng/mkdocs (Python 3.12)
$ pip uninstall -q -y mkdocs
$ python -m mkdocs --version
python -m mkdocs, version 2.0.0.dev0 from site-packages/mkdocs_ng/mkdocs (Python 3.12)
$ python -m mkdocs build -q && echo 'strict build: OK'
strict build: OK

Behavior

Scenario Before After
pip install mkdocs-ng mkdocs-material upstream 1.6.1 may run (depends on write order) MkDocs NG
upstream reinstalled after MkDocs NG (plugin upgrade) upstream 1.6.1 runs MkDocs NG
import mkdocs / mkdocs.build() in a user script whichever files won MkDocs NG
Material's BasePlugin comes from whichever files won mkdocs_ng/mkdocs/plugins.py
pip uninstall mkdocs package deleted, mkdocs and python -m mkdocs broken package intact, python -m mkdocs works; only the shared mkdocs launcher is removed (pip install --force-reinstall --no-deps mkdocs-ng restores it)
switching from upstream MkDocs pip uninstall mkdocs && pip install mkdocs-ng, breaks on the next plugin install pip install -U mkdocs-ng
upgrading from mkdocs-ng 1.8.0 (old layout) with plugins – pip install -U mkdocs-ng → MkDocs NG runs from the new location
editable dev install with upstream also installed needed PYTHONPATH={root} checkout wins by itself

Verification

Unit tests (mkdocs/tests/mkdocs_ng_tests.py, 11 tests, plus 2 in utils_tests.py):

  • _find_root handles the wheel layout and the source checkout, and never falls back to upstream's site-packages/mkdocs/.
  • The finder only handles the top-level mkdocs.
  • install() is idempotent.
  • SitePackagesTests start a fresh python -I -S on a fake site-packages that contains both packages. With site.addsitedir, import mkdocs gives MkDocs NG, mkdocs.plugins loads from mkdocs_ng/mkdocs/, an upstream-only module can't be imported (no mixing), upstream's launcher (from mkdocs.__main__ import cli) runs MkDocs NG, mkdocs_ng.main() works without .pth processing, and it works without upstream installed.
  • With the import line removed from mkdocs_ng.pth, the two key tests fail, as they should.

Full suite and checks:

  • 921 tests OK on Python 3.10 and 3.15.0. On 3.15.0, with the import line removed from the .pth, the subprocess tests still pass: the .start file alone drives the hook. The new tests and utils_tests also pass on PyPy 3.11.
  • uvx prek run -a passes, and hatch run types:check (now mypy mkdocs mkdocs_ng) passes.
  • hatch run integration.py3.12-default:test passes without the PYTHONPATH workaround.
  • hatch run docs:mkdocs build --strict passes. In that environment upstream 1.6.1 is installed, and mkdocs --version reports the checkout.
  • The CI package check now expects mkdocs_ng/mkdocs/..., mkdocs_ng/__init__.py and mkdocs_ng.pth in the wheel, and fails if the wheel contains a top-level mkdocs/. I ran it locally against uv build output.

End to end with real installers (fresh venvs, the site above with --strict):

  • pip and uv pip:
    • fresh install;
    • upstream force-reinstalled after MkDocs NG;
    • mkdocs.build() from a script;
    • uninstalling upstream (python -m mkdocs keeps working, reinstalling MkDocs NG restores the launcher);
    • switching from mkdocs + Material + minify with pip install -U;
    • upgrading from mkdocs-ng==1.8.0 with plugins (before the upgrade it was running upstream 1.6.1);
    • upgrading from mkdocs-ng==1.8.0 alone.
  • uv project with and without override-dependencies, and uvx --overrides.
  • Python 3.10, 3.12, 3.14, PyPy 3.11 and 3.15.0: Material + minify strict build, with upstream reinstalled last. Also checked: install from the sdist, mkdocs serve (page and Material CSS served), mkdocs new, -t readthedocs, multiprocessing spawn children, python -I, and uninstalling mkdocs-ng (upstream works again, no leftovers).

Follow-up commits

  • 5d87976: CodSpeed flagged test_load_config (34.3 → 39.8 ms, −13.75%), and I reproduced it locally (min 1.69 → 1.98 ms). Root cause: Theme clears get_themes()'s cache on every config load, and EntryPoint.dist.name re-parses METADATA on every access. Counting mkdocs-ng's themes as builtin doubled those parses. Each name is now read once, and timing is back to main (min 1.69 ms vs 1.69 ms).
  • 00ca6d8: added mkdocs_ng.start for Python 3.15+ (see above).

Docs

  • Installation: new section Plugins, themes and the mkdocs package. It explains why upstream MkDocs gets installed and why that's harmless, says to leave it installed (and how to restore the command if it was uninstalled), adds an optional Keeping upstream MkDocs out (uv override-dependencies / --overrides, tested), and Switching from upstream MkDocs (just pip install -U mkdocs-ng). The mkdocs --version example shows the new path.
  • README and Fixed Upstream Issues: the switch command is now pip install -U mkdocs-ng.
  • Release note under 2.0.0 › Fixed.

Limitations

  • import mkdocs from your own code relies on Python processing .pth files, which it does by default (including -I). The mkdocs command doesn't rely on it. Where .pth files are skipped (python -S, pip install --target dirs added via PYTHONPATH, some bundlers), a script's import mkdocs can find upstream instead. The docs note this.
  • pip uninstall mkdocs still removes the mkdocs launcher, because both distributions record bin/mkdocs. The package is unaffected, and the docs give the one-line restore.
  • With upstream also installed, static type checkers (mypy) resolve mkdocs to upstream's sources, while runtime uses MkDocs NG. With only MkDocs NG installed, mypy finds it through the .pth path line. This only matters to plugin authors; uv's override keeps upstream out.
  • Upstream MkDocs and its dependencies still get installed (unused) unless kept out with uv's override.

Almost every plugin and theme depends on the 'mkdocs' distribution, so
installing one also installs upstream MkDocs into site-packages/mkdocs/,
the same directory MkDocs NG installed to. Whichever was installed last
won, and 'pip uninstall mkdocs' deleted most of MkDocs NG.

The wheel now installs the package to site-packages/mkdocs_ng/mkdocs/.
mkdocs_ng.pth runs a tiny bootstrap at interpreter startup that puts a
meta path finder first, so 'import mkdocs' loads MkDocs NG in any install
order, with module names, logger names and entry points unchanged. The
'mkdocs' console script also installs the finder itself, so the command
works even where .pth files are skipped.

Editable installs add the repository root to sys.path (hatch can't map
the prefix rewrite), and the same hook keeps the checkout ahead of an
installed upstream package, so the PYTHONPATH workarounds in the docs and
integration environments are gone.

Builtin themes registered by mkdocs-ng now count as builtin, and the
same themes registered by upstream MkDocs are no longer reported as a
conflict.
@github-actions github-actions Bot added the enhancement New feature or request 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 feature/isolated-install (1ffa55a) with main (f03b4ba)

Open in CodSpeed

EntryPoint.dist.name parses the distribution's metadata on every access,
and get_themes() runs for every config load because Theme clears its
cache. Counting mkdocs-ng's themes as builtin made the loop read the name
twice more, slowing load_config by about 15% (CodSpeed: test_load_config
34.3 ms -> 39.8 ms). Read each name once.
Python 3.15 deprecates import lines in .pth files in favour of .start
files that name entry points. With a matching mkdocs_ng.start, 3.15+
runs mkdocs_ng:install from it and suppresses the .pth import line
(no deprecation notice under -v); older versions ignore the .start file
and keep running the import line. Without it, 3.18+ would silently
skip the hook.
@shenxianpeng shenxianpeng mentioned this pull request Oct 30, 2026
2 of 3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant