relative target path for intersphinx leads to broken links

Dan Callaghan avatarDan Callaghan created an issue

The intersphinx docs say: "Relative local paths for target locations are taken as relative to the base of the built documentation". But that doesn't work when the document is in a subdirectory. The href should have ../ prepended as many times as needed, but it doesn't.

For example, an intersphinx mapping like this:

intersphinx_mapping = {
    'other': ('../other/', '../other/other.inv'),
}

when used in a document such as topic/details.rst, will link to ../other/something whereas it should link to ../../other/something.

I had a go at fixing this myself, I got as far as figuring out that it should be calling app.builder.get_relative_uri, something like this:

--- a/sphinx/ext/intersphinx.py
+++ b/sphinx/ext/intersphinx.py
@@ -220,7 +220,8 @@ def missing_reference(app, env, node, contnode):
             if objtype not in inventory or target not in inventory[objtype]:
                 continue
             proj, version, uri, dispname = inventory[objtype][target]
-            newnode = nodes.reference('', '', internal=False, refuri=uri,
+            newnode = nodes.reference('', '', internal=False,
+                          refuri=app.builder.get_relative_uri('WHAT GOES HERE?', uri),
                           reftitle=_('(in %s v%s)') % (proj, version))
             if node.get('refexplicit'):
                 # use whatever title was given

but I can't figure out how the missing_reference hook can tell what the source document path is (I was expecting I could do something like node.document.name but that is not a thing).

Comments (3)

  1. WAKAYAMA shirou

    Currently intersphinx assumes the base directory is the build output dir.

    So I think you rewrite the intersphinx_mapping in the conf.py to relative path from the build output dir (../../other/something).

    If add get_relative_uri() by your suggestion, some other documents may become broken.

  2. Log in to comment
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.