Skip to content

Commit 22ea974

Browse files
Avoid trying to save descriptors to cache when their computation failed (#1304)
1 parent e727b42 commit 22ea974

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

python-frontend/src/main/java/org/sonar/python/semantic/ProjectLevelSymbolTable.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ public void insertEntry(String moduleName, Set<Descriptor> descriptors) {
169169
this.globalDescriptorsByModuleName.put(moduleName, descriptors);
170170
}
171171

172+
@CheckForNull
172173
public Set<Descriptor> descriptorsForModule(String moduleName) {
173174
return globalDescriptorsByModuleName.get(moduleName);
174175
}

sonar-python-plugin/src/main/java/org/sonar/plugins/python/indexer/SonarQubePythonIndexer.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -154,8 +154,13 @@ private void saveGlobalSymbolsInCache(List<InputFile> files) {
154154
for (InputFile inputFile : files) {
155155
String moduleFQN = inputFileToFQN.get(inputFile);
156156
Set<Descriptor> descriptors = projectLevelSymbolTable().descriptorsForModule(moduleFQN);
157-
caching.writeProjectLevelSymbolTableEntry(inputFile.key(), descriptors);
158-
caching.writeImportsMapEntry(inputFile.key(), projectLevelSymbolTable().importsByModule().get(moduleFQN));
157+
Set<String> imports = projectLevelSymbolTable().importsByModule().get(moduleFQN);
158+
if (descriptors != null && imports != null) {
159+
// Descriptors/imports map may be null if the file failed to parse.
160+
// We don't try to save information in the cache in that case.
161+
caching.writeProjectLevelSymbolTableEntry(inputFile.key(), descriptors);
162+
caching.writeImportsMapEntry(inputFile.key(), imports);
163+
}
159164
}
160165
}
161166

sonar-python-plugin/src/test/java/org/sonar/plugins/python/indexer/SonarQubePythonIndexerTest.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -326,6 +326,17 @@ public void test_regular_scan_when_scan_without_parsing_fails() {
326326
assertThat(logTester.logs(LoggerLevel.INFO)).contains("1/1 source file has been analyzed");
327327
}
328328

329+
@Test
330+
public void test_no_data_in_cache_for_parse_error() {
331+
file1 = createInputFile(baseDir, "parse_error.py", InputFile.Status.ADDED, InputFile.Type.MAIN);
332+
333+
List<InputFile> inputFiles = new ArrayList<>(List.of(file1));
334+
335+
pythonIndexer = new SonarQubePythonIndexer(inputFiles, cacheContext);
336+
pythonIndexer.buildOnce(context);
337+
assertThat(writeCache.getData().containsKey(projectSymbolTableCacheKey("moduleKey:parse_error.py"))).isFalse();
338+
}
339+
329340
private byte[] importsAsByteArray(List<String> mod) {
330341
return String.join(";", mod).getBytes(StandardCharsets.UTF_8);
331342
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello! I'm a parse error!

0 commit comments

Comments
 (0)