From d28792537baff2f8d454ac02bf19c7823f1bef94 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Wed, 13 May 2026 10:32:05 +0200 Subject: [PATCH] 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> --- .../writing-diagnostics/diagnostics.expected | 14 ++++++------ python/extractor/semmle/logging.py | 22 +++++++++++++++---- 2 files changed, 25 insertions(+), 11 deletions(-) diff --git a/python/extractor/cli-integration-test/writing-diagnostics/diagnostics.expected b/python/extractor/cli-integration-test/writing-diagnostics/diagnostics.expected index de218a50e1e..12a241ad7b6 100644 --- a/python/extractor/cli-integration-test/writing-diagnostics/diagnostics.expected +++ b/python/extractor/cli-integration-test/writing-diagnostics/diagnostics.expected @@ -17,13 +17,13 @@ ] }, "location": { - "file": "/repo_dir/syntaxerror3.py", + "file": "syntaxerror3.py", "startColumn": 0, "endColumn": 0, "startLine": 1, "endLine": 1 }, - "markdownMessage": "A parse error occurred while processing `/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.", + "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.", "severity": "warning", "source": { "extractorName": "python", @@ -56,13 +56,13 @@ ] }, "location": { - "file": "/repo_dir/syntaxerror1.py", + "file": "syntaxerror1.py", "startColumn": 0, "endColumn": 0, "startLine": 3, "endLine": 3 }, - "markdownMessage": "A parse error occurred while processing `/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.", + "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.", "severity": "warning", "source": { "extractorName": "python", @@ -95,13 +95,13 @@ ] }, "location": { - "file": "/repo_dir/syntaxerror2.py", + "file": "syntaxerror2.py", "startColumn": 0, "endColumn": 0, "startLine": 5, "endLine": 5 }, - "markdownMessage": "A parse error occurred while processing `/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.", + "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.", "severity": "warning", "source": { "extractorName": "python", @@ -145,7 +145,7 @@ ] }, "location": { - "file": "/repo_dir/recursion_error.py" + "file": "recursion_error.py" }, "plaintextMessage": "maximum recursion depth exceeded while calling a Python object", "severity": "error", diff --git a/python/extractor/semmle/logging.py b/python/extractor/semmle/logging.py index 0e0b173a4d7..6f31bff4f39 100644 --- a/python/extractor/semmle/logging.py +++ b/python/extractor/semmle/logging.py @@ -359,11 +359,25 @@ def get_stack_trace_lines(): return lines[:i] return lines +def _get_source_root(): + """Get the source root directory for relativizing diagnostic paths.""" + return os.environ.get("LGTM_SRC", os.getcwd()) + +def _relative_path(path): + """Make a path relative to the source root for use in diagnostic locations. + If the path is not under the source root, return it unchanged.""" + source_root = _get_source_root() + relpath = os.path.relpath(path, source_root) + if relpath.startswith(os.pardir): + return path + return relpath + def syntax_error_message(exception, unit): - l = Location(file=unit.path, startLine=exception.lineno, startColumn=exception.offset) + diag_path = _relative_path(unit.path) + l = Location(file=diag_path, startLine=exception.lineno, startColumn=exception.offset) error = (DiagnosticMessage(Source("py/diagnostics/syntax-error", "Could not process some files due to syntax errors"), Severity.WARNING) .with_location(l) - .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)) + .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)) .attribute("traceback", get_stack_trace_lines()) .attribute("args", exception.args) .status_page() @@ -374,7 +388,7 @@ def syntax_error_message(exception, unit): def recursion_error_message(exception, unit): # if unit is a BuiltinModuleExtractable, there will be no path attribute - l = Location(file=unit.path) if hasattr(unit, "path") else None + l = Location(file=_relative_path(unit.path)) if hasattr(unit, "path") else None return (DiagnosticMessage(Source("py/diagnostics/recursion-error", "Recursion error in Python extractor"), Severity.ERROR) .with_location(l) .text(exception.args[0]) @@ -385,7 +399,7 @@ def recursion_error_message(exception, unit): def internal_error_message(exception, unit): # if unit is a BuiltinModuleExtractable, there will be no path attribute - l = Location(file=unit.path) if hasattr(unit, "path") else None + l = Location(file=_relative_path(unit.path)) if hasattr(unit, "path") else None return (DiagnosticMessage(Source("py/diagnostics/internal-error", "Internal error in Python extractor"), Severity.ERROR) .with_location(l) .text("Internal error")