diff --git a/myst_parser/warnings_.py b/myst_parser/warnings_.py index e4312f7f..4653b9c3 100644 --- a/myst_parser/warnings_.py +++ b/myst_parser/warnings_.py @@ -126,7 +126,9 @@ def create_warning( message, type=type_str, subtype=subtype_str, - location=node if node is not None else (document["source"], line), + location=node + if node is not None + else (document.settings.env.docname, line), ) if _is_suppressed_warning( type_str, subtype_str, document.settings.env.config.suppress_warnings diff --git a/tests/test_sphinx/sourcedirs/substitutions_missing/conf.py b/tests/test_sphinx/sourcedirs/substitutions_missing/conf.py new file mode 100644 index 00000000..251ee3a5 --- /dev/null +++ b/tests/test_sphinx/sourcedirs/substitutions_missing/conf.py @@ -0,0 +1,4 @@ +extensions = ["myst_parser"] +exclude_patterns = ["_build"] +myst_enable_extensions = ["substitution"] +myst_substitutions = {"defined": "This is defined"} diff --git a/tests/test_sphinx/sourcedirs/substitutions_missing/index.md b/tests/test_sphinx/sourcedirs/substitutions_missing/index.md new file mode 100644 index 00000000..45cb85f9 --- /dev/null +++ b/tests/test_sphinx/sourcedirs/substitutions_missing/index.md @@ -0,0 +1,5 @@ +# Title + +{{ defined }} + +{{ missing }} diff --git a/tests/test_sphinx/test_sphinx_builds.py b/tests/test_sphinx/test_sphinx_builds.py index 4ac336e4..93549dc4 100644 --- a/tests/test_sphinx/test_sphinx_builds.py +++ b/tests/test_sphinx/test_sphinx_builds.py @@ -451,6 +451,27 @@ def test_substitutions( get_sphinx_app_output(app, filename="index.html", regress_html=True) +@pytest.mark.sphinx( + buildername="html", + srcdir=os.path.join(SOURCE_DIR, "substitutions_missing"), + freshenv=True, +) +def test_substitutions_missing( + app, + status, + warning, +): + """Test that missing substitution generate warning without '.rst' suffix in location.""" + app.build() + assert "build succeeded" in status.getvalue() # Build succeeded + warnings = warning.getvalue().strip().splitlines() + assert len(warnings) == 1 + assert ( + "index.md:5: WARNING: Substitution error:UndefinedError: 'missing' is undefined [myst.substitution]" + in warnings[0] + ) + + @pytest.mark.sphinx( buildername="gettext", srcdir=os.path.join(SOURCE_DIR, "gettext"), freshenv=True )