checkstyle / sonar-checkstyle

Support on Checkstyle in SonarQube. Officially transfered from https://github.com/SonarQubeCommunity/sonar-checkstyle
GNU Lesser General Public License v3.0
174 stars 70 forks source link

Analysis fails with InputMismatchException after Sonar and Checkstyle Update #533

Open CarstenNeumannHB opened 1 month ago

CarstenNeumannHB commented 1 month ago

Sonar version: 10.7 (was 10.4) Checkstyle sonar plugin version: 10.17.0 (was 10.12.5)

Hi,

since we upgraded Sonar and Checkstyle an exception occurs while performing the checkstyle analysis:

Console output:

[INFO] ------------- Run sensors on project [INFO] Sensor CheckstyleSensor [checkstyle] [INFO] Checkstyle output report: C:\Runners\gitlab-builds\06\censored\cpq-veterans-aggregator\target\sonar\checkstyle-result.xml [INFO] Checkstyle configuration: C:\Runners\gitlab-builds\06\censored\cpq-veterans-aggregator\target\sonar\checkstyle.xml [INFO] Checkstyle charset: windows-1252 line 30:34 token recognition error at: ''ä' [INFO] Time spent writing ucfgs 0ms

Error message:

[ERROR] Failed to execute goal org.sonarsource.scanner.maven:sonar-maven-plugin:3.9.1.2184:sonar (default-cli) on project cpq-veterans-aggregator: Can not execute Checkstyle: Exception was thrown while processing C:\Runners\gitlab-builds\06\censored\util\sort\comparator\AbstractPipelineComparator.java: IllegalStateException occurred while parsing file C:\Runners\gitlab-builds\06\censored\util\sort\comparator\AbstractPipelineComparator.java. 28:39: mismatched input '(' expecting ';': InputMismatchException -> [Help 1] [ERROR]

With Checkstyle the analysis on this file can be performed:

PS C:\Tools\checkstyle> java -jar '-Duser.language=en -Duser.country=US' .\checkstyle-10.17.0-all.jar -c /sun_checks.xml .\AbstractPipelineComparator.java Starting audit... [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:1: Missing package-info.java file. [JavadocPackage] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:13: Line is longer than 80 characters (found 82). [LineLength] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:14: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:15:5: Missing a Javadoc comment. [MissingJavadocMethod] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:16: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:18: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:23: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:25: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:27:5: Class 'AbstractPipelineComparator' looks like designed for extension (can be subclassed), but the method 'prepairForCompare' does not have javadoc that explains how to do that safely. If class is not designed for extension consider making the class 'AbstractPipelineComparator' final or making the method 'prepairForCompare' static/final/abstract/empty, or adding allowed annotation for the method. [DesignForExtension] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:27:40: Parameter o should be final. [FinalParameters] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:29: Line has trailing spaces. [RegexpSingleline] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:30:38: ',' is not followed by whitespace. [WhitespaceAfter] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:31:38: ',' is not followed by whitespace. [WhitespaceAfter] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:32:38: ',' is not followed by whitespace. [WhitespaceAfter] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:33:38: ',' is not followed by whitespace. [WhitespaceAfter] [ERROR] C:\Tools\checkstyle.\AbstractPipelineComparator.java:35: Line has trailing spaces. [RegexpSingleline] Audit done. Checkstyle ends with 16 errors.

This is the affected file:

/*
 * AbstractPipelineComparator.java
 *
 * Created on 16. Juni 2004, 14:15
 */

package com.censored.util.sort.comparator;

/**
 *
 * @author  censored
 */
public abstract class AbstractPipelineComparator implements java.util.Comparator {

    public abstract int compare(Object o1, Object o2);

    /*
     * Ersetzt in einem String (falls vorhanden) 
     * alle Umlaute (ä,ö,ü) durch (a,o,u),
     * 'ß' durch s und alle Zeichen in Kleinbuchstaben.
     * Dies ermöglicht eine genaue Sortierung eines
     * Strings.
     * 
     * @param Object o
     * @return ersetzter String 
     */
    protected String prepairForCompare(Object o) {
        String prepair = String.valueOf(o);
        prepair = prepair.toLowerCase();        
        prepair = prepair.replace('ä','a');
        prepair = prepair.replace('ö','o');
        prepair = prepair.replace('ü','u');
        prepair = prepair.replace('ß','s');
        return prepair;
    }    
}

As you can see, it contains several German umlauts. The highlighted console output as well mentions one of them. With former versions of SonarQube and Checkstyle-Plugin this file didn't cause any problems and it hasn't been changed since.

Thanks!

Regards, Carsten

muhlba91 commented 1 month ago

the output ([INFO] Checkstyle charset: windows-1252) shows that Checkstyle uses the windows-1252 charset. this charset doesn't contain these special characters, so Checkstyle cannot interpret them correctly.

i don't think this is related to the Checkstyle upgrade. SQ (scanners) usually use the system encoding unless SQ was configured to use a specific encoding (e.g., UTF-8).

muhlba91 commented 3 weeks ago

fyi, i have copied your code using SQ 10.7 (containerized) and analysis runs through smoothly. additionally, you can try running the CS cli using your configuration to see if this is an issue caused by CS itself.

CarstenNeumannHB commented 2 days ago

Hi,

sorry for the late reply. We run our analysis on a Windows system, not containerized, maybe this makes a difference. Do we have to explicitly provide the right charset somehow? The analysis with CS itself runs fine, as you can see in my first post.

Thanks!

rnveach commented 2 days ago

The latest checkstyle uses utf 8 by default unless an override is sent to it (by configuration file or by setting directly).

I don't know sonar, but the windows charset output looks it comes from sonar inside CheckstyleConfiguration where it gets the value from fileSystem.encoding. That is sonar's api (not native file system). Sonar says it's the project's charset, so maybe look at the project settings or global project settings.