From 63db95e519cc4d785c69fc7f7e7e9ed6f99a16f7 Mon Sep 17 00:00:00 2001 From: tony1233200 <127579663+tony1233200@users.noreply.github.com> Date: Thu, 19 Nov 2026 22:29:23 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Point=20nested=20`{eval-r?= =?UTF-8?q?st}`=20nodes=20at=20the=20real=20document?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit `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 #1241 --- myst_parser/mdit_to_docutils/base.py | 6 ++++++ tests/test_docutils.py | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/myst_parser/mdit_to_docutils/base.py b/myst_parser/mdit_to_docutils/base.py index 7b1808af..bbafc76e 100644 --- a/myst_parser/mdit_to_docutils/base.py +++ b/myst_parser/mdit_to_docutils/base.py @@ -1888,6 +1888,12 @@ def render_restructuredtext(self, token: SyntaxTreeNode) -> None: if node["names"]: self.document.note_explicit_target(node, node) self.current_node.extend(newdoc.children) + # ``extend`` only re-parents the direct children, so point every nested node + # at the real document, not the temporary one (which shares the ``settings.env`` + # that Sphinx strips from its own copy of the settings before pickling the doctree) + for node in newdoc.children: + for descendant in node.findall(nodes.Element): + descendant.document = self.document def render_directive( self, diff --git a/tests/test_docutils.py b/tests/test_docutils.py index cabb2c03..49c355f0 100644 --- a/tests/test_docutils.py +++ b/tests/test_docutils.py @@ -274,6 +274,23 @@ def test_field_list_body_source_line(): assert bodies[0].line # a real line, not 0/None +def test_eval_rst_nested_nodes_belong_to_document(): + """Nodes created inside an ``{eval-rst}`` block must point at the real document. + + Regression: the block is parsed into a temporary document sharing the page's + settings, and only the direct children were re-parented when it was merged, so + deeper nodes kept ``.document`` set to the temporary document. Sphinx removes + ``settings.env`` from its own copy of the settings before pickling a doctree, but + not from the temporary document's settings, so those nodes dragged the whole + ``BuildEnvironment`` into the pickled doctree. + """ + source = "```{eval-rst}\n* item one\n\n * nested item\n\n paragraph\n```\n" + doctree = publish_doctree(source=source, parser=Parser()) + elements = list(doctree.findall(nodes.Element)) + assert any(isinstance(node, nodes.list_item) for node in elements) + assert [node.tagname for node in elements if node.document is not doctree] == [] + + def test_linkify_disabled_without_linkify_it_py(monkeypatch): """Enabling ``linkify`` without ``linkify-it-py`` warns and disables it.