mirror of
https://github.com/github/codeql.git
synced 2026-05-24 16:17:07 +02:00
Merge pull request #21847 from github/redsun82/redsun82-python-absolute-paths-in-diagno
Python extractor: use relative paths in diagnostic locations
This commit is contained in:
@@ -17,13 +17,13 @@
|
||||
]
|
||||
},
|
||||
"location": {
|
||||
"file": "<test-root-directory>/repo_dir/syntaxerror3.py",
|
||||
"file": "syntaxerror3.py",
|
||||
"startColumn": 0,
|
||||
"endColumn": 0,
|
||||
"startLine": 1,
|
||||
"endLine": 1
|
||||
},
|
||||
"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.",
|
||||
"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": "<test-root-directory>/repo_dir/syntaxerror1.py",
|
||||
"file": "syntaxerror1.py",
|
||||
"startColumn": 0,
|
||||
"endColumn": 0,
|
||||
"startLine": 3,
|
||||
"endLine": 3
|
||||
},
|
||||
"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.",
|
||||
"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": "<test-root-directory>/repo_dir/syntaxerror2.py",
|
||||
"file": "syntaxerror2.py",
|
||||
"startColumn": 0,
|
||||
"endColumn": 0,
|
||||
"startLine": 5,
|
||||
"endLine": 5
|
||||
},
|
||||
"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.",
|
||||
"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": "<test-root-directory>/repo_dir/recursion_error.py"
|
||||
"file": "recursion_error.py"
|
||||
},
|
||||
"plaintextMessage": "maximum recursion depth exceeded while calling a Python object",
|
||||
"severity": "error",
|
||||
|
||||
@@ -359,11 +359,30 @@ 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 = os.path.abspath(_get_source_root())
|
||||
abs_path = os.path.abspath(path)
|
||||
try:
|
||||
relpath = os.path.relpath(abs_path, source_root)
|
||||
except ValueError:
|
||||
# On Windows, relpath raises ValueError for paths on different drives
|
||||
return path
|
||||
if relpath.startswith(os.pardir):
|
||||
return path
|
||||
return relpath.replace(os.sep, "/")
|
||||
|
||||
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 +393,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 +404,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")
|
||||
|
||||
Reference in New Issue
Block a user