sbt-tpolecat icon indicating copy to clipboard operation
sbt-tpolecat copied to clipboard

Scala.js options overwritten on Scala 3

Open keynmol opened this issue 3 years ago • 5 comments

On Scala 3, -scalajs option is required to generate IR files. Without it, the compiler will complain, and the linking will succeed without actually doing anything.

In the app you will likely notice it immediately, in the libraries - not so much.

Desired behaviour: -scalajs option, if present, is preserved

───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ./Test.scala
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ @main def hello = println("yo")
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ./project/plugins.sbt
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ addSbtPlugin("io.github.davidgregory084" % "sbt-tpolecat"              % "0.4.1")
   2   │ addSbtPlugin("org.scala-js"              % "sbt-scalajs"               % "1.10.1")
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
───────┬──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: ./build.sbt
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
   1   │ scalaVersion := "3.1.3"
   2   │
   3   │ enablePlugins(ScalaJSPlugin)
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
/v/f/m/d/T/tmp.B2Uwm0IW > sbt run
[info] welcome to sbt 1.6.2 (GraalVM Community Java 17.0.3)
[info] loading global plugins from /Users/velvetbaldmime/.sbt/1.0/plugins
...
[warn] The `-scalajs` flag was missing from `compile / scalacOptions`, but it is required to produce Scala.js IR.
[warn] Linking, running and/or testing will probably go wrong.
[warn] The most likely cause is that you used `scalacOptions := ...` instead of using `++=`.

keynmol avatar Jul 28 '22 08:07 keynmol

I just stumbled across this issue via this project ( github.com/objektwerks/scalajs/blob/master/build.sbt ). To avoid getting this error during sbt clean test ( java.lang.AssertionError: assertion failed: asTerm called on not-a-Term val ), I had to add scalacOptions ++= Seq("-scalajs") to the build.sbt. Apparently sbt-tpolecat was removing the -scalajs option added by the Scalajs plugin ( addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.10.1") ).

objektwerks avatar Jul 28 '22 14:07 objektwerks

Unfortunately the obvious fix to this - appending the existing scalacOptions.value to the resulting scalacOptions after this plugin is applied - doesn't resolve the issue, or at least it causes more issues. I've started a discussion in sbt (sbt/sbt#6986) about how to resolve this so hopefully we will get some good advice there.

DavidGregory084 avatar Jul 28 '22 14:07 DavidGregory084

Beyond simple tasks, Sbt is like an extreme sport. So be careful. :)

objektwerks avatar Jul 28 '22 15:07 objektwerks

Just bumping this issue, it only occurred for me when I tried to go from sbt-tpolecat version 0.3.1 to 0.4.1. Same output as originally shown:

[info] Running scalafix on 1 Scala sources (incremental)
[warn] The `-scalajs` flag was missing from `compile / scalacOptions`, but it is required to produce Scala.js IR.
[warn] Linking, running and/or testing will probably go wrong.
[warn] The most likely cause is that you used `scalacOptions := ...` instead of using `++=`.

davesmith00000 avatar Aug 06 '22 14:08 davesmith00000

Sorry for the silence on this one folks - I'm going mad trying to find a way to solve this without breaking any of the previous behaviours of the library and while still accomodating the ways that folks set the various keys of the library. The combination of +=, ++= and sbt scope delegation is very hard to navigate.

DavidGregory084 avatar Sep 27 '22 11:09 DavidGregory084

Sorry for adding more salt to the wound, but I just found that sbt-tpolecat also causes problems with metals / semanticdb; where this plugin ends up removing the required scalac flags for it to work properly.

Apparently, the issue is related to the exclusions rule. I didn't test with main sources, but if I do this:

Test / tpolecatExcludeOptions ++= ScalacOptions.defaultConsoleExclude

Then metals is broken on the test files.

BalmungSan avatar Oct 10 '22 19:10 BalmungSan

PR available: #126

sjrd avatar Oct 28 '22 12:10 sjrd

I can confirm that reproducer no longer works with a locally published version of the plugin 🎉

keynmol avatar Jan 06 '23 12:01 keynmol