removeWildcardImports: throw new AssertionError instead of removing#2557
removeWildcardImports: throw new AssertionError instead of removing#2557Pankraz76 wants to merge 7 commits intodiffplug:mainfrom
Conversation
nedtwigg
left a comment
There was a problem hiding this comment.
Error is much better than silently removing, but we have a better way to do that.
lib/src/main/java/com/diffplug/spotless/generic/ReplaceRegexStep.java
Outdated
Show resolved
Hide resolved
8e3a5f4 to
57f2b91
Compare
plugin-maven/src/test/java/com/diffplug/spotless/maven/MavenIntegrationHarness.java
Show resolved
Hide resolved
57f2b91 to
a93a424
Compare
| FormatterFunc toLinter() { | ||
| return raw -> { | ||
| if (regex.matcher(raw).find()) { | ||
| throw atUndefinedLine("", replacement).shortcut(); |
There was a problem hiding this comment.
i would like to determine the line. But adding with line number 444 it was not mentioned in the error log. So i dont know how to test it.
There was a problem hiding this comment.
- Looks like
ReplaceRegexStepis missing its step-level test. It should have one calledReplaceRegexStepTestwhich should be a peer of this - We use selfie to do snapshot tests of the lint results, here is an example how that looks
- don't write out what's inside of
toBe, just puttoBe_TODO()and then run the test, it will write the string literal for you - KtLint needs the path to work, thus
StepHarnessWithFile, you can use justStepHarnesswhich is easier
There was a problem hiding this comment.
not understand the code, but the output does not show the line number given.
Expecting actual:
"[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spotless Maven Plugin Tests 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) @ spotless-maven-plugin-tests ---
[INFO] Index file does not exist. Fallback to an empty index
[ERROR] Step 'removeWildcardImports' found problem in 'test.java':
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
com.diffplug.spotless.Lint$ShortcutException: 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
at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.440 s
[INFO] Finished at: 2025-07-18T21:55:53+02:00
[INFO] Final Memory: 11M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java: 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 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:73)
at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 22 more
Caused by: com.diffplug.spotless.Lint$ShortcutException: 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
at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
... 25 more
[ERROR]
[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
"
to contain:
"11111"
java.lang.AssertionError:
Expecting actual:
"[INFO] Error stacktraces are turned on.
[INFO] Scanning for projects...
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] Building Spotless Maven Plugin Tests 1.0.0-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO]
[INFO] --- spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) @ spotless-maven-plugin-tests ---
[INFO] Index file does not exist. Fallback to an empty index
[ERROR] Step 'removeWildcardImports' found problem in 'test.java':
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
com.diffplug.spotless.Lint$ShortcutException: 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
at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 0.440 s
[INFO] Finished at: 2025-07-18T21:55:53+02:00
[INFO] Final Memory: 11M/40M
[INFO] ------------------------------------------------------------------------
[ERROR] Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java: 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 -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal com.diffplug.spotless:spotless-maven-plugin:2.46.0-SNAPSHOT:apply (default-cli) on project spotless-maven-plugin-tests: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:216)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:116)
at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:80)
at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build(SingleThreadedBuilder.java:51)
at org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:120)
at org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:355)
at org.apache.maven.DefaultMaven.execute(DefaultMaven.java:155)
at org.apache.maven.cli.MavenCli.execute(MavenCli.java:584)
at org.apache.maven.cli.MavenCli.doMain(MavenCli.java:216)
at org.apache.maven.cli.MavenCli.main(MavenCli.java:160)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:289)
at org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:229)
at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:415)
at org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:356)
at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
at java.base/java.lang.reflect.Method.invoke(Method.java:565)
at org.apache.maven.wrapper.BootstrapMainStarter.start(BootstrapMainStarter.java:39)
at org.apache.maven.wrapper.WrapperExecutor.execute(WrapperExecutor.java:122)
at org.apache.maven.wrapper.MavenWrapperMain.main(MavenWrapperMain.java:55)
Caused by: org.apache.maven.plugin.MojoExecutionException: Unable to format file /private/var/folders/3m/r9yztvfj0tl0sw8s6zl75_x00000gn/T/junit-7476299284891749091/src/main/java/test.java
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:73)
at com.diffplug.spotless.maven.AbstractSpotlessMojo.execute(AbstractSpotlessMojo.java:255)
at org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:132)
at org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:208)
... 22 more
Caused by: com.diffplug.spotless.Lint$ShortcutException: 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
at com.diffplug.spotless.Lint.shortcut(Lint.java:111)
at com.diffplug.spotless.generic.ReplaceRegexStep$State.lambda$toLinter$1(ReplaceRegexStep.java:68)
at com.diffplug.spotless.FormatterFunc.apply(FormatterFunc.java:33)
at com.diffplug.spotless.FormatterStepEqualityOnStateSerialization.format(FormatterStepEqualityOnStateSerialization.java:49)
at com.diffplug.spotless.Formatter.computeWithLint(Formatter.java:170)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:97)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:82)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:77)
at com.diffplug.spotless.DirtyState.of(DirtyState.java:73)
at com.diffplug.spotless.maven.SpotlessApplyMojo.process(SpotlessApplyMojo.java:63)
... 25 more
[ERROR]
[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
"
to contain:
"11111"
at com.diffplug.spotless.maven.java.RemoveWildcardImportsStepTest.testRemoveWildcardImports(RemoveWildcardImportsStepTest.java:48)
at java.base/java.lang.reflect.Method.invoke(Method.java:580)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1596)
lib/src/main/java/com/diffplug/spotless/java/RemoveWildcardImportsStep.java
Outdated
Show resolved
Hide resolved
…ortsStep.java Co-authored-by: Owais Kazi <owaiskazi19@gmail.com>
plugin-maven/src/test/java/com/diffplug/spotless/maven/java/RemoveWildcardImportsStepTest.java
Outdated
Show resolved
Hide resolved
|
@iddeepak kindly asking if you might complete the linting approach? idk how to continue here. Actually it should be quite simple, but i dont get it working. Thanks. |
|
|
|
||
| String path = "src/main/java/test.java"; | ||
| setFile(path).toResource("java/removewildcardimports/JavaCodeWildcardsUnformatted.test"); | ||
| FormatterStep step = KtLintStep.create("0.49.0", TestProvisioner.mavenCentral()); |
There was a problem hiding this comment.
meant to validate only the removeWildcardImports behavior, Is it just for testing...... pulling in a KtLint step is not required here.
|
@Pankraz76 |
|
thanks @iddeepak, as always took care. |

Please DO NOT FORCE PUSH. Don't worry about messy history, it's easier to do code review if we can tell what happened after the review, and force pushing breaks that.
Please make sure that your PR allows edits from maintainers. Sometimes it's faster for us to just fix something than it is to describe how to fix it.
After creating the PR, please add a commit that adds a bullet-point under the
[Unreleased]section of CHANGES.md, plugin-gradle/CHANGES.md, and plugin-maven/CHANGES.md which includes:If your change only affects a build plugin, and not the lib, then you only need to update the
plugin-foo/CHANGES.mdfor that plugin.If your change affects lib in an end-user-visible way (fixing a bug, updating a version) then you need to update
CHANGES.mdfor both the lib and all build plugins. Users of a build plugin shouldn't have to refer to lib to see changes that affect them.This makes it easier for the maintainers to quickly release your changes :)
spotlessto8.0.0opensearch-project/OpenSearch#18770kindly request your feedback.
@owaiskazi19
@iddeepak
@sbrannen