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

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

Open
blacelle opened this issue Feb 15, 2024 · 2 comments
Open

Comments

@blacelle
Copy link
Contributor

This can be seen as a reboot of #558 and #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?

@blacelle
Copy link
Contributor Author

This relates to a recent bug-report in PMD: pmd/pmd#4816

@blacelle
Copy link
Contributor Author

This issue can be workarounded by pushing the whole import section between // spotless:off|on:

package io.xxx.sp.scope;

//spotless:off
import java.sql.Connection;
import java.util.TreeSet; // NOPMD false positive, see https://github.com/pmd/pmd/issues/4816
import java.util.function.Supplier;

import com.google.common.annotations.VisibleForTesting;

import eu.solven.pepper.pgsql.AOverDatasource;
//spotless:on

@SuppressWarnings("PMD.UnnecessaryImport")
public class SpScopesInPgsql extends AOverDatasource<MitrustScopes> implements ISpScopes {
    [...]

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