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

Unused import warning in routes (Play 2.6.0-RC1 and Scala 2.12.2) #7382

Open
maxcom opened this issue May 24, 2017 · 19 comments
Open

Unused import warning in routes (Play 2.6.0-RC1 and Scala 2.12.2) #7382

maxcom opened this issue May 24, 2017 · 19 comments

Comments

@maxcom
Copy link
Contributor

maxcom commented May 24, 2017

Play Version (2.5.x / etc)

2.6.0-RC1

API (Scala / Java / Neither / Both)

Scala

Expected Behavior

Play-2.6.0-RC1 project can be compiled with "-Xfatal-warnings" compiler option and Scala 2.12.2.

Actual Behavior

[error] /home/maxcom/git-checkout/abook-daemon/conf/routes: Unused import
[error] /home/maxcom/git-checkout/abook-daemon/conf/routes: Unused import
[error] /home/maxcom/git-checkout/abook-daemon/conf/routes: Unused import
[error] three errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 20 s, completed 24.05.2017 12:19:33

I think that unused import is

import _root_.controllers.Assets.Asset

in generated Routes and ReverseRoutes

@marcospereira
Copy link
Member

Hey @maxcom,

If you are not using Assets controller, you can remove the import by adding the following lines to your build.sbt file:

import play.sbt.routes.RoutesKeys
RoutesKeys.routesImport := Seq.empty

Can you confirm that using this configuration fixes the problem for you? _root_.controllers.Assets.Asset is being imported because it is part of the default configuration, as you can see here:

RoutesKeys.routesImport ++= Seq("controllers.Assets.Asset"),

Thanks.

@maxcom
Copy link
Contributor Author

maxcom commented Jun 5, 2017

Thanx, I used

RoutesKeys.routesImport -= "controllers.Assets.Asset"

That did reduce number of warnings, but did not solve all of them.

@maxcom
Copy link
Contributor Author

maxcom commented Jun 5, 2017

Other unused imports are

import _root_.utils.Binders._

in ReverseRoutes.scala. We actually use reverse routes and we use binders in routes.

@marcospereira
Copy link
Member

Thanks for the feedback, @maxcom.

Is Binders part of your application code, right? Maybe we should have more specific configurations for ReverseRouter and JavascriptReverseRouter.

WDYT?

@maxcom
Copy link
Contributor Author

maxcom commented Jun 6, 2017

I'm not sure that it will help. Reverse routes is not the one file, but it is generated for each individual package. Some controllers uses only base types, some uses custom types and Options.

Maybe there is the way to disable this warnings for generated files only?

@gmalouf
Copy link

gmalouf commented Nov 9, 2017

I just want to chime in I am seeing what I believe is the same warning except in my template files:

scala 2.12.4
play 2.6.7

@(elements: helper.FieldElements)

<div class="form-group checkbox-group @if(elements.hasErrors) {error}">
    @elements.input
    <label for="@elements.id" class="checkbox">@elements.label</label>
    <span class="errors">@elements.errors.mkString(", ")</span>
</div>
[warn] /Users/gary/AdAgility/coriander/app/com/offerlogic/coriander/infrastructure/display/checkboxFormHelper.scala.html:1: Unused import
[warn] @(elements: helper.FieldElements)

@gmalouf
Copy link

gmalouf commented Nov 9, 2017

Disabling -Xlint gets rid of the warnings - having said that this feels like a regression.

@mihaisoloi
Copy link

This is still happening with application code Binders imported into the routes file from build.sbt using routesImport and the -Ywarn-unused:imports flag in scalacOptions. I am not using the Binders on all of the controllers but I can't specify on which controller the import should be used, so I guess that's what leads to the issue.

scala 2.12.4
play 2.6.11

@steinybot
Copy link

steinybot commented Mar 23, 2018

This still occurs in Scala 2.12.5, play 2.6.12.

The warning comes from scala.tools.nsc.typechecker.Contexts#warnUnusedImports. The import it complains about is import _root_.controllers.Assets.Asset which is in ReverseRoutes.scala, JavaScriptReverseRoutes.scala and Routes.scala.

Adding:

routesImport := Seq.empty

To the bulid does solve the problem but it is annoying that this is needed.

@gavares
Copy link
Contributor

gavares commented Mar 25, 2018

This also seems to be triggered when using multiple routes files like:
conf/routes:

GET    /api/    controllers.MyController.showApiVersion
->     /ui      controllers.ui.MyUIController.serveUI

Compilation fails with:

/Users/gavares/workspace/my-proj/conf/routes: Unused import
/Users/gavares/workspace/my-proj/conf/routes: Unused import
two errors found

@iaco86
Copy link

iaco86 commented Oct 25, 2018

I'm having the same problem while upgrading to play 2.6 and scala 2.12 - also the project is gradle-based so I was wondering if there's any workaround for gradle. I looked into it and couldn't find any...
Thanks!

@marcospereira
Copy link
Member

Hi @iaco86, when using Gradle, I think you can tweak the defaultImports list to what you want/need.

@iaco86
Copy link

iaco86 commented Oct 30, 2018

defaultImports seems to be using only the enum

public enum TwirlImports {
  SCALA, JAVA
}

@mihaisoloi
Copy link

I've introduced a way to silence the unused imports errors generated by the Play Routes in the scalac plugin silencer. You just need to add the following in your build.sbt

// silence all warnings on autogenerated files
scalacOptions += "-P:silencer:pathFilters=target/.*" 
// Make sure you only exclude warnings for the project directories, i.e. make builds reproducible
scalacOptions += s"-P:silencer:sourceRoots=${baseDirectory.value.getCanonicalPath}"

@iaco86
Copy link

iaco86 commented Dec 6, 2018

I was following your PR, thanks a lot @mihaisoloi !

@balagez
Copy link

balagez commented May 15, 2020

With Scala 2.13.2's configurable warnings you can suppress all warnings like this:

scalacOptions += s"-Wconf:src=${target.value}/.*:s"

@steinybot
Copy link

A variation on @mihaisoloi's solution which limits the scope of silencer a bit more:

    scalacOptions ++= Seq(
      s"-P:silencer:sourceRoots=${(Compile / routes / target).value}",
      s"-P:silencer:pathFilters=.*"
    ),

If you have other paths that you are filtering then you can relativize the target to the base directory and put that in the filter instead.

@leszekgruchala
Copy link

For anyone looking for a no-plugin solution, I've come up with a working one for Scala 2.13.7: https://stackoverflow.com/a/70418263/804812

@mkurz
Copy link
Member

mkurz commented Mar 9, 2023

@mkurz mkurz modified the milestones: 2.9.0, 2.10.0 May 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: 🆕 New
Development

No branches or pull requests