cosmicsilence / gradle-scalafix

Gradle plugin for Scalafix
BSD 3-Clause "New" or "Revised" License
27 stars 5 forks source link

Scala 3 support #60

Closed gianluca-nitti closed 11 months ago

gianluca-nitti commented 2 years ago

Recent scalafix versions support Scala 3. I'm trying to use this plugin in my project which uses Scala 3 and Gradle. Is this possible?

Using release 0.1.13 of the plugin, I get the following behavior:

Task :app:compileScala FAILED


* If I understand correctly, [SemanticDB support is built in the Scala 3 compiler](https://github.com/joan38/mill-scalafix/issues/46#issuecomment-858942933), so there is no plugin which to add. To work around this, I tried disabling the autoConfigure and manually passing the compiler flag needed to enable SemanticDB:
```gradle
// build.gradle
scalafix {
configFile = file("scalafix.conf")
semanticdb {
// this would automatically add compiler flags to generate extra info
// required by some scalafix rules, but is not compatible with Scala 3
// so we add them manually (see below)
autoConfigure = false
}
}

tasks.withType(ScalaCompile) { scalaCompileOptions.additionalParameters = ['-Xsemanticdb'] // for scalafix }

Which however, with semantic rules enabled in `scalafix.conf`, fails with this output:

Task :app:checkScalafixMain FAILED

FAILURE: Build failed with an exception.

marcelocenerine commented 2 years ago

@gianluca-nitti support for Scala 3 in Gradle is relatively recent and this plugin doesn't support that yet 😞 . I don't think there is any workaround you could use atm... The main problem which affects both syntactic and semantic rules is this logic to figure out the version of Scala used in your project, which doesn't correctly work for Scala 3.

marcelocenerine commented 2 years ago

Thanks for bringing this up though. This is something that would be great to add.

Just one note from the Scalafix docs:

Scala 3.x: Scala 3 support is experimental and many built-in rules are not supported.

gianluca-nitti commented 2 years ago

I understand, thanks for the information.

olafurpg commented 2 years ago

[Error] : bad option: -P:semanticdb:sourceroot:/home/gianluca/MYPROJECT/app

The Scala 3 equivalent flag is called -sourceroot and -semanticdb-targetroot for -P:semanticdb:targetroot. One good source of inspiration is the built-in SemanticDB support in sbt https://github.com/sbt/sbt/blob/6c537848266e93674b1de4673c084e8b3f2039df/main/src/main/scala/sbt/plugins/SemanticdbPlugin.scala#L91

I seem to get past the SemanticDB problem but I get parsing errors in files which use certain Scala 3 features such as extension methods.

@marcelocenerine The Scala 3 dialect should be picked up automatically through the --scala-version setting, which seems to be passed correctly here https://github.com/cosmicsilence/gradle-scalafix/blob/master/src/main/groovy/io/github/cosmicsilence/scalafix/ScalafixTask.groovy#L81 cc/ @mlachkar can maybe confirm. Alternatively, it could be that things are working correctly but you're using Scala 3 syntax that's not yet supported by the Scalameta parser (cc/ @tgodzik)

I think the reason could be that it's using a scalafix artifact for Scala 2.13:

Scalafix supports Scala 3 even if Scalafix itself it still compiled with 2.13. There's on Scala 3 artifact for Scalafix yet.

mlachkar commented 2 years ago

In fact Scalafix support scala 3, the only thing we don't have yet is ExcplicitResultType working for scala 3.

@bjaglin can you please confirm ? Thanks

If you need any help, don't hesitate, I would be happy to help!

marcelocenerine commented 2 years ago

Thank you for your prompt response and for sharing all this info, @olafurpg & @mlachkar. I hope get started on this soon 👍