vscode-java icon indicating copy to clipboard operation
vscode-java copied to clipboard

java.completion.importOrder (even the default) takes precedence over checkstyle specs

Open hardyoyo opened this issue 2 years ago • 3 comments

The java.completion.importOrder property, documented here https://github.com/redhat-developer/vscode-java/blob/562afa80c0f705613c6ed3148b4d9eac82093b26/package.json#L519-L527 does not play well with checkstyle. If checkstyle specifies a different sort order for imports, the default importOrder setting will always be used instead. An unsatisfactory workaround is to attempt to specify a matching importOrder via this property. However, it's simply not as expressive as what is possible via Checkstyle, so finding an exact match is impossible.

This situation creates the highly likely event of imports being automatically sorted via the editor, and then the resulting code is in violation of checkstyle constraints, so a CI or regular build process will fail.

Here's an example of just such a sad situation: https://github.com/DSpace/DSpace/pull/8088/commits/23e93ff2857b7099ba8005d0b5b6c3b65f052f1c

Environment
  • Operating System: OSX
  • JDK version: openjdk version "11.0.2" 2019-01-15
  • Visual Studio Code version: 1.63.2
  • Java extension version: v1.3.0
Steps To Reproduce
  1. work on a Java project with a checkstyle configuration (one example: DSpace/DSspace)
  2. install a checkstyle plugin
  3. configure the checkstyle plugin to use the project configuration
  4. open a Java file
  5. click the yellow light bulb that appears over the Java file's imports, select 'organize imports'
  6. save the file
  7. validate the project against the custom checkstyle, it will fail, if the checkstyle specification for import order differs from the default value of the java.completion.importOrder property
Current Result

imports are always sorted to match the order specified by java.completion.importOrder

Expected Result

imports are sorted to match the order specified by checkstyle

Additional Informations

hardyoyo avatar Jan 25 '22 21:01 hardyoyo

I believe this is because the default value of importOrder is actually ["java", "javax", "org", "com"] to go along with Eclipse's default order: https://github.com/redhat-developer/vscode-java/blob/9c91aa22053202630ee46a10f24173362c4f91f7/package.json#L521-L531

You can easily ensure Checkstyle also follows this order by adding the groups property to the ImportOrder rule:

<module name="ImportOrder">
  <property name="groups" value="/^java\./,javax,org,com" />
</module>

I hope this helps 🙂

JoseLion avatar Jul 12 '22 06:07 JoseLion

I believe you have restated part of the problem (yes, the default order is as described), and then suggested that the project change its checkstyle configuration to match that default. If I'm correct that is your suggestion, it's a no-go, the DSpace project has been using that checkstyle configuration for years, and they're going to be very reluctant to change it just so VSCode doesn't erroneously ignore it. (editing to add that I did tentatively broach the subject with a few of the DSpace committers, and while no official ruling was made, it was made clear to me that they'd be reluctant to make such a change... it would touch every java file in the project, a huge change, for a workaround).

hardyoyo avatar Jul 12 '22 14:07 hardyoyo

Oh, I see! So if I understand correctly, you're asking for VSCode to take the import order values from Checkstyle's configuration instead.

It would be nice if VSCode could identify the checkstyle.xml file of the opened project, parse the XML and find the ImportOrder.groups value so it can use those values instead.

However, I also think this could be harder than it sounds. What if there're multiple projects with different checkstyle.xml files? What if the ImportOrder.groups value is not defined? How would VSCode get the default value Checkstyle use for that property? Also, I'm unsure if VSCode allows regular expressions in the java.completion.importOrder values. Checkstyle does, which makes sense because it only needs to check them, not automatically sort them.

Overall, it seems more convenient to have a custom java.completion.importOrder setting in the project's .vscode/settings.json file. That way, VSCode will know the import order is different on that project and sort them accordingly 🙂

JoseLion avatar Jul 15 '22 17:07 JoseLion