Search code examples
gradleintellij-ideajvm-argumentserrorprone

Set -Xmaxerros and -Xmaxwarns in Gradle to display all ErrorProne messages


I have created a script that runs ./gradlew build -x and then counts all occurences of error: [SomeError] and warning: [SomeWarning] from the output.

#!/bin/bash

# Run './gradlew build -x test' and capture the output
build_output=$(./gradlew build -x test 2>&1)

# Extract error URLs from the build output and count their occurrences
error_urls=$(echo "$build_output" | grep -o -P '(error|warning):\s*\[([^]]+)\]' | sort | uniq -c)

# Create a mapping file with error URLs and occurrence counts
echo "$error_urls" | while read -r count url; do
    printf "| %-50s | %-5s |\n" "$url" "$count" >> error_mapping.txt
done

I'm getting this output for ./gradlew build -x:

Note: Some input files use unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
100 errors
100 warnings
only showing the first 100 errors, of 262 total; use -Xmaxerrs if you would like to see more
only showing the first 100 warnings, of 227 total; use -Xmaxwarns if you would like to see more

I want to display all messages to have a total of 489 entries in my map.

This is my config in build.gradle.kts:

import net.ltgt.gradle.errorprone.errorprone

plugins {
  id("net.ltgt.errorprone") version "3.1.0" apply false
}


subprojects {
  plugins.withType<JavaPlugin> {
    apply(plugin = "net.ltgt.errorprone")
    dependencies {     
      add("errorprone", "com.google.errorprone:error_prone_core:2.16")

    }
    tasks.withType<JavaCompile>().configureEach {
      options.errorprone {
        disableWarningsInGeneratedCode.set(true)
      }
    }
  }
}

I tried multiple things:

  • Inputting ./gradlew -x test -Xmaxerrors 1000
  • Changing the compiler in IntelliJ to Javac with error-prone and to Eclipse
  • Adding the config in build.gradle.kts: options.compilerArgs.add("-Xmaxerrs 1000")
  • Setting gradle args in gradle.properties

What else can I do?


Solution

  • On the right track

    • Inputting ./gradlew -x test -Xmaxerrors 1000
      • -Xmaxerrs and -Xmaxwarns options are specific to the Java compiler (javac) and cannot be directly passed through to Gradle's ./gradlew command.
    • Adding the config in build.gradle.kts: options.compilerArgs.add("-Xmaxerrs 1000")
      • This is where you were very close, for options.compilerArgs.add each compiler argument should be passed as a separate element in the compilerArgs list. With ("-Xmaxerrs 1000") it is being passed as one argument that javac does not understand, you need to split the arguments as ("-Xmaxerrs", "1000"), so that javac evaluates the arguments as option, value.
      • As you will note below, the options.compilerArgs.add() has been updated to options.compilerArgs.addAll(listOf()). The add method is used to add a single element to the compilerArgs list, the addAll method is used to add multiple elements to the compilerArgs list. The listOf function creates a new list with the specified elements ("-Xmaxerrs", "1000", "-Xmaxwarns", "1000"), and then those elements are added to the compilerArgs list.

    Update to the following:

    subprojects {
        plugins.withType<JavaPlugin> {
            apply(plugin = "net.ltgt.errorprone")
            dependencies {
                add("errorprone", "com.google.errorprone:error_prone_core:2.16")
            }
            tasks.withType<JavaCompile>().configureEach {
                options.errorprone {
                    disableWarningsInGeneratedCode.set(true)
                    compilerArgs.addAll(listOf("-Xmaxerrs", "1000", "-Xmaxwarns", "1000"))
                }
            }
        }
    }
    

    N.B.
    The Java compiler (javac) does have default limits for the maximum number of errors / warnings displayed. The default limits are set to prevent an overwhelming output in the case of large number of errors / warnings.

    Default and maximum values for -Xmaxerrs and -Xmaxwarns may vary depending on the variant/version of the JDK. Common default value is 100 for both errors and warnings, but best to check official documentation.

    In your case, the exception message "only showing the first 100 errors, of 262 total; use -Xmaxerrs if you would like to see more" it indicated that the Java compiler had reached the default limit of 100 errors and suggesting the use of the -Xmaxerrs option to increase the limit if you want to see more. Duplicated for the warnings.

    Also a side note, the value can be set to "0", which will disable the limit and setting it to infinity, but this is discouraged.