Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

logging: always show source locations as absolute paths #10460

Merged
merged 1 commit into from Jun 16, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
3 changes: 3 additions & 0 deletions sphinx/util/logging.py
Expand Up @@ -12,6 +12,7 @@

from sphinx.errors import SphinxWarning
from sphinx.util.console import colorize
from sphinx.util.osutil import abspath

if TYPE_CHECKING:
from sphinx.application import Sphinx
Expand Down Expand Up @@ -514,6 +515,8 @@ class WarningLogRecordTranslator(SphinxLogRecordTranslator):

def get_node_location(node: Node) -> Optional[str]:
(source, line) = get_source_line(node)
if source:
source = abspath(source)
if source and line:
return "%s:%s" % (source, line)
elif source:
Expand Down
18 changes: 17 additions & 1 deletion tests/test_util_logging.py
Expand Up @@ -2,13 +2,14 @@

import codecs
import os
import os.path

import pytest
from docutils import nodes

from sphinx.errors import SphinxWarning
from sphinx.testing.util import strip_escseq
from sphinx.util import logging
from sphinx.util import logging, osutil
from sphinx.util.console import colorize
from sphinx.util.logging import is_suppressed_warning, prefixed_warnings
from sphinx.util.parallel import ParallelTasks
Expand Down Expand Up @@ -379,3 +380,18 @@ def test_prefixed_warnings(app, status, warning):
assert 'WARNING: Another PREFIX: message3' in warning.getvalue()
assert 'WARNING: PREFIX: message4' in warning.getvalue()
assert 'WARNING: message5' in warning.getvalue()


def test_get_node_location_abspath():
# Ensure that node locations are reported as an absolute path,
# even if the source attribute is a relative path.

relative_filename = os.path.join('relative', 'path.txt')
absolute_filename = osutil.abspath(relative_filename)

n = nodes.Node()
n.source = relative_filename

location = logging.get_node_location(n)

assert location == absolute_filename + ':'