Skip to content

.. include:: directive shouldn't care about filename extension #8515

Closed

Description

@brechtm

index.rst:

.. |subst| replace:: Substitution

.. include:: a.rst

.. include:: b.rst

a.rst:

A: |subst|

b.rst:

B: |subst|

The output is correct:

A: Substitution

B: Substitution

But building the Sphinx projects yields these warnings:

a.rst:2: WARNING: Undefined substitution referenced: "subst".
b.rst:2: WARNING: Undefined substitution referenced: "subst".

Replacing the extension on a.rst and b.rst with .txt suppresses the warnings. This is apparently because Sphinx scans files with the .rst extension twice. I hope you agree that is very confusing, and the user should be able to use the .rst extension also for included files?

Environment info

  • OS: Linux
  • Python version: 3.6.9
  • Sphinx version: 3.2.1

Activity

  1. tk0miya commented on Dec 8, 2020

    @tk0miya
    Member

    Yes. As you quoted, Sphinx processes all .rst files under the source directory. So the warnings are correct to me. What do you expect?

  2. changed the title [-]Invalid "WARNING: Undefined substitution referenced"[/-] [+].. include:: directive shouldn't care about filename extension[/+] on Dec 10, 2020
  3. brechtm commented on Dec 10, 2020

    @brechtm
    ContributorAuthor

    My issue is that this is confusing behavior. How should the user know that they shouldn't use the .rst extension when .. include::ing a file? Why do they even need to think about this?

    Sphinx simply shouldn't output a warning even if they included files have a .rst extension.

  4. tk0miya commented on Dec 11, 2020

    @tk0miya
    Member

    At present, Sphinx scans the target documents from the source directory by file extensions. After that, it reads them all. To prevent warnings (or skip processing), we need to know the "included" file before processing. How do we check it? I think it is hard to know until reading all of the target documents.

  5. delameter commented on Jul 20, 2022

    @delameter

    My issue is that this is confusing behavior. How should the user know that they shouldn't use the .rst extension when .. include::ing a file? Why do they even need to think about this?

    Wow, thanks! That was the issue. Warnings were counter-intuitive insofar as possible. Changed extensions of files invoked by "include" to .rsti, it helped

  6. added this to the some future version milestone on Sep 29, 2022
  7. picnixz commented on Aug 16, 2023

    @picnixz
    Member

    A future improvement could be that such warnings are delayed as much as possible but it's very difficult since it would require more post-processing (and won't be worth the effort most of the time).

    With tk0miya's answer, I think this issue should be closed (you'll still get warnings/errors if you use rst documents like that, so it's an expected behaviour). However, I'll just change the labels and leave it open.

  8. added
    type:proposal
    and removed on Aug 16, 2023
  9. jdillard commented on Aug 26, 2026

    @jdillard
    SponsorMember

    Closing as expected behavior, but I made a docs PR, #14649, to give tips on how to avoid common issues if you want to give it a review.

  10. locked as resolved and limited conversation to collaborators on Sep 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    type:docstype:proposal

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions