Skip to content

Commit d287925

Browse files
redsun82Copilot
andcommitted
Python extractor: use relative paths in diagnostic locations
Diagnostic `Location.file` fields contained absolute filesystem paths, causing the GitHub UI to generate broken file links with runner paths like `/home/runner/work/...`. Now paths are relativized against the source root (`LGTM_SRC` or cwd), falling back to absolute if the file is outside the source root. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent 0b808e1 commit d287925

2 files changed

Lines changed: 25 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: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -359,11 +359,25 @@ 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 = _get_source_root()
370+
relpath = os.path.relpath(path, source_root)
371+
if relpath.startswith(os.pardir):
372+
return path
373+
return relpath
374+
362375
def syntax_error_message(exception, unit):
363-
l = Location(file=unit.path, startLine=exception.lineno, startColumn=exception.offset)
376+
diag_path = _relative_path(unit.path)
377+
l = Location(file=diag_path, startLine=exception.lineno, startColumn=exception.offset)
364378
error = (DiagnosticMessage(Source("py/diagnostics/syntax-error", "Could not process some files due to syntax errors"), Severity.WARNING)
365379
.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))
380+
.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))
367381
.attribute("traceback", get_stack_trace_lines())
368382
.attribute("args", exception.args)
369383
.status_page()
@@ -374,7 +388,7 @@ def syntax_error_message(exception, unit):
374388

375389
def recursion_error_message(exception, unit):
376390
# if unit is a BuiltinModuleExtractable, there will be no path attribute
377-
l = Location(file=unit.path) if hasattr(unit, "path") else None
391+
l = Location(file=_relative_path(unit.path)) if hasattr(unit, "path") else None
378392
return (DiagnosticMessage(Source("py/diagnostics/recursion-error", "Recursion error in Python extractor"), Severity.ERROR)
379393
.with_location(l)
380394
.text(exception.args[0])
@@ -385,7 +399,7 @@ def recursion_error_message(exception, unit):
385399

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

0 commit comments

Comments
 (0)