Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

“'myst' reference target not found: b.md#anchor” when building in parallel #411

Closed
andersk opened this issue Aug 10, 2021 · 7 comments · Fixed by #525
Closed

“'myst' reference target not found: b.md#anchor” when building in parallel #411

andersk opened this issue Aug 10, 2021 · 7 comments · Fixed by #525
Labels
bug Something isn't working

Comments

@andersk
Copy link

andersk commented Aug 10, 2021

Describe the problem

I configured myst_heading_anchors = 2 as documented, but anchor links are giving me this warning if I enable parallelism (sphinx-build -W -j N for any N other than 1):

Warning, treated as error:
/tmp/myst-anchor-test/docs/dir/a.md:2:'myst' reference target not found: b.md#anchor

When building without parallelism, there is no warning and the anchor works fine.

Link to your repository or website

https://github.com/andersk/myst-anchor-test

Steps to reproduce

$ pip install Sphinx myst-parser

Successfully installed Jinja2-3.0.1 MarkupSafe-2.0.1 Pygments-2.9.0 Sphinx-4.1.2 alabaster-0.7.12 attrs-21.2.0 babel-2.9.1 certifi-2021.5.30 charset-normalizer-2.0.4 docutils-0.17.1 idna-3.2 imagesize-1.2.0 markdown-it-py-1.1.0 mdit-py-plugins-0.2.8 myst-parser-0.15.1 packaging-21.0 pyparsing-2.4.7 pytz-2021.1 pyyaml-5.4.1 requests-2.26.0 snowballstemmer-2.1.0 sphinxcontrib-applehelp-1.0.2 sphinxcontrib-devhelp-1.0.2 sphinxcontrib-htmlhelp-2.0.0 sphinxcontrib-jsmath-1.0.1 sphinxcontrib-qthelp-1.0.3 sphinxcontrib-serializinghtml-1.1.5 urllib3-1.26.6
$ git clone https://github.com/andersk/myst-anchor-test.git
$ cd myst-anchor-test
$ sphinx-build -W -j2 docs build
Running Sphinx v4.1.2

Warning, treated as error:
/tmp/myst-anchor-test/docs/dir/a.md:2:'myst' reference target not found: b.md#anchor
$ sphinx-build -W docs build
Running Sphinx v4.1.2
build succeeded.

The HTML pages are in build.

If you want to reproduce again, rm -rf build.

The version of Python you're using

3.9.6

Your operating system

NixOS 21.11

Versions of your packages

$ pip freeze
alabaster==0.7.12
attrs==21.2.0
Babel==2.9.1
certifi==2021.5.30
charset-normalizer==2.0.4
docutils==0.17.1
idna==3.2
imagesize==1.2.0
Jinja2==3.0.1
markdown-it-py==1.1.0
MarkupSafe==2.0.1
mdit-py-plugins==0.2.8
myst-parser==0.15.1
packaging==21.0
Pygments==2.9.0
pyparsing==2.4.7
pytz==2021.1
PyYAML==5.4.1
requests==2.26.0
snowballstemmer==2.1.0
Sphinx==4.1.2
sphinxcontrib-applehelp==1.0.2
sphinxcontrib-devhelp==1.0.2
sphinxcontrib-htmlhelp==2.0.0
sphinxcontrib-jsmath==1.0.1
sphinxcontrib-qthelp==1.0.3
sphinxcontrib-serializinghtml==1.1.5
urllib3==1.26.6

Additional context

No response

@andersk andersk added the bug Something isn't working label Aug 10, 2021
@welcome
Copy link

welcome bot commented Aug 10, 2021

Thanks for opening your first issue here! Engagement like this is essential for open source projects! 🤗

If you haven't done so already, check out EBP's Code of Conduct. Also, please try to follow the issue template as it helps other community members to contribute more effectively.

If your issue is a feature request, others may react to it, to raise its prominence (see Feature Voting).

Welcome to the EBP community! 🎉

@chrisjsewell
Copy link
Member

thanks, I'll try to have a look soon(ish) 😬

@andersk
Copy link
Author

andersk commented Dec 28, 2021

Not fixed in 90c98aa (#467), despite #467 being marked as related.

@chrisjsewell
Copy link
Member

Not fixed in 90c98aa (#467), despite #467 being marked as related.

Well that's why it as related and not fixed 😅

I had a brief look in that PR, but could not see anything obviously wrong with the myst-parser implementation.

The issue is possibly to do with the merging of global environments from the parallel processes, after reading documents, here: https://github.com/sphinx-doc/sphinx/blob/a284aa65717f7a0c494c4c0859d2158094284ad1/sphinx/environment/__init__.py#L316-L317

The myst-parser renderer add to the standard domain here:

domain.anonlabels[doc_slug] = self.doc_env.docname, labelid
domain.labels[doc_slug] = (
self.doc_env.docname,
labelid,
clean_astext(section[0]),
)

Then sphinx should merge this into the final global environment here: https://github.com/sphinx-doc/sphinx/blob/a284aa65717f7a0c494c4c0859d2158094284ad1/sphinx/domains/std.py#L727-L743

The data should then be available for the post-transform (performed after all documents have been read), here:

def _resolve_anchor(

I would maybe check if the final global environment: docs/_build/html/.doctrees/environment.pickle actually contains all the requisite data that it should (compared to the serial build)

I probably won't have time to do this soon, but you or anyone is welcome to have a go

@andersk
Copy link
Author

andersk commented Feb 23, 2022

I’m not exactly sure what to look for, but I do notice that the pickled environment from the parallel build is missing the myst_anchors attribute that the one from the serial build has.

>>> a = pickle.load(open("build-serial/.doctrees/environment.pickle", "rb"))
>>> a
<sphinx.environment.BuildEnvironment object at 0x7f47e873feb0>
>>> a.myst_anchors
True
>>> b = pickle.load(open("build-parallel/.doctrees/environment.pickle", "rb"))
>>> b
<sphinx.environment.BuildEnvironment object at 0x7f47e8701fd0>
>>> b.myst_anchors
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'BuildEnvironment' object has no attribute 'myst_anchors'

@andersk
Copy link
Author

andersk commented Feb 23, 2022

Indeed, removing the myst_anchors test seems to fix the problem completely:

--- a/myst_parser/myst_refs.py
+++ b/myst_parser/myst_refs.py
@@ -203,7 +203,7 @@ class MystReferenceResolver(ReferencesResolver):
     ) -> Optional[Element]:
         """Resolve doc with anchor."""
         target = node["reftarget"]  # type: str
-        if not ("#" in target and hasattr(self.env, "myst_anchors")):
+        if "#" not in target:
             return None
         # the link may be a heading anchor; we need to first get the relative path
         rel_path, anchor = target.rsplit("#", 1)

@chrisjsewell
Copy link
Member

Its okay, I know why it is, fixing now

andersk added a commit to andersk/zulip that referenced this issue Feb 24, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip that referenced this issue Feb 24, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip that referenced this issue Feb 25, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to andersk/zulip that referenced this issue Feb 25, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
andersk added a commit to zulip/zulip that referenced this issue Mar 1, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
tiger-yash pushed a commit to tiger-yash/zulip that referenced this issue Mar 8, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
N-Shar-ma pushed a commit to N-Shar-ma/zulip that referenced this issue Mar 18, 2022
Work around executablebooks/MyST-Parser#411.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
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 a pull request may close this issue.

2 participants