Skip to content

Commit cc3a957

Browse files
SONARPY-1583: Add explanatory comments for injection of SonarLintCache
1 parent 18fc36c commit cc3a957

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.sonar.api.SonarRuntime;
2828
import org.sonar.api.config.PropertyDefinition;
2929
import org.sonar.api.resources.Qualifiers;
30+
import org.sonar.plugins.python.api.SonarLintCache;
3031
import org.sonar.plugins.python.bandit.BanditRulesDefinition;
3132
import org.sonar.plugins.python.bandit.BanditSensor;
3233
import org.sonar.plugins.python.coverage.PythonCoverageSensor;
@@ -41,7 +42,6 @@
4142
import org.sonar.plugins.python.ruff.RuffSensor;
4243
import org.sonar.plugins.python.warnings.AnalysisWarningsWrapper;
4344
import org.sonar.plugins.python.xunit.PythonXUnitSensor;
44-
import org.sonar.plugins.python.api.SonarLintCache;
4545

4646
public class PythonPlugin implements Plugin {
4747

@@ -71,7 +71,6 @@ public void define(Context context) {
7171
.defaultValue("py")
7272
.build(),
7373

74-
7574
Python.class,
7675

7776
PythonProfile.class,
@@ -221,6 +220,16 @@ static class SonarLintPluginAPIManager {
221220

222221
public void addSonarlintPythonIndexer(Context context, SonarLintPluginAPIVersion sonarLintPluginAPIVersion) {
223222
if (sonarLintPluginAPIVersion.isDependencyAvailable()) {
223+
// Only SonarLintPythonIndexer has the ModuleFileListener dependency.
224+
// However, SonarLintCache can only be used with SonarLintPythonIndexer present at the moment.
225+
// Hence, we also add it here, even if it technically does not share the dependency.
226+
//
227+
// Furthermore, with recent versions of SonarLint, the ModuleFileListener dependency should always be available.
228+
//
229+
// Attention:
230+
// The constructors of PythonSensor currently expect both, SonarLintCache and SonarLintPythonIndexer, to always be available at the
231+
// same time for injection.
232+
// Thus, some care is required when making changes to the addExtension calls here.
224233
context.addExtension(SonarLintCache.class);
225234
context.addExtension(SonarLintPythonIndexer.class);
226235
} else {

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -67,31 +67,33 @@ public final class PythonSensor implements Sensor {
6767
private final SonarLintCache sonarLintCache;
6868
private final AnalysisWarningsWrapper analysisWarnings;
6969
private static final Logger LOG = LoggerFactory.getLogger(PythonSensor.class);
70-
static final String UNSET_VERSION_WARNING =
71-
"Your code is analyzed as compatible with all Python 3 versions by default." +
70+
static final String UNSET_VERSION_WARNING = "Your code is analyzed as compatible with all Python 3 versions by default." +
7271
" You can get a more precise analysis by setting the exact Python version in your configuration via the parameter \"sonar.python.version\"";
7372

7473
/**
7574
* Constructor to be used by pico if neither PythonCustomRuleRepository nor PythonIndexer are to be found and injected.
7675
*/
7776
public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory,
78-
NoSonarFilter noSonarFilter, AnalysisWarningsWrapper analysisWarnings) {
77+
NoSonarFilter noSonarFilter, AnalysisWarningsWrapper analysisWarnings) {
7978
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, null, null, analysisWarnings);
8079
}
8180

8281
public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
83-
PythonCustomRuleRepository[] customRuleRepositories, AnalysisWarningsWrapper analysisWarnings) {
82+
PythonCustomRuleRepository[] customRuleRepositories, AnalysisWarningsWrapper analysisWarnings) {
8483
this(fileLinesContextFactory, checkFactory, noSonarFilter, customRuleRepositories, null, null, analysisWarnings);
8584
}
8685

8786
public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
88-
PythonIndexer indexer, SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
87+
PythonIndexer indexer, SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
88+
// ^^ This constructor implicitly assumes that a PythonIndexer and a SonarLintCache are always available at the same time.
89+
// In practice, this is currently the case, since both are provided by PythonPlugin under the same conditions.
90+
// See also PythonPlugin::SonarLintPluginAPIManager::addSonarlintPythonIndexer.
8991
this(fileLinesContextFactory, checkFactory, noSonarFilter, null, indexer, sonarLintCache, analysisWarnings);
9092
}
9193

9294
public PythonSensor(FileLinesContextFactory fileLinesContextFactory, CheckFactory checkFactory, NoSonarFilter noSonarFilter,
93-
@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer,
94-
@Nullable SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
95+
@Nullable PythonCustomRuleRepository[] customRuleRepositories, @Nullable PythonIndexer indexer,
96+
@Nullable SonarLintCache sonarLintCache, AnalysisWarningsWrapper analysisWarnings) {
9597
this.checks = new PythonChecks(checkFactory)
9698
.addChecks(CheckList.REPOSITORY_KEY, CheckList.getChecks())
9799
.addCustomChecks(customRuleRepositories);

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,9 @@ public void buildOnce(SensorContext context) {
8383
@Override
8484
public void setSonarLintCache(@Nullable SonarLintCache sonarLintCache) {
8585
if (sonarLintCache != null) {
86+
// ^This null check is defensive.
87+
// In practice, a SonarLintCache instance should always be available when a SonarLintPythonIndexer is available.
88+
// See also PythonPlugin::SonarLintPluginAPIManager::addSonarlintPythonIndexer.
8689
this.cacheContext = new CacheContextImpl(true, new PythonWriteCacheImpl(sonarLintCache), new PythonReadCacheImpl(sonarLintCache));
8790
}
8891
}

0 commit comments

Comments
 (0)