playframework / twirl

Twirl is Play's default template engine
Apache License 2.0
549 stars 108 forks source link

Unused import #105

Open MasseGuillaume opened 8 years ago

MasseGuillaume commented 8 years ago

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$
kusamakura commented 8 years ago

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)

Pyppe commented 8 years ago

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?

schmitch commented 8 years ago

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

bfil commented 7 years ago

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

marcospereira commented 7 years ago

@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.

bfil commented 7 years ago

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.

steinybot commented 6 years ago

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

mckinley-olsen-oc commented 6 years ago

@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

iaco86 commented 6 years ago

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

robinspollak commented 5 years ago

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

marcospereira commented 5 years ago

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 commented 5 years ago

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.

SethTisue commented 3 years ago

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?

mkurz commented 3 years ago

@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

yamin-oanda commented 2 years ago

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
mpro7 commented 2 years ago

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 commented 2 years ago

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/.*"

SethTisue commented 2 years ago

-Wconf works in Scala 2.12.13 and newer, too.

mpro7 commented 2 months ago

Scala 3 supports it from version 3.5.0, in lower Scala 3 versions you will get the fallowing warning:

[warn] Failed to parse `-Wconf` configuration: src=target/.*:s
[warn] unknown filter: src