Repository navigation
🐛 FIX: Point nested {eval-rst} nodes at the real document - #1242
Open
tony1233200 wants to merge 1 commit into
Open
tony1233200 wants to merge 1 commit into
tony1233200 wants to merge 1 commit into
Conversation
`render_restructuredtext` parses the block into a temporary document that shares the page's settings, then merges its children with `Element.extend`. That only sets `.document` on the direct children, so every deeper node kept pointing at the temporary document. Sphinx strips `settings.env` from its own copy of the settings before it pickles a doctree, but not from the temporary document's settings, so a stray node pulled the whole `BuildEnvironment` into the page's `.doctree`. Re-parent all merged descendants to the real document. Fixes executablebooks#1241
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1241.
Problem
{eval-rst}blocks leave nested nodes pointing at a temporary document, so every pickled doctree that contains such a block also embeds the SphinxBuildEnvironment. The cost grows with the size of the environment, for every page that uses{eval-rst}.Root cause
DocutilsRenderer.render_restructuredtextparses the block into a temporary document (newdoc) that shares the page'ssettings, then merges its children withElement.extend.extend->setup_childonly sets.documenton the direct children, so every deeper node (for example the contents of a nested bullet list) keeps.documentset tonewdoc.Sphinx's
write_doctreesetsenv = Noneon its own copy of the document settings before pickling, butnewdoc.settingsis the original shared object, which still hasenv. Pickling a stray node followsnode.document -> newdoc -> settings.env.Fix
After merging, walk the merged children and point every descendant element at the real document (3 lines in
myst_parser/mdit_to_docutils/base.py).Evidence
Regression test
tests/test_docutils.py::test_eval_rst_nested_nodes_belong_to_document(an{eval-rst}block with a nested bullet list, asserting every element's.documentis the real document):Real Sphinx build of a one-page project with the same block, size of the pickled
index.doctree:BuildEnvironment.BuildEnvironment.Suite and checks (Python 3.12, Sphinx 9.1.1, docutils 0.22.4,
linkify-it-pyinstalled):pytest tests: 1566 passed, 11 skipped. (Withoutlinkify-it-py, 3 linkify tests fail identically on the unmodified code.)pre-commit run --files <changed files>: ruff check, ruff format, mypy, end-of-file and trailing-whitespace all pass.