Trying to add quickfixes to a custom plugin

Please provide:

  • Operating system: Windows 10
  • IDE name and flavor/env: IntelliJ with SonarQube for IDE version 10.23.0.81344

And a thorough description of the problem / question:

I’m currently working on a custom plugin with a few custom Java rules. For some those rules I would like to add quickfixes, however, currently they are not showing up when I open an issue in the IDE. Unfortunately, I couldn’t find any documentation on how to create your own quickfixes so if anyone has something I could look into, I’d be very grateful.

This is the code snippet where I attempt to add the quickfixes:

for (String value : values) {
    NewQuickFix quickFix = issue.newQuickFix().message("Change value to " + value);
    NewInputFileEdit inputFileEdit = quickFix.newInputFileEdit().on(inputFile);
    NewTextEdit textEdit = inputFileEdit.newTextEdit().at(location);
    if (value.matches("[0-9]+")) {
        textEdit = textEdit.withNewText(value);
    } else {
        textEdit = textEdit.withNewText("\"" + value + "\"");
    }
    inputFileEdit = inputFileEdit.addTextEdit(textEdit);
    quickFix = quickFix.addInputFileEdit(inputFileEdit);
    issue.addQuickFix(quickFix);
}
issue.setQuickFixAvailable(true);

I have confirmed that the values array is not empty so that can’t be the problem and there aren’t any errors being thrown.

Thanks in advance!

Hello @brec , thanks for sharing with the community.

It would help me a lot if you could share the whole rule code.

Also which is the process you follow with your rule that includes the quickfix and SonarQube / SonarQube for IDE in terms of compilation, etc ? I mean in order to see it in SonarQube for IDE.

Meanwhile here you can see a built in rule with a quickfix : sonar-java/java-checks/src/main/java/org/sonar/java/checks/MathClampRangeCheck.java at c44fcee0e9c22a1b0662b8c443771617db430d82 · SonarSource/sonar-java · GitHub

Thank you for your answer.

About compilation and viewing issues in IDE:
The plugin is a shaded maven jar with SonarLint support set to true. However, we use a custom sensor for the analysis instead of tying into the SonarJava analyzer and I’m aware that (from what I understand) SonarQube for IDE doesn’t execute custom sensors. Therefore, the analysis is currently only run from the command line, but through Connected Mode the issues can still be opened in the IDE. I was hoping that this would be enough to make quickfixes available, since creating issues from a SensorContext (NewIssue) appears to have methods for this, as I used above.

As for rule code:
The plugin is for integrating a SAST tool (as a maven dependency, in case that’s important) that analyses a jar. This means that inside our sensor the process is to generate the jar → run the tool which creates a json report → create a SonarQube issue for every issue in the json report. Thus, the only actual “rule code” we have is the RulesDefinition where the repository is created and the html + json files that contain the descriptions, tags, etc. In the json files the rules have a quickfix field set to partial, covered, infeasible…

So the exact process is (after the SAST tool analysis):
The issues from the json report are mapped to the rules and (before the code snippet I already posted) a NewIssue is created with the rule, location and error message → if the error message from the json report contains quickfix options then those options are added (the code snippet) → the issue is saved and then shows up in SonarQube. If quickfixesAvailable was set to true, then the issue in SonarQube has a SonarQube for IDE icon with the text “Quick fix”, but when opening the issue in the IDE I can’t seem to find the quickfix.

Do you think the problem could be solely from the fact that a custom sensor is used? Because I was hoping that I won’t have to try to change that part, but unfortunately the example in the link only works with a JavaFileScannerContext, at least from what I can see.

Also, is there some kind of API endpoint that I could use to check if the quickfixes actually “arrived” in the SonarQube server? I looked through the Web API but didn’t see anything for the actual quickfixes, only the parameter quickfixAvaliable.

Thanks again in advance for your help!

Thanks a lot for your detailed information, and apologies for this late reply (so many conferences lately)

I’m in conversations with the SonarQube for IDE team, and we’ll come up with an answer.

1 Like

Hey there @brec! :waving_hand:

Do you think the problem could be solely from the fact that a custom sensor is used?

Yes, this is the root cause. Currently, SonarQube for IDE will only display QuickFixes for the issues that were detected in the IDE through local analysis. QuickFixes are not sent to the IDE as part of the Open in IDE request.

I will flag this for our PMs as an insight, as it is effectively a feature request. I’m not sure how much priority it will get though…

Hope that helps and all the best,
Sophio :sunflower:

2 Likes

Thank you for the reply! I guess I need to find a work around for my use case. However, I do wonder why you can add quickfixes via the SensorContext if they can’t actually be applied anywhere. Maybe you could add a note about this in the documentation of the methods? Because this whole thing was very confusing to me…

1 Like

For sure, it’s not an easy topic at all!!

I will flag this for the documentation team in case there is something to be improved. Just out of curiosity, which docs page(s) have you been referring to up-to-now (if any)?

Best,
Sophio

Maybe I was just blind, but I couldn’t actually find any documentation on how to do quickfixes. Everything I have found was by manually going through the sonar-plugin-api git repo (and then finding the NewIssue class with the quickfix methods) but of course that only has a very general description of the methods’ purpose.

Also, after I first started actually attempting quickfixes I also noticed that I couldn’t seem to set “quickfixAvailable” to true for any issue (using the setQuickfixAvailable method from NewIssue). Only after looking at the json files for the regular sonar-java rules I found the quickfix tag (which is not mentioned in CUSTOM RULES 101!) and then a community post lead me to the explanation for rule metadata. At this point we had to redo our rule creation because we had modeled it after the JavaRulesDefinition in the sonar-custom-plugin-example (it was recommended to start from this base project) and here the rules aren’t actually based on json metadata but instead are created directly on the repository with a method “createRule” which apparently doesn’t have the option of attaching quickfix related metadata. Setting “quickfixAvailable” to true worked afterwards (though quickfixes themselves of course still didn’t).

So all in all, I think the documentation on quickfixes could definitely be improved. (Unless I’ve just missed existing docs)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.