From 99f432246d92c97c27079ba56451c461f99b9a97 Mon Sep 17 00:00:00 2001 From: Ilya Boyazitov Date: Fri, 20 Mar 2026 21:36:15 +0300 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20FIX:=20Use=20docname=20instead?= =?UTF-8?q?=20of=20source=20path=20in=20warning=20locations?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Changed warning location generation to use `document.settings.env.docname` instead of `document["source"]`. This ensures warning locations display clean file names (e.g., `index.md`) rather than full paths with potential `.rst` suffixes (e.g., `index.md.rst`). --- myst_parser/warnings_.py | 4 +++- .../sourcedirs/substitutions_missing/conf.py | 4 ++++ .../sourcedirs/substitutions_missing/index.md | 5 +++++ tests/test_sphinx/test_sphinx_builds.py | 21 +++++++++++++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) create mode 100644 tests/test_sphinx/sourcedirs/substitutions_missing/conf.py create mode 100644 tests/test_sphinx/sourcedirs/substitutions_missing/index.md 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 )