Skip to content

Commit c1e26f9

Browse files
authored
Merge pull request #21847 from github/redsun82/redsun82-python-absolute-paths-in-diagno
Python extractor: use relative paths in diagnostic locations
2 parents 49a435c + adf59f3 commit c1e26f9

2 files changed

Lines changed: 30 additions & 11 deletions

File tree

python/extractor/cli-integration-test/writing-diagnostics/diagnostics.expected

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
]
1818
},
1919
"location": {
20-
"file": "<test-root-directory>/repo_dir/syntaxerror3.py",
20+
"file": "syntaxerror3.py",
2121
"startColumn": 0,
2222
"endColumn": 0,
2323
"startLine": 1,
2424
"endLine": 1
2525
},
26-
"markdownMessage": "A parse error occurred while processing `<test-root-directory>/repo_dir/syntaxerror3.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
26+
"markdownMessage": "A parse error occurred while processing `syntaxerror3.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
2727
"severity": "warning",
2828
"source": {
2929
"extractorName": "python",
@@ -56,13 +56,13 @@
5656
]
5757
},
5858
"location": {
59-
"file": "<test-root-directory>/repo_dir/syntaxerror1.py",
59+
"file": "syntaxerror1.py",
6060
"startColumn": 0,
6161
"endColumn": 0,
6262
"startLine": 3,
6363
"endLine": 3
6464
},
65-
"markdownMessage": "A parse error occurred while processing `<test-root-directory>/repo_dir/syntaxerror1.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
65+
"markdownMessage": "A parse error occurred while processing `syntaxerror1.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
6666
"severity": "warning",
6767
"source": {
6868
"extractorName": "python",
@@ -95,13 +95,13 @@
9595
]
9696
},
9797
"location": {
98-
"file": "<test-root-directory>/repo_dir/syntaxerror2.py",
98+
"file": "syntaxerror2.py",
9999
"startColumn": 0,
100100
"endColumn": 0,
101101
"startLine": 5,
102102
"endLine": 5
103103
},
104-
"markdownMessage": "A parse error occurred while processing `<test-root-directory>/repo_dir/syntaxerror2.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
104+
"markdownMessage": "A parse error occurred while processing `syntaxerror2.py`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.",
105105
"severity": "warning",
106106
"source": {
107107
"extractorName": "python",
@@ -145,7 +145,7 @@
145145
]
146146
},
147147
"location": {
148-
"file": "<test-root-directory>/repo_dir/recursion_error.py"
148+
"file": "recursion_error.py"
149149
},
150150
"plaintextMessage": "maximum recursion depth exceeded while calling a Python object",
151151
"severity": "error",

python/extractor/semmle/logging.py

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,30 @@ def get_stack_trace_lines():
359359
return lines[:i]
360360
return lines
361361

362+
def _get_source_root():
363+
"""Get the source root directory for relativizing diagnostic paths."""
364+
return os.environ.get("LGTM_SRC", os.getcwd())
365+
366+
def _relative_path(path):
367+
"""Make a path relative to the source root for use in diagnostic locations.
368+
If the path is not under the source root, return it unchanged."""
369+
source_root = os.path.abspath(_get_source_root())
370+
abs_path = os.path.abspath(path)
371+
try:
372+
relpath = os.path.relpath(abs_path, source_root)
373+
except ValueError:
374+
# On Windows, relpath raises ValueError for paths on different drives
375+
return path
376+
if relpath.startswith(os.pardir):
377+
return path
378+
return relpath.replace(os.sep, "/")
379+
362380
def syntax_error_message(exception, unit):
363-
l = Location(file=unit.path, startLine=exception.lineno, startColumn=exception.offset)
381+
diag_path = _relative_path(unit.path)
382+
l = Location(file=diag_path, startLine=exception.lineno, startColumn=exception.offset)
364383
error = (DiagnosticMessage(Source("py/diagnostics/syntax-error", "Could not process some files due to syntax errors"), Severity.WARNING)
365384
.with_location(l)
366-
.markdown("A parse error occurred while processing `{}`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.".format(unit.path))
385+
.markdown("A parse error occurred while processing `{}`, and as a result this file could not be analyzed. Check the syntax of the file using the `python -m py_compile` command and correct any invalid syntax.".format(diag_path))
367386
.attribute("traceback", get_stack_trace_lines())
368387
.attribute("args", exception.args)
369388
.status_page()
@@ -374,7 +393,7 @@ def syntax_error_message(exception, unit):
374393

375394
def recursion_error_message(exception, unit):
376395
# if unit is a BuiltinModuleExtractable, there will be no path attribute
377-
l = Location(file=unit.path) if hasattr(unit, "path") else None
396+
l = Location(file=_relative_path(unit.path)) if hasattr(unit, "path") else None
378397
return (DiagnosticMessage(Source("py/diagnostics/recursion-error", "Recursion error in Python extractor"), Severity.ERROR)
379398
.with_location(l)
380399
.text(exception.args[0])
@@ -385,7 +404,7 @@ def recursion_error_message(exception, unit):
385404

386405
def internal_error_message(exception, unit):
387406
# if unit is a BuiltinModuleExtractable, there will be no path attribute
388-
l = Location(file=unit.path) if hasattr(unit, "path") else None
407+
l = Location(file=_relative_path(unit.path)) if hasattr(unit, "path") else None
389408
return (DiagnosticMessage(Source("py/diagnostics/internal-error", "Internal error in Python extractor"), Severity.ERROR)
390409
.with_location(l)
391410
.text("Internal error")

0 commit comments

Comments
 (0)