Spotless confuses real code with simulated code in unit tests

Open
#2,997 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
52/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Quiet
Tech stack
java
Domain
build-system

Research direction

Start with OrderImportsCustomTest.java at line 83 and reproduce the report using the Spotless Maven plugin versions 3.6.0, 3.7.0, and 3.8.0. Trace why the Java text block is treated as source code during the check command, then verify that the simulated import is ignored while real wildcard imports are still reported.

Written by the indexing model from the issue text.

Description

We have a project focused on the application of best practices and code correction using OpenRewrite. We have created a class to implement a custom Recipe that controls and corrects the use of the wildcard "*" in import groupings according to our internally defined standards.
We have a series of unit tests that use the testing libraries provided by OpenRewrite, in which we create code scenarios using string blocks:

 String before =
        """
        import java.util.List;
        import java.util.ArrayList;
        import java.util.regex.Pattern;
        import java.util.Objects;
        import java.util.Set;
        import java.util.Map;
        """;

    String after =
        """
        import java.util.*;
        import java.util.regex.Pattern;
        """;

    rewriteRun(java(before, after));

The problem is that when we run the check command for the Maven plugin com.diffplug.spotless:spotless-maven-plugin:3.8.0, Spotless detects these strings as if they were actual code, despite the use of triple quotes -> """ string code """.

OrderImportsCustomTest.java:L83 toggle(import java.util.*;) Do not use wildcard imports (e.g. java.util.*) - replace with specific class imports (e.g. java.util.List) as 'spotlessApply' cannot auto-fix this

We have verified that this behavior only occurs in versions 3.7.0 and above. Version 3.6.0 correctly ignores this simulated code.
We have had to apply a workaround using // spotless:off, but we would like to know if there is a permanent solution to this behavior, which we believe is a false positive.

Poject Stack:

  • Java 25
  • Openrewrite 8.72.0
  • Spottles Maven Plugin 3.8.0
Dominant language
Java
Stars
5.7k
Forks
560
Avg merge
1d 13h
Merged PRs (30d)
43

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from diffplug/spotless

All issues in diffplug/spotless

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.