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 #105

Open
MasseGuillaume opened this issue Jun 6, 2016 · 18 comments
Open

Unused import #105

MasseGuillaume opened this issue Jun 6, 2016 · 18 comments

Comments

@MasseGuillaume
Copy link

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
Copy link

kusamakura commented Jun 29, 2016

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
Copy link

Pyppe commented Nov 1, 2016

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
Copy link
Contributor

schmitch commented Nov 1, 2016

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

@bfil
Copy link

bfil commented Jan 14, 2017

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

@marcospereira
Copy link
Member

@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
Copy link

bfil commented Jan 16, 2017

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.

digital-wonderland added a commit to digital-wonderland/play-webpack-typescript-react that referenced this issue Oct 25, 2017
This requires the default Twirl imports to be disabled because of
the unused import check.

See
scala/bug#10270
and
playframework/twirl#105
@steinybot
Copy link

This is similar to playframework/playframework#7382 although far more painful to workaround.

@mckinley-olsen-oc
Copy link

mckinley-olsen-oc commented Jun 13, 2018

@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
Copy link

iaco86 commented Oct 25, 2018

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

@robinspollak
Copy link

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

@marcospereira
Copy link
Member

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
Copy link
Member

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
Copy link
Member

SethTisue commented Feb 22, 2021

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
Copy link
Member

mkurz commented Feb 22, 2021

@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
Copy link

yamin-oanda commented Jan 13, 2022

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
Copy link

mpro7 commented Aug 25, 2022

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
Copy link

mpro7 commented Aug 25, 2022

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
Copy link
Member

-Wconf works in Scala 2.12.13 and newer, too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests