twirl icon indicating copy to clipboard operation
twirl copied to clipboard

Unused import

Open MasseGuillaume opened this issue 8 years ago • 19 comments

I have "-Xfatal-warnings", "-Ywarn-unused-import", in my scalacOptions

I cannot use twirl

import play.twirl.api._
import play.twirl.api.TemplateMagic._

class error extends BaseScalaTemplate[play.twirl.api.HtmlFormat.Appendable,Format[play.twirl.api.HtmlFormat.Appendable]](play.twirl.api$

MasseGuillaume avatar Jun 06 '16 18:06 MasseGuillaume

One workaround I did was to add a dummy line somewhere that uses both imports: @if(List.empty[Txt]){}

(Uses play.twirl.api.Txt and play.twirl.api.TemplateMagic.iterableToBoolean)

kusamakura avatar Jun 29 '16 05:06 kusamakura

Any chance for this to get fixed? Or maybe a pointer (for a person such as myself who hasn't worked with sbt plugins) where to look at in order to get this fixed?

Pyppe avatar Nov 01 '16 12:11 Pyppe

You can set TwirlKeys.templateImports := Seq() which will actually remove all the imports. However you should re add the necessary onces.

schmitch avatar Nov 01 '16 12:11 schmitch

Customizing the templateImports doesn't really fix the issue when you have different templates needing different imports. Unless I'm missing something?

bfil avatar Jan 14 '17 23:01 bfil

@bfil if you set the value of TwirlKeys.templateImportsto an empty Seq like @schmitch suggested, then it will solve the issue, since no imports will be automatically added to the views. You can then re-add the necessary imports to each view.

marcospereira avatar Jan 16 '17 14:01 marcospereira

I was thinking: if I have template1.scala.html using @Html and template2.scala.html using @Txt, then If I add both to the SBT settings template1 will say unused import on Txt and template2 will say unused import on Html, cause it's a global setting.

I guess the suggestion is removing them all from the SBT settings and adding the import line within the templates themselves.

bfil avatar Jan 16 '17 16:01 bfil

This is similar to https://github.com/playframework/playframework/issues/7382 although far more painful to workaround.

steinybot avatar Mar 27 '18 03:03 steinybot

@marcospereira which import would I need to add in order to use the traditional syntax of routes.Assets.versioned("someFile.js")? edit: got it figured out; no import, just need to refer directly to controllers.routes.Assets; there must be some magic going on I don't quite understand. Anyway, happy to be failing on warnings now

mckinley-olsen-oc avatar Jun 13 '18 05:06 mckinley-olsen-oc

This becomes even more painful when using gradle - no workaround to be found there :(

iaco86 avatar Oct 25 '18 19:10 iaco86

We are also experiencing this. Any chance of a fix?

robinspollak avatar Dec 19 '18 17:12 robinspollak

here must be some magic going on I don't quite understand

@mckinley-olsen-oc there are some default imports made for all the templates. That is why you can use routes.Assets without writing an import by yourself.

marcospereira avatar Jan 03 '19 18:01 marcospereira

We are also experiencing this. Any chance of a fix?

Hey @robinspollak not yet. Contributions would be very helpful here since this is not a priority right now. If you are using sbt, it should be possible to use silencer compiler plugin to filter code generated by twirl.

marcospereira avatar Jan 03 '19 18:01 marcospereira

workaround ideas:

  1. exile the generated files to their own subproject, and set scalacOptions appropriately in that subproject only
  2. -Wconf (new in Scala 2.13.2 / 2.12.13: https://www.scala-lang.org/2021/01/12/configuring-and-suppressing-warnings.html; replaces silencer) offers many facilities for suppressing warnings, perhaps a src=... filter would do the trick?

SethTisue avatar Feb 22 '21 01:02 SethTisue

@SethTisue Actually my plan for Play is to set a -Wconf flag by default so all these unused import warnings for twirl templates would be ignored. That would mean however users have to use at least Scala 2.13.2 and 2.12.13 - that's why I am going to implement this on Play's master branch only and we have to make those Scala versions a requirement for upcoming Play 2.9

mkurz avatar Feb 22 '21 11:02 mkurz

I can confirm that @SethTisue 's suggestion works on Scala 2.12.13+:

scalacOptions ++= Seq(
  "-deprecation",
  "-feature",
  "-Xfatal-warnings",
  "-Xlint",
  // Suggested here https://github.com/playframework/twirl/issues/105#issuecomment-782985171
  "-Wconf:src=routes/.*:is,src=twirl/.*:is",
...

This prints:

[info] 86 unused info messages; change -Wconf for cat=unused to display individual messages

yamin-oanda avatar Jan 13 '22 18:01 yamin-oanda

These are just workarounds, what about to add a flag to disable default imports defined in TwirlCompiler.scala?

  val DefaultImports = Seq(
    "_root_.play.twirl.api.TwirlFeatureImports._",
    "_root_.play.twirl.api.TwirlHelperImports._",
    "_root_.play.twirl.api.Html",
    "_root_.play.twirl.api.JavaScript",
    "_root_.play.twirl.api.Txt",
    "_root_.play.twirl.api.Xml"
  )

mpro7 avatar Aug 25 '22 08:08 mpro7

For Scala 2.13.2 version and above use: scalacOptions +== "-Wconf:src=target/.*:s"

For other versions of Scala use Silencer plugin: scalacOptions +== "-P:silencer:pathFilters=target/.*"

mpro7 avatar Aug 25 '22 09:08 mpro7

-Wconf works in Scala 2.12.13 and newer, too.

SethTisue avatar Aug 25 '22 13:08 SethTisue