Skip to content

Commit 0becc84

Browse files
SONARPY-847 Deactivate Project Symbol Table in SonarLint for big projects (#928)
1 parent d794035 commit 0becc84

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

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

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ public class SonarLintPythonIndexer extends PythonIndexer implements ModuleFileL
3939
private final ModuleFileSystem moduleFileSystem;
4040
private static final Logger LOG = Loggers.get(SonarLintPythonIndexer.class);
4141
private boolean shouldBuildProjectSymbolTable = true;
42+
private static final long DEFAULT_MAX_LINES_FOR_INDEXING = 150_000;
43+
private static final String MAX_LINES_PROPERTY = "sonar.python.sonarlint.indexing.maxlines";
4244

4345
public SonarLintPythonIndexer(ModuleFileSystem moduleFileSystem) {
4446
this.moduleFileSystem = moduleFileSystem;
@@ -50,12 +52,19 @@ public void buildOnce(SensorContext context) {
5052
return;
5153
}
5254
this.projectBaseDirAbsolutePath = context.fileSystem().baseDir().getAbsolutePath();
55+
shouldBuildProjectSymbolTable = false;
5356
List<InputFile> files = getInputFiles(moduleFileSystem);
57+
long nLines = files.stream().map(InputFile::lines).mapToLong(Integer::longValue).sum();
58+
long maxLinesForIndexing = context.config().getLong(MAX_LINES_PROPERTY).orElse(DEFAULT_MAX_LINES_FOR_INDEXING);
59+
if (nLines > maxLinesForIndexing) {
60+
// Avoid performance issues for large projects
61+
LOG.debug("Project symbol table deactivated due to project size (total number of lines is {}, maximum for indexing is {})", nLines, maxLinesForIndexing);
62+
return;
63+
}
5464
LOG.debug("Input files for indexing: " + files);
5565
// computes "globalSymbolsByModuleName"
5666
GlobalSymbolsScanner globalSymbolsStep = new GlobalSymbolsScanner(context);
5767
globalSymbolsStep.execute(files, context);
58-
shouldBuildProjectSymbolTable = false;
5968
}
6069

6170
private static List<InputFile> getInputFiles(ModuleFileSystem moduleFileSystem) {

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -320,6 +320,22 @@ public void cross_files_issues_only_one_file_analyzed() {
320320
assertThat(issue.flows()).isEmpty();
321321
}
322322

323+
@Test
324+
public void no_indexer_when_project_too_large_sonarlint() {
325+
activeRules = new ActiveRulesBuilder()
326+
.addRule(new NewActiveRule.Builder()
327+
.setRuleKey(RuleKey.of(CheckList.REPOSITORY_KEY, "S930"))
328+
.build())
329+
.build();
330+
context.setSettings(new MapSettings().setProperty("sonar.python.sonarlint.indexing.maxlines", 1));
331+
332+
InputFile mainFile = inputFile("main.py");
333+
PythonIndexer pythonIndexer = pythonIndexer(Collections.singletonList(mainFile));
334+
sensor(CUSTOM_RULES, pythonIndexer).execute(context);
335+
assertThat(context.allIssues()).isEmpty();
336+
assertThat(logTester.logs(LoggerLevel.DEBUG)).contains("Project symbol table deactivated due to project size (total number of lines is 4, maximum for indexing is 1)");
337+
}
338+
323339
@Test
324340
public void loop_in_class_hierarchy() {
325341
activeRules = new ActiveRulesBuilder()

0 commit comments

Comments
 (0)