Skip to content

Commit d081ea3

Browse files
committed
logging: always show source locations as absolute paths
Nodes attached to the parse tree via the `include` directive have their `source` attribute set to the relative path of the included file. Other nodes in the tree use the full absolute path. For consistent logging, ensure that the node location is always expressed as an absolute path, when the filename is known. See sphinx-contrib/spelling#153 for an example of the effect of the original problem.
1 parent 3636776 commit d081ea3

File tree

2 files changed

+23
-1
lines changed

2 files changed

+23
-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: 20 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,21 @@ 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():
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+
n.line = 100
395+
396+
location = logging.get_node_location(n)
397+
source, line = location.split(':')
398+
399+
assert source == absolute_filename
400+
assert line == '100'

0 commit comments

Comments
 (0)