Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

imports broken by whitespace over at least two lines cannot be parsed #2106

Open
jsolomon8080 opened this issue May 4, 2024 · 1 comment

Comments

@jsolomon8080
Copy link

Gradle 7.6.1
Spotless 6.25.0
Ubuntu 22.04
Java 11 or 17

Our code is:

import static foo.bar.
    FooClass.CONSTANT;

The import class package is very very long so it's broken across multiple lines, but I believe any import will reproduce the issue.

The spotless stacktrace is:

Step 'removeUnusedImports' found problem in 'path/to/offending/file.java':
14:84: error: <identifier> expected
com.google.googlejavaformat.java.FormatterException: 14:84: error: <identifier> expected
        at com.google.googlejavaformat.java.FormatterException.fromJavacDiagnostics(FormatterException.java:51)
        at com.google.googlejavaformat.java.RemoveUnusedImports.parse(RemoveUnusedImports.java:264)
        at com.google.googlejavaformat.java.RemoveUnusedImports.removeUnusedImports(RemoveUnusedImports.java:218)
        at com.diffplug.spotless.glue.java.GoogleJavaFormatRemoveUnusedImporterFormatterFunc.apply(GoogleJavaFormatRemoveUnusedImporterFormatterFunc.java:38)

Using toggleOffOn() with // spotless:off and // spotless:on around the offending import(s) makes no difference because the code cannot be parsed.

Gradle plugin block:

spotless {
    enforceCheck false
    java {
        toggleOffOn()
        target 'src/**/*.java'
        encoding 'UTF-8'
        lineEndings 'UNIX'
        importOrderFile("/path/to/import-order-file")
        removeUnusedImports()
    }   
}   

My workaround at the moment is to add:

targetExclude 'path/to/offending/file.java'

to our gradle plugin config.

At a minimum the toggleOffOn() should work but in theory, since this is legal java, the parser should be able to deal with it and it should just work.

@jsolomon8080
Copy link
Author

The parser was able to handle such a line using spotless plugin 5.12.4.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant