Skip to content

Commit 9c9a52d

Browse files
authored
logging: always show source locations as absolute paths (#10460)
1 parent 6ef22d2 commit 9c9a52d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

sphinx/util/logging.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
from sphinx.errors import SphinxWarning
1414
from sphinx.util.console import colorize
15+
from sphinx.util.osutil import abspath
1516

1617
if TYPE_CHECKING:
1718
from sphinx.application import Sphinx
@@ -514,6 +515,8 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator):
514515

515516
def get_node_location(node: Node) -> Optional[str]:
516517
(source, line) = get_source_line(node)
518+
if source:
519+
source = abspath(source)
517520
if source and line:
518521
return "%s:%s" % (source, line)
519522
elif source:

tests/test_util_logging.py

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import codecs
44
import os
5+
import os.path
56

67
import pytest
78
from docutils import nodes
89

910
from sphinx.errors import SphinxWarning
1011
from sphinx.testing.util import strip_escseq
11-
from sphinx.util import logging
12+
from sphinx.util import logging, osutil
1213
from sphinx.util.console import colorize
1314
from sphinx.util.logging import is_suppressed_warning, prefixed_warnings
1415
from sphinx.util.parallel import ParallelTasks
@@ -379,3 +380,18 @@ def test_prefixed_warnings(app, status, warning):
379380
assert 'WARNING: Another PREFIX: message3' in warning.getvalue()
380381
assert 'WARNING: PREFIX: message4' in warning.getvalue()
381382
assert 'WARNING: message5' in warning.getvalue()
383+
384+
385+
def test_get_node_location_abspath():
386+
# Ensure that node locations are reported as an absolute path,
387+
# even if the source attribute is a relative path.
388+
389+
relative_filename = os.path.join('relative', 'path.txt')
390+
absolute_filename = osutil.abspath(relative_filename)
391+
392+
n = nodes.Node()
393+
n.source = relative_filename
394+
395+
location = logging.get_node_location(n)
396+
397+
assert location == absolute_filename + ':'

0 commit comments

Comments
 (0)