diffplug / diffplug/spotless

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

Open
#2,106 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
Java
Stars
5.6k
Forks
559
Avg merge
1d 13h
Merged PRs (30d)
34

Description

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: expected
com.google.googlejavaformat.java.FormatterException: 14:84: error: 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.

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.