Skip to content

java.removeUnusedImports does not handle import module correctly #2890

@sdavids

Description

@sdavids

pom.xml

  <properties>
    <maven.compiler.release>25</maven.compiler.release>
  </properties>

  <build>
    <plugins>
      <plugin>
        <groupId>com.diffplug.spotless</groupId>
        <artifactId>spotless-maven-plugin</artifactId>
        <version>3.3.0</version>
        <configuration>
          <java>
            <removeUnusedImports/>
          </java>
        </configuration>
      </plugin>
    </plugins>
  </build>

ModuleImportTest.java

import module java.base;

class ModuleImportTest {

  void test() {
    UUID.randomUUID();
  }
}

$ ./mvnw -v                     
Apache Maven 3.9.9 (8e8579a9e76f7d015ee5ec7bfcdc97d260186937)
Maven home: /Users/redacted/.m2/wrapper/dists/apache-maven-3.9.9/3477a4f1
Java version: 25.0.2, vendor: Eclipse Adoptium, runtime: /Users/redacted/.local/share/sdkman/candidates/java/25.0.2-tem
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "12.7.6", arch: "x86_64", family: "mac"

$ ./mvnw spotless:apply
[INFO] Scanning for projects...
[INFO] 
[INFO] ----------------< spotless:module-imports >-----------------
[INFO] Building module-imports 1.0-SNAPSHOT
[INFO]   from pom.xml
[INFO] --------------------------------[ jar ]---------------------------------
[INFO] 
[INFO] --- spotless:3.3.0:apply (default-cli) @ module-imports ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  1.126 s
[INFO] Finished at: 2026-03-17T10:30:13+01:00
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:3.3.0:apply (default-cli) on project module-imports: There were 1 lint error(s), they must be fixed or suppressed.
[ERROR]   src/main/java/ModuleImportTest.java:LINE_UNDEFINED removeUnusedImports(java.lang.IllegalArgumentException) object of type com.sun.tools.javac.tree.JCTree$JCModuleImport is not an instance of com.sun.tools.javac.tree.JCTree$JCImport (...)                                                                                                                                                                                                                                                 
[ERROR]   Resolve these lints or suppress with `<lintSuppressions>`
[ERROR] -> [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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions