Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
]
},
"location": {
"file": "<test-root-directory>/repo_dir/syntaxerror3.py",
"file": "syntaxerror3.py",
Comment thread
redsun82 marked this conversation as resolved.
"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.",
Comment thread
redsun82 marked this conversation as resolved.
"severity": "warning",
"source": {
"extractorName": "python",
Expand Down Expand Up @@ -56,13 +56,13 @@
]
},
"location": {
"file": "<test-root-directory>/repo_dir/syntaxerror1.py",
"file": "syntaxerror1.py",
Comment thread
redsun82 marked this conversation as resolved.
"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.",
Comment thread
redsun82 marked this conversation as resolved.
"severity": "warning",
"source": {
"extractorName": "python",
Expand Down Expand Up @@ -95,13 +95,13 @@
]
},
"location": {
"file": "<test-root-directory>/repo_dir/syntaxerror2.py",
"file": "syntaxerror2.py",
Comment thread
redsun82 marked this conversation as resolved.
"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.",
Comment thread
redsun82 marked this conversation as resolved.
"severity": "warning",
"source": {
"extractorName": "python",
Expand Down Expand Up @@ -145,7 +145,7 @@
]
},
"location": {
"file": "<test-root-directory>/repo_dir/recursion_error.py"
"file": "recursion_error.py"
Comment thread
redsun82 marked this conversation as resolved.
},
"plaintextMessage": "maximum recursion depth exceeded while calling a Python object",
"severity": "error",
Expand Down
27 changes: 23 additions & 4 deletions python/extractor/semmle/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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])
Expand All @@ -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")
Expand Down
Loading