'Sphinx is building cross-references with broken paths (singlehtml output)

I have a Sphinx project that has the following structure:

projectRoot
|
|__mainDoc
|  |__conf.py
|  |__index.rst
|
|__subsections
   |__subsectionA.rst
   |__subsectionB.rst

In my conf.py, I have set my master_doc variable to "mainDoc/index", and to build the project I run the following command at the projectRoot directory: sphinx-build -c ./mainDoc -b singlehtml . _build

index.rst contains a simple toctree directive that lists the two subsection files like so:

.. toctree::
   :numbered:
   :hidden:
   :maxdepth: 4
   :caption: Contents:

   ../subsections/subsectionA
   ../subsections/subsectionB

subsectionA.rst:

.. _anchorA:

First Title
===========

This is a sample sentence.

subsectionB.rst:

Second Title
============

Please refer to :ref:`this sentence <anchorA>`

So far everything is pretty straightforward, and I was able to successfully build an HTML file. However, the cross-references are broken. Specifically, if the generated HTML file is in _build/maindoc/index.html, the link in subsectionB.rst points to _build/maindoc/maindoc/index.html#anchora, which is a file that doesn't exist since there's an extra maindoc folder in the path.

What is causing that extra folder to be inserted in the link target, and what can I do to fix that? Thanks in advance.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source