Skip to content

lombok causes an IndexOutOfBoundsException in UnusedVariable #1250

Open

Description

@mrt181

Description of the problem / feature request:

./gradlew clean compileJava throws this error

Service.java:1: error: An unhandled exception was thrown by the Error Prone static analysis plugin.                                                    
package io.mine.service.grpc;
^
     Please report this at https://github.com/google/error-prone/issues/new and include the following:
	 
     error-prone version: 2.3.3
     BugPattern: UnusedVariable
     Stack Trace:
     java.lang.IndexOutOfBoundsException
        at java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:580)
        at java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:42)
        at com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments(SuggestedFixes.java:1021)
        at com.google.errorprone.bugpatterns.UnusedVariable.buildUnusedVarFixes(UnusedVariable.java:387)
        at com.google.errorprone.bugpatterns.UnusedVariable.matchCompilationUnit(UnusedVariable.java:239)
        at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:433)
        at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:541)
        at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:150)

Feature requests: what underlying problem are you trying to solve with this feature?

none, its applied by error-prone 2.3.3

Bugs: what's the simplest, easiest way to reproduce this bug? Please provide a minimal example if possible.

What version of Error Prone are you using?

errorproneJavacVersion = "9+181-r4173-1"
errorproneVersion = "2.3.2"
errorpronePluginVersion = "0.7.1"

openjdk version "1.8.0_192"

Have you found anything relevant by searching the web?

reverting back to 2.3.2 fixes the problem.
I tried to exclude the BugPattern but that does not work:

gradle.projectsEvaluated {
    tasks.withType(JavaCompile) {
        options.encoding = "UTF-8"
        options.annotationProcessorPath = configurations.errorprone
        options.errorprone.enabled = true
        options.errorprone.disableWarningsInGeneratedCode = true
        options.errorprone.excludedPaths = ".*/build/gen.*/.*"
        options.errorprone.errorproneArgs = ["-Xep:ParameterName:OFF",
                                            "Xep:UnusedVariable:OFF"]
    }
}

Activity

  1. bbzg commented on Apr 7, 2019

    @bbzg

    Title says 2.3.2 but should say 2.3.3?

    I am also seeing this issue on 2.3.3, fixed by reverting to 2.3.2.

  2. changed the title [-]2.3.2 fails with new UnusedVariable message[/-] [+]2.3.3 fails with new UnusedVariable message[/+] on Apr 7, 2019
  3. martinschaef commented on May 3, 2019

    @martinschaef

    I can unblock myself by adding -Xep:ParameterName:OFF as suggested in:
    #780 (comment)

  4. mrt181 commented on May 21, 2019

    @mrt181
    Author

    -Xep:ParameterName:OFF does not work for me

    This works though

    gradle.projectsEvaluated {
        tasks.withType(JavaCompile).configureEach {
            options.encoding = "UTF-8"
            options.annotationProcessorPath = configurations.errorprone
            options.errorprone.disableWarningsInGeneratedCode = true
            options.errorprone.excludedPaths = ".*/build/gen.*/.*"
            options.errorprone {
                disable("ParameterName")
                disable("UnusedVariable")
            }
        }
    }
  5. cushon commented on Jan 19, 2021

    @cushon
    Collaborator

    re: #1250 (comment), if -Xep:ParameterName:OFF works around the crash it's a different bug: #780

    Re-opening to track crashes related to lombok and UnusedVariable.

  6. reopened this on Jan 19, 2021
  7. changed the title [-]2.3.3 fails with new UnusedVariable message[/-] [+]lombok causes an IndexOutOfBoundsException in UnusedVariable[/+] on Jan 19, 2021
  8. cushon commented on Jan 19, 2021

    @cushon
    Collaborator
    import lombok.extern.flogger.Flogger;
    
    @Flogger
    public class I1250 {}
    javac   -XDcompilePolicy=simple   -processorpath lombok-1.18.16.jar:error_prone_core-2.5.1-with-dependencies.jar:dataflow-shaded-3.7.1.jar:jFormatString-3.0.0.jar   '-Xplugin:ErrorProne -Xep:UnusedVariable:ERROR -Xep:ReferenceEquality:OFF' -cp lombok-1.18.16.jar:flogger-0.5.1.jar I1250.java
    
    import lombok.extern.flogger.Flogger;
    ^
    
         error-prone version: 2.5.1
         BugPattern: UnusedVariable
         Stack Trace:
         java.lang.IndexOutOfBoundsException: Range [54, -1) out of bounds for length 70
      	at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
      	at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:76)
      	at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:295)
      	at java.base/java.util.Objects.checkFromToIndex(Objects.java:385)
      	at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:677)
      	at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:44)
      	at com.google.errorprone.VisitorState.getOffsetTokens(VisitorState.java:585)
      	at com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments(SuggestedFixes.java:1547)
      	at com.google.errorprone.bugpatterns.UnusedVariable.buildUnusedVarFixes(UnusedVariable.java:381)
    
  9. rspilker commented on Jan 19, 2021

    @rspilker

    I am a Lombok maintainer. If there's anything we can do to help, please let us know. (apart from not generating a field that is not used...)

  10. cushon commented on Jan 19, 2021

    @cushon
    Collaborator

    @rspilker I think most of the crashes are happening because the source positions for the AST nodes lombok adds are incomplete (e.g. missing end positions), or they get out of sync with the source (e.g. from compilationUnit.getSourceFile().getCharContent(...)). I think projectlombok/lombok#2691 would help, although I'm not sure it's enough by itself to prevent crashes, unless you're also able to update the contents of the FileObject and adjust the positions of other AST nodes in the file?

    The other thing I can think of is that if @lombok.Generated was generated by default. We could rely on it to not process some of the generated code, which would also avoid crashes and false positives.

    For @lombok.Generated, I think I'm seeing cases where lombok is editing AST nodes (which then don't have end positions) outside of regions that have a @lombok.Generated annotation. Is that expected?

  11. Rawi01 commented on Jan 24, 2021

    @Rawi01

    @cushon Thanks for adding some code to ignore generated stuff, that should fix most problems. Meanwhile I started to add start and end positions for everything lombok generates. If that is finished and merged all remaining problmes should be solved.

  12. david-sitsky commented on Oct 12, 2021

    @david-sitsky

    I was hit by this reported issue in my project which uses the lombok.extern.slf4j.Slf4j annotation. This annotation auto-generates the "log" variable for logging purposes. As it turns out, I had changed my class and failed to realise I no longer had any more usages of the log variable, and this is why this exception from errorprone occurred.

    Is there any way errorprone can output a more friendly error message in this situation? It took me quite some time to figure out this was the cause.

    I am using errorprone 2.4.0 and lombok 1.18.12. Apologies if this is a separate issue to what is reported here, but the exception output is the same.

  13. Rawi01 commented on Oct 12, 2021

    @Rawi01

    @david-sitsky Can you try to use a newer lombok version? We added start and end positions in lombok 1.18.18 for generated lombok nodes which should fix this issue.

  14. 2 remaining items

  15. reopened this on Apr 19, 2022
  16. cushon commented on Apr 19, 2022

    @cushon
    Collaborator

    One thing to try here is using SuppressibleTreePathScanner in the implementation of UnusedVariable, which should help it skip the lombok generated code.

  17. msridhar commented on Apr 19, 2022

    @msridhar
    Contributor

    FWIW with Lombok 1.18.22 and disableWarningsInGeneratedCode enabled, we no longer see crashes in this checker for NullAway tests. We should see if someone can actually repro with the latest Lombok version (I think 1.18.24 just came out).

  18. astellingwerf commented on Apr 19, 2022

    @astellingwerf

    How do I add that setting in Maven?

    I'd do this:

                <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-compiler-plugin</artifactId>
                    <version>3.8.0</version>
                    <configuration>
                        <source>11</source>
                        <target>11</target>
                        <encoding>UTF-8</encoding>
                        <compilerArgs>
                            <arg>-XDcompilePolicy=simple</arg>
                            <arg>-Xplugin:ErrorProne</arg>
                            <arg>-XepDisableWarningsInGeneratedCode</arg>
                        </compilerArgs>
                        <annotationProcessorPaths>
                            <path>
                                <groupId>com.google.errorprone</groupId>
                                <artifactId>error_prone_core</artifactId>
                                <version>2.13.1</version>
                            </path>
                            <path>
                                <groupId>jp.skypencil.errorprone.slf4j</groupId>
                                <artifactId>errorprone-slf4j</artifactId>
                                <version>0.1.11</version>
                            </path>
                            <path>
                                <groupId>org.projectlombok</groupId>
                                <artifactId>lombok</artifactId>
                                <version>${lombok.version}</version>
                            </path>
                        </annotationProcessorPaths>
                    </configuration>
                </plugin>
    

    but that fails:

    [INFO] ------------------------------------------------------------------------
    [INFO] BUILD FAILURE
    [INFO] ------------------------------------------------------------------------
    [INFO] Total time:  30.008 s
    [INFO] Finished at: 2022-04-19T09:06:24Z
    [INFO] ------------------------------------------------------------------------
    [ERROR] Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.0:compile (default-compile) on project dx-ot2-health-monitor: Fatal error compiling: error: invalid flag: -XepDisableWarningsInGeneratedCode -> [Help 1]
    [ERROR] 
    [ERROR] To see the full stack trace of the errors, re-run Maven with the -e switch.
    [ERROR] Re-run Maven using the -X switch to enable full debug logging.
    [ERROR] 
    [ERROR] For more information about the errors and possible solutions, please read the following articles:
    [ERROR] [Help 1] http://cwiki.apache.org/confluence/display/MAVEN/MojoExecutionException
    
  19. cushon commented on Apr 19, 2022

    @cushon
    Collaborator

    How do I add that setting in Maven?

    See https://errorprone.info/docs/flags#maven

    You need to pass -Xplugin:ErrorProne -XepDisableWarningsInGeneratedCode as a single <arg>

  20. msridhar commented on Apr 19, 2022

    @msridhar
    Contributor

    I should say that we also set lombok.addLombokGeneratedAnnotation = true in lombok.config (docs here). Without that, I'm not sure Lombok will add @lombok.Generated annotations to the code it generates.

  21. ParSal123 commented on Jun 6, 2023

    @ParSal123

    How do I add that setting in Maven?

    See https://errorprone.info/docs/flags#maven

    You need to pass -Xplugin:ErrorProne -XepDisableWarningsInGeneratedCode as a single <arg>

    This didn't work for me on 2.19.1 with lombok 1.18.22.

    An unhandled exception was thrown by the Error Prone static analysis plugin.
    [ERROR]      Please report this at https://github.com/google/error-prone/issues/new and include the following:
    [ERROR]
    [ERROR]      error-prone version: 2.19.1
    [ERROR]      BugPattern: UnusedVariable
    [ERROR]      Stack Trace:
    [ERROR]      java.lang.IndexOutOfBoundsException: Range [573, 541) out of bounds for length 2941
    [ERROR]         at java.base/jdk.internal.util.Preconditions.outOfBounds(Preconditions.java:64)
    [ERROR]         at java.base/jdk.internal.util.Preconditions.outOfBoundsCheckFromToIndex(Preconditions.java:76)
    [ERROR]         at java.base/jdk.internal.util.Preconditions.checkFromToIndex(Preconditions.java:313)
    [ERROR]         at java.base/java.util.Objects.checkFromToIndex(Objects.java:385)
    [ERROR]         at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:664)
    [ERROR]         at java.base/java.nio.HeapCharBuffer.subSequence(HeapCharBuffer.java:44)
    [ERROR]         at com.google.errorprone.VisitorState.getOffsetTokens(VisitorState.java:607)
    [ERROR]         at com.google.errorprone.fixes.SuggestedFixes.replaceIncludingComments(SuggestedFixes.java:1667)
    [ERROR]         at com.google.errorprone.bugpatterns.UnusedVariable.buildUnusedVarFixes(UnusedVariable.java:409)
    [ERROR]         at com.google.errorprone.bugpatterns.UnusedVariable.matchCompilationUnit(UnusedVariable.java:249)
    [ERROR]         at com.google.errorprone.scanner.ErrorProneScanner.processMatchers(ErrorProneScanner.java:449)
    [ERROR]         at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:555)
    [ERROR]         at com.google.errorprone.scanner.ErrorProneScanner.visitCompilationUnit(ErrorProneScanner.java:150)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.tree.JCTree$JCCompilationUnit.accept(JCTree.java:614)
    [ERROR]         at jdk.compiler/com.sun.source.util.TreePathScanner.scan(TreePathScanner.java:60)
    [ERROR]         at com.google.errorprone.scanner.Scanner.scan(Scanner.java:58)
    [ERROR]         at com.google.errorprone.scanner.ErrorProneScannerTransformer.apply(ErrorProneScannerTransformer.java:43)
    [ERROR]         at com.google.errorprone.ErrorProneAnalyzer.finished(ErrorProneAnalyzer.java:156)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.api.MultiTaskListener.finished(MultiTaskListener.java:132)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1394)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.flow(JavaCompiler.java:1341)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.main.JavaCompiler.compile(JavaCompiler.java:933)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.lambda$doCall$0(JavacTaskImpl.java:104)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.invocationHelper(JavacTaskImpl.java:152)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.doCall(JavacTaskImpl.java:100)
    [ERROR]         at jdk.compiler/com.sun.tools.javac.api.JavacTaskImpl.call(JavacTaskImpl.java:94)
    [ERROR]         at org.codehaus.plexus.compiler.javac.JavaxToolsCompiler.compileInProcess(JavaxToolsCompiler.java:126)
    [ERROR]         at org.codehaus.plexus.compiler.javac.JavacCompiler.performCompile(JavacCompiler.java:174)
    [ERROR]         at org.apache.maven.plugin.compiler.AbstractCompilerMojo.execute(AbstractCompilerMojo.java:1134)
    [ERROR]         at org.apache.maven.plugin.compiler.CompilerMojo.execute(CompilerMojo.java:187)
    [ERROR]         at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
    [ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute2(MojoExecutor.java:370)
    [ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.doExecute(MojoExecutor.java:351)
    [ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:215)
    [ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:171)
    [ERROR]         at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:163)
    [ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:117)
    [ERROR]         at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:81)
    [ERROR]         at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:56)
    [ERROR]         at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:128)
    [ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:298)
    [ERROR]         at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:192)
    [ERROR]         at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:105)
    [ERROR]         at org.apache.maven.cli.MavenCli.execute(MavenCli.java:960)
    [ERROR]         at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:293)
    [ERROR]         at org.apache.maven.cli.MavenCli.main(MavenCli.java:196)
    [ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    [ERROR]         at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
    [ERROR]         at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    [ERROR]         at java.base/java.lang.reflect.Method.invoke(Method.java:568)
    [ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:282)
    [ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:225)
    [ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:406)
    [ERROR]         at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:347)
    [ERROR]
    
  22. ParSal123 commented on Jun 8, 2023

    @ParSal123

    Actually, my bad. Forgot the lombok.addLombokGeneratedAnnotation = true.
    But now I get another error. #3954
    I'll add a comment there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions