Skip to content

Commit 054cd8f

Browse files
committed
SONARPY-2059 PythonScanner should log the parsing exception on the correct line for notebooks (#1912)
1 parent fd05909 commit 054cd8f

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

sonar-python-plugin/src/main/java/org/sonar/plugins/python/PythonScanner.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -121,12 +121,16 @@ protected void scanFile(PythonInputFile inputFile) throws IOException {
121121
}
122122
} catch (RecognitionException e) {
123123
visitorContext = new PythonVisitorContext(pythonFile, e, context.runtime().getProduct());
124+
125+
var line = (inputFile.kind() == PythonInputFile.Kind.IPYTHON) ? ((GeneratedIPythonFile) inputFile).locationMap().get(e.getLine()).line() : e.getLine();
126+
var newMessage = e.getMessage().replace("line " + e.getLine(), "line " + line);
127+
124128
LOG.error("Unable to parse file: " + inputFile);
125-
LOG.error(e.getMessage());
129+
LOG.error(newMessage);
126130
context.newAnalysisError()
127131
.onFile(inputFile.wrappedFile())
128-
.at(inputFile.wrappedFile().newPointer(e.getLine(), 0))
129-
.message(e.getMessage())
132+
.at(inputFile.wrappedFile().newPointer(line, 0))
133+
.message(newMessage)
130134
.save();
131135
}
132136
List<PythonSubscriptionCheck> checksBasedOnTree = new ArrayList<>();

sonar-python-plugin/src/test/java/org/sonar/plugins/python/IPynbSensorTest.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import java.util.List;
2727
import org.junit.jupiter.api.BeforeEach;
2828
import org.junit.jupiter.api.Test;
29+
import org.junit.jupiter.api.extension.RegisterExtension;
2930
import org.mockito.Mockito;
3031
import org.slf4j.event.Level;
3132
import org.sonar.api.SonarRuntime;
@@ -76,7 +77,7 @@ class IPynbSensorTest {
7677

7778
private ActiveRules activeRules;
7879

79-
@org.junit.Rule
80+
@RegisterExtension
8081
public LogTesterJUnit5 logTester = new LogTesterJUnit5().setLevel(Level.DEBUG);
8182

8283
@BeforeEach
@@ -227,4 +228,14 @@ void test_notebook_sensor_does_not_execute_cpd_measures() {
227228
assertThat(tokensLines)
228229
.isNull();
229230
}
231+
232+
@Test
233+
void test_notebook_sensor_parse_error_on_valid_line(){
234+
inputFile("notebook_parse_error.ipynb");
235+
activeRules = new ActiveRulesBuilder().build();
236+
var sensor = notebookSensor();
237+
sensor.execute(context);
238+
var logs = String.join("", logTester.logs());
239+
assertThat(logs).contains("Unable to parse file: notebook_parse_error.ipynbParse error at line 1");
240+
}
230241
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"cells":[{"cell_type":"code","execution_count":null,"metadata":{},"outputs":[],"source":["a = 3"]},{"cell_type":"code","execution_count":1,"metadata":{},"outputs":[{"ename":"SyntaxError","evalue":"incomplete input (2552824149.py, line 1)","output_type":"error","traceback":["\u001b[0;36m Cell \u001b[0;32mIn[1], line 1\u001b[0;36m\u001b[0m\n\u001b[0;31m def main(\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m incomplete input\n"]}],"source":[" def main(): ..."]}],"metadata":{"kernelspec":{"display_name":"Python 3","language":"python","name":"python3"},"language_info":{"codemirror_mode":{"name":"ipython","version":3},"file_extension":".py","mimetype":"text/x-python","name":"python","nbconvert_exporter":"python","pygments_lexer":"ipython3","version":"3.12.2"}},"nbformat":4,"nbformat_minor":2}

0 commit comments

Comments
 (0)