spotless:off and spotless:on fails to prevent changes to imports (v2)

Open
#2,042 3 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
3/5
Estimated time
1-2 days
Newbie friendliness
35/100
Issue type
Bug
Clarity
Mostly clear
Activity status
Stale
Tech stack
java

Research direction

Reproduce the Maven configuration with java ImportSorter and toggleOffOn using the spotless:off/on example. Start by reading ImportOrderStep and the toggleOut handling in PipeStepPair, then check the existing tests for these steps. Done means the trailing import comment remains intact and the toggle markers no longer trigger the reported exception.

Written by the indexing model from the issue text.

Description

This can be seen as a reboot of https://github.com/diffplug/spotless/issues/558 and https://github.com/diffplug/spotless/issues/996.

I want Java ImportSorter not to remove the trailing comment from:

import java.util.TreeSet; // NOPMD false positive, see https://github.com/pmd/pmd/issues/4816
```.

To do so, I wrote:
com.diffplug.spotless spotless-maven-plugin 2.43.0 [...] ``` and
// spotless:off
import java.util.TreeSet; // NOPMD false positive, see https://github.com/pmd/pmd/issues/4816
// spotless:on

It led to the exception:

[INFO] --- spotless-maven-plugin:2.43.0:apply (default-cli) @ sp-core ---
[INFO] Index file corresponds to a different configuration of the plugin. Either the plugin version or its configuration has changed. Fallback to an empty index
[ERROR] Step 'toggleOut' found problem in 'src/main/java/.../XXX.java':
An intermediate step removed a match of spotless:off spotless:on
java.lang.Error: An intermediate step removed a match of spotless:off spotless:on
    at com.diffplug.spotless.generic.PipeStepPair.stateOutCompute (PipeStepPair.java:207)
    at com.diffplug.spotless.generic.PipeStepPair$StateOut.format (PipeStepPair.java:176)
    at com.diffplug.spotless.generic.PipeStepPair.lambda$new$2 (PipeStepPair.java:97)
    at com.diffplug.spotless.FormatterFunc.apply (FormatterFunc.java:32)
    at com.diffplug.spotless.FormatterStepImpl$Standard.format (FormatterStepImpl.java:82)
    at com.diffplug.spotless.FormatterStep$Strict.format (FormatterStep.java:103)
    at com.diffplug.spotless.Formatter.compute (Formatter.java:246)
    at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:203)
    at com.diffplug.spotless.PaddedCell.calculateDirtyState (PaddedCell.java:190)
    at com.diffplug.spotless.maven.SpotlessApplyMojo.process (SpotlessApplyMojo.java:63)
    at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute (AbstractSpotlessMojo.java:237)
    at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo (DefaultBuildPluginManager.java:137)
    [...]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  5.807 s
[INFO] Finished at: 2024-02-15T14:21:17+04:00
[INFO] ------------------------------------------------------------------------
---------------------------------------------------
[...]
---------------------------------------------------
Exception in thread "main" java.lang.Error: An intermediate step removed a match of spotless:off spotless:on
	at com.diffplug.spotless.generic.PipeStepPair.stateOutCompute(PipeStepPair.java:207)
	at com.diffplug.spotless.generic.PipeStepPair$StateOut.format(PipeStepPair.java:176)
	at com.diffplug.spotless.generic.PipeStepPair.lambda$new$2(PipeStepPair.java:97)
	at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:32)
	at com.diffplug.spotless.FormatterStepImpl$Standard.format(FormatterStepImpl.java:82)
	at com.diffplug.spotless.FormatterStep$Strict.format(FormatterStep.java:103)
	at com.diffplug.spotless.Formatter.compute(Formatter.java:246)
	at com.diffplug.spotless.PaddedCell.calculateDirtyState(PaddedCell.java:203)
	at com.diffplug.spotless.PaddedCell.calculateDirtyState(PaddedCell.java:190)
	at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
	at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:237)
	at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:137)
	at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:210)

Is this a missing feature of ImportOrderStep? Or a bug around toggleOffOn?

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.