Open MasseGuillaume opened 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)
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?
You can set TwirlKeys.templateImports := Seq()
which will actually remove all the imports. However you should re add the necessary onces.
Customizing the templateImports
doesn't really fix the issue when you have different templates needing different imports. Unless I'm missing something?
@bfil if you set the value of TwirlKeys.templateImports
to 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.
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.
This is similar to https://github.com/playframework/playframework/issues/7382 although far more painful to workaround.
@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
This becomes even more painful when using gradle - no workaround to be found there :(
We are also experiencing this. Any chance of a fix?
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.
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.
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 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
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
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"
)
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/.*"
-Wconf
works in Scala 2.12.13 and newer, too.
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
I have
"-Xfatal-warnings", "-Ywarn-unused-import",
in my scalacOptionsI cannot use twirl