Skip to content

Commit 99f4322

Browse files
committed
🐛 FIX: Use docname instead of source path in warning locations
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`).
1 parent 9364edb commit 99f4322

File tree

4 files changed

+33
-1
lines changed

4 files changed

+33
-1
lines changed

myst_parser/warnings_.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,9 @@ def create_warning(
126126
message,
127127
type=type_str,
128128
subtype=subtype_str,
129-
location=node if node is not None else (document["source"], line),
129+
location=node
130+
if node is not None
131+
else (document.settings.env.docname, line),
130132
)
131133
if _is_suppressed_warning(
132134
type_str, subtype_str, document.settings.env.config.suppress_warnings
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
extensions = ["myst_parser"]
2+
exclude_patterns = ["_build"]
3+
myst_enable_extensions = ["substitution"]
4+
myst_substitutions = {"defined": "This is defined"}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Title
2+
3+
{{ defined }}
4+
5+
{{ missing }}

tests/test_sphinx/test_sphinx_builds.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,27 @@ def test_substitutions(
451451
get_sphinx_app_output(app, filename="index.html", regress_html=True)
452452

453453

454+
@pytest.mark.sphinx(
455+
buildername="html",
456+
srcdir=os.path.join(SOURCE_DIR, "substitutions_missing"),
457+
freshenv=True,
458+
)
459+
def test_substitutions_missing(
460+
app,
461+
status,
462+
warning,
463+
):
464+
"""Test that missing substitution generate warning without '.rst' suffix in location."""
465+
app.build()
466+
assert "build succeeded" in status.getvalue() # Build succeeded
467+
warnings = warning.getvalue().strip().splitlines()
468+
assert len(warnings) == 1
469+
assert (
470+
"index.md:5: WARNING: Substitution error:UndefinedError: 'missing' is undefined [myst.substitution]"
471+
in warnings[0]
472+
)
473+
474+
454475
@pytest.mark.sphinx(
455476
buildername="gettext", srcdir=os.path.join(SOURCE_DIR, "gettext"), freshenv=True
456477
)

0 commit comments

Comments
 (0)