From 82cf4fa4f54990202c0c85d36d45ad5584e8e320 Mon Sep 17 00:00:00 2001 From: Chris Kipp Date: Mon, 29 Nov 2021 22:13:19 +0100 Subject: [PATCH] Don't add semanticdb plugin for Scala 3. For Scala 3 semanticdb is produced natively by the compiler behind the `-Xsemanticdb` flag, so the plugin isn't necessary. This change ensures the correct scalacOptions are set when using Scala 3 with ScalaMetalsSupport. --- .../scalalib/bsp/ScalaMetalsSupport.scala | 35 +++++++++++++++---- 1 file changed, 29 insertions(+), 6 deletions(-) diff --git a/scalalib/src/mill/scalalib/bsp/ScalaMetalsSupport.scala b/scalalib/src/mill/scalalib/bsp/ScalaMetalsSupport.scala index eb7ca9ef25b..c9e2f6989fc 100644 --- a/scalalib/src/mill/scalalib/bsp/ScalaMetalsSupport.scala +++ b/scalalib/src/mill/scalalib/bsp/ScalaMetalsSupport.scala @@ -3,23 +3,46 @@ package mill.scalalib.bsp import mill.api.experimental import mill.{Agg, T} import mill.define.Target +import mill.scalalib.api.ZincWorkerUtil.isScala3 import mill.scalalib.{Dep, DepSyntax, ScalaModule} +import mill.api.Result /*+ Enable some common settings required to properly support Metals Language Server (via BSP). */ @experimental trait ScalaMetalsSupport extends ScalaModule { - /** The semanticDB version to use. It needs to support your configured Scala versions. */ - def semanticDbVersion: T[String] + /** The semanticDB version to use. It needs to support your configured Scala 2 version. */ + def semanticDbVersion: T[String] = T { "" } + override def scalacPluginIvyDeps: Target[Agg[Dep]] = T { - super.scalacPluginIvyDeps() ++ Agg( - ivy"org.scalameta:::semanticdb-scalac:${semanticDbVersion()}" - ) + if (!isScala3(scalaVersion()) && semanticDbVersion().isEmpty) { + val msg = + """| + |When using ScalaMetalsSupport with Scala 2 you must provide a semanticDbVersion + | + |def semanticDbVersion = ??? + |""".stripMargin + Result.Failure(msg) + } else if (isScala3(scalaVersion())) { + Result.Success(super.scalacPluginIvyDeps()) + } else { + Result.Success( + super.scalacPluginIvyDeps() ++ Agg( + ivy"org.scalameta:::semanticdb-scalac:${semanticDbVersion()}" + ) + ) + } } /** Adds some options and configures the semanticDB plugin. */ override def mandatoryScalacOptions: Target[Seq[String]] = T { - super.mandatoryScalacOptions() ++ Seq("-Yrangepos", s"-P:semanticdb:sourceroot:${T.workspace}") + super.mandatoryScalacOptions() ++ { + if (isScala3(scalaVersion())) { + Seq("-Xsemanticdb") + } else { + Seq("-Yrangepos", s"-P:semanticdb:sourceroot:${T.workspace}") + } + } } /** Filters options unsupported by Metals. */