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

RemoveUnusedImports is not working #1893

Closed
cnarsimharaju opened this issue Oct 29, 2020 · 5 comments
Closed

RemoveUnusedImports is not working #1893

cnarsimharaju opened this issue Oct 29, 2020 · 5 comments

Comments

@cnarsimharaju
Copy link

cnarsimharaju commented Oct 29, 2020

ATTENTION! Please read and follow:

Description of the problem / feature request:

We are planning to use errorprone with our service. We have a challenge with respect to removal of unused imports.

Feature requests: what underlying problem are you trying to solve with this feature?

We want to use RemoveUnusedImports feature from ErrorProne. These are our pom.xml arguments. Even after the build the unused import are not removed.

<arg>-Xplugin:ErrorProne -XepDisableAllChecks -Xep:RemoveUnusedImports -Xep:Piranha:WARN -XepPatchChecks:Piranha -XepPatchLocation:IN_PLACE -XepOpt:Piranha:FlagName=${SAMPLE_STALE_FLAG} -XepOpt:Piranha:IsTreated=true -XepOpt:Piranha:Config=${project.basedir}/src/main/resources/config/pirahana.json</arg> `

We tried on a vanilla spring boot application as well we are not seeing the removal of the unused import. We tried with the option of -Xep:RemoveUnusedImports:ERROR it is showing compilation error for unused import. These are the pluginInfo which we tried

<plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId<version>3.8.0</version><configuration><source>8</source><target>8</target><compilerArgs><arg>-XDcompilePolicy=simple</arg><arg>-Xplugin:ErrorProne -Xep:RemoveUnusedImports:ERROR</arg></compilerArgs><annotationProcessorPaths><path><groupId>com.google.errorprone</groupId><artifactId>error_prone_core</artifactId><version>2.4.0</version></path></annotationProcessorPaths></configuration></plugin>

What version of Error Prone are you using?

2.4.0

Have you found anything relevant by searching the web?

http://errorprone.info/docs/flags

Places to look:

@cnarsimharaju cnarsimharaju changed the title Pirahana with RemoveUnusedImports is not working RemoveUnusedImports is not working Oct 29, 2020
@cushon
Copy link
Collaborator

cushon commented Oct 29, 2020

-XepPatchChecks:Piranha

I think you also need -XepPatchChecks:RemoveUnusedImports ?

The following simple example works for me:

javac -XDcompilePolicy=simple -processorpath error_prone_core-2.4.0-with-dependencies.jar:dataflow-shaded-3.1.2.jar:jFormatString-3.0.0.jar \
  '-Xplugin:ErrorProne -XepDisableAllChecks -Xep:RemoveUnusedImports -XepPatchChecks:RemoveUnusedImports -XepPatchLocation:IN_PLACE'  \
  Test.java

@cnarsimharaju
Copy link
Author

Hey @cushon Even after adding this property i could not see removal of unused imports. I could also see piranha is not working if i add this. We are integrating this with our micro-service which uses maven for build.

@cushon
Copy link
Collaborator

cushon commented Nov 24, 2020

I could also see piranha is not working if i add this

I think I should have suggested -XepPatchChecks:Piranha,RemoveUnusedImports instead of passing -XepPatchChecks: twice.

If RemoveUnusedImports still isn't working for you, can you provide a minimal, self-contained example that demonstrates the problem?

@cnarsimharaju
Copy link
Author

Thanks @cushon this option worked for me. The only challenge is it is taking 2 passes

  1. In the first pass it removed my stale flag but imports are not imported.
  2. In the second pass it removed the unused imports.

@cushon
Copy link
Collaborator

cushon commented Jan 6, 2021

I think that's WAI: Error Prone checks run in parallel, they can't 'see' the code after it has been refactored by other checks. To use RemoveUnusedImports to clean up after another check it's necessary to run two serial passes.

(FWIW, the approach we use for cleaning up imports is to do that separately after applying fixes with a formatter.)

@cushon cushon closed this as completed Jan 6, 2021
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

2 participants