Repository navigation
🐛 Resolve links to documents that are symlinks - #1227
Open
nazarli-shabnam wants to merge 1 commit into
Open
nazarli-shabnam wants to merge 1 commit into
nazarli-shabnam wants to merge 1 commit into
Conversation
A link such as `[text](doc.md#id)`, or `project:doc.md#id`, to a document that is a symlink to a file outside the source directory warned "Unknown source document" and was not rendered as a link. The link target was found with `BuildEnvironment.relfn2path`, which resolves symlinks. Sphinx registers the document under the path of the symlink, so the path of its target is not a document. Compute the absolute path of the target without following symlinks, in the two places that look up a document from a link. Closes executablebooks#1055
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 #1055
Problem
A link to a document that is a symlink to a file outside the source directory is not rendered as a link. With
docs/linked.md->../README.md, both of thesewarn
Unknown source document '/path/to/project/README' [myst.xref_missing].render_link_projectandrender_link_unknownfind the target withBuildEnvironment.relfn2path, which ends with.resolve()and so follows the symlink. Sphinx registers the document under the path of the symlink (docs/linked.md), so the resolved path (README.md) is not a document.Fix
Add
SphinxRenderer._link_target_path, which builds the absolute path the same wayrelfn2pathdoes (a leading/is relative to the source directory, otherwise relative to the current document) but withos.path.normpathinstead of.resolve(), and use it in the two places that look up a document from a link.relfn2pathis still used by theincludedirective inmocking.py; following the symlink is fine there because it reads the file.Tests
tests/test_sphinx/test_symlinked_docs.pybuilds a small project intmp_pathwheredocs/linked.mdis a symlink to../README.md, and checks that both link forms build without the warning and givehref="linked.html#target-section". It is skipped where symlinks are not available. The project is created at run time because a symlink cannot be committed reliably (Windows checkouts).Unknown source document '.../project/README'.pytest testswithpip install -e ".[testing]"in apython:3.12container (Sphinx 9.1.1, docutils 0.22.4): 1512 passed, 3 failed. The 3 failures werelinkifytests, because I had not installed thelinkifyextra. With.[testing,linkify],pytest tests/test_renderers/test_myst_config.py tests/test_sphinx tests/test_html: 132 passed.Not verified
ruff,mypyandpre-commitwere not run (not installed on my machine); I kept to the surrounding style and added type annotations.CHANGELOG.mdentry: the file has no unreleased section and recent fix PRs did not touch it.AI disclosure
Claude (Anthropic) helped investigate the cause, write the fix and the test, and draft this description. I reviewed the diff.