Skip to content

Commit 3b5938f

Browse files
authored
Fix plugin for Scala 3 projects (#76)
Our previous logic relied on withDefaultValue in the Scalameta version map to get the version of scalameta plugin. Issue is, withDefaultValue does not affect .get operations: ```scala Welcome to Scala 2.12.18 (OpenJDK 64-Bit Server VM, Java 17.0.6). Type in expressions for evaluation. Or try :help. scala> val mp = Map("a" -> 2).withDefaultValue(3) mp: scala.collection.immutable.Map[String,Int] = Map(a -> 2) scala> mp.get("c") res0: Option[Int] = None ``` Moreover, we don't need to add semanticdb plugin at all if we're working on a Scala 3 project. This comment fixes the logic to be explicit about Scala 3. Additionally, we amend the semanticdb parameters used to customise output location, according to https://github.com/sbt/sbt/blob/1.9.x/main/src/main/scala/sbt/plugins/SemanticdbPlugin.scala#L92 * Clean up the build 1. Remove Java 17-ism 2. Upgrade SBT to 1.9.3 3. Remove sbt-bloop (it's added automatically by metals)
1 parent 75bda00 commit 3b5938f

File tree

11 files changed

+65
-22
lines changed

11 files changed

+65
-22
lines changed

build.sbt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,9 @@ Compile / resourceGenerators += Def.task {
5151
if (!out.exists()) {
5252
val versions = Versions.semanticdbVersionsByScalaVersion()
5353
val props = new Properties()
54-
props.putAll(versions.asJava)
54+
versions.foreach { case (k, v) =>
55+
props.put(k, v)
56+
}
5557
IO.write(props, "SemanticDB versions grouped by Scala version.", out)
5658
}
5759
List(out)
@@ -84,3 +86,4 @@ scriptedLaunchOpts ++= Seq(
8486
)
8587

8688
def isCI = "true" == System.getenv("CI")
89+
ThisBuild / version := { if (!isCI) "dev" else version.value }

project/build.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
sbt.version=1.5.2
1+
sbt.version=1.9.3

project/plugins.sbt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ addSbtPlugin("com.geirsson" % "sbt-ci-release" % "1.5.5")
22
addSbtPlugin("com.eed3si9n" % "sbt-buildinfo" % "0.10.0")
33
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.4.3")
44
addSbtPlugin("ch.epfl.scala" % "sbt-scalafix" % "0.9.29")
5-
addSbtPlugin("ch.epfl.scala" % "sbt-bloop" % "1.4.9-22-2d02726c")
65

76
libraryDependencies += "org.scala-sbt" %% "scripted-plugin" % sbtVersion.value
87

src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphEnable.scala

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@ object SourcegraphEnable {
2525
|2. conditionally sets semanticdbVersion & scalaVersion when support is not built-in in the compiler""".stripMargin
2626
) { s =>
2727
val extracted = Project.extract(s)
28+
2829
val scalacOptionsSettings = Seq(Compile, Test).flatMap(
2930
inConfig(_)(SourcegraphPlugin.relaxScalacOptionsConfigSettings)
3031
)
32+
3133
val semanticdbJavacVersion = Versions.semanticdbJavacVersion()
3234
val settings = for {
3335
(p, semanticdbVersion, overriddenScalaVersion) <- collectProjects(
@@ -47,13 +49,16 @@ object SourcegraphEnable {
4749
),
4850
overriddenScalaVersion.map(v => scalaVersion.in(p) := v),
4951
Option(SemanticdbPlugin.semanticdbEnabled.in(p) := true),
50-
Option(SemanticdbPlugin.semanticdbVersion.in(p) := semanticdbVersion)
52+
semanticdbVersion.map(ver =>
53+
SemanticdbPlugin.semanticdbVersion.in(p) := ver
54+
)
5155
).flatten
5256
settings <-
5357
inScope(ThisScope.in(p))(
5458
scalacOptionsSettings
5559
) ++ enableSemanticdbPlugin
5660
} yield settings
61+
5762
Compat.append(extracted, settings, s)
5863
}
5964

@@ -88,11 +93,14 @@ object SourcegraphEnable {
8893
scalaVersion.in(p) := v
8994
},
9095
Option(
91-
allDependencies.in(p) += compilerPlugin(
92-
SourcegraphPlugin.autoImport.sourcegraphSemanticdb(
93-
semanticdbVersion
94-
)
95-
)
96+
allDependencies.in(p) ++=
97+
semanticdbVersion.map { ver =>
98+
compilerPlugin(
99+
SourcegraphPlugin.autoImport.sourcegraphSemanticdb(
100+
ver
101+
)
102+
)
103+
}.toSeq
96104
)
97105
).flatten
98106
settings <-
@@ -105,8 +113,8 @@ object SourcegraphEnable {
105113

106114
private val semanticdbConfigSettings: Seq[Def.Setting[_]] =
107115
Seq(
108-
scalacOptions.in(compile) := {
109-
val old = scalacOptions.in(compile).value
116+
scalacOptions := {
117+
val old = scalacOptions.value
110118
val options = List(
111119
"-Yrangepos",
112120
"-Xplugin-require:semanticdb"
@@ -119,7 +127,7 @@ object SourcegraphEnable {
119127

120128
private def collectProjects[U](
121129
extracted: Extracted
122-
): Seq[(ProjectRef, String, Option[String])] = for {
130+
): Seq[(ProjectRef, Option[String], Option[String])] = for {
123131
p <- extracted.structure.allProjectRefs
124132
projectScalaVersion <- scalaVersion
125133
.in(p)
@@ -132,8 +140,7 @@ object SourcegraphEnable {
132140
)
133141
Some("2.11.12")
134142
else None
135-
semanticdbVersion <- Versions
143+
semanticdbVersion = Versions
136144
.semanticdbVersion(overriddenScalaVersion.getOrElse(projectScalaVersion))
137-
.toList
138145
} yield (p, semanticdbVersion, overriddenScalaVersion)
139146
}

src/main/scala/com/sourcegraph/sbtsourcegraph/SourcegraphPlugin.scala

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -180,13 +180,26 @@ object SourcegraphPlugin extends AutoPlugin {
180180
def configSettings: Seq[Def.Setting[_]] = List(
181181
sourcegraphUpload := sourcegraphUpload.value,
182182
sourcegraphScalacTargetroot := {
183-
val customDirectory = (for {
184-
option <- scalacOptions.value
185-
if option.startsWith("-P:semanticdb:targetroot:")
186-
} yield new File(
187-
option.stripPrefix("-P:semanticdb:targetroot:")
188-
)).lastOption
189-
customDirectory.getOrElse(classDirectory.value)
183+
val scala2Prefix = "-P:semanticdb:targetroot:"
184+
val scala3Prefix = "-semanticdb-target"
185+
186+
val customDirectory = scalacOptions.value
187+
.collectFirst {
188+
case flag if flag.startsWith(scala2Prefix) =>
189+
flag.stripPrefix(scala2Prefix)
190+
case flag if flag.startsWith(scala3Prefix) =>
191+
flag.stripPrefix(scala3Prefix)
192+
}
193+
.map(new File(_))
194+
195+
lazy val root = SemanticdbPlugin.semanticdbTargetRoot.?.value
196+
197+
val explicitRoot =
198+
if (SemanticdbPlugin.isAvailable())
199+
root
200+
else None
201+
202+
explicitRoot.orElse(customDirectory).getOrElse(classDirectory.value)
190203
},
191204
sourcegraphJavacTargetroot := {
192205
(for {

src/main/scala/com/sourcegraph/sbtsourcegraph/Versions.scala

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ object Versions {
1515
throw new NoSuchElementException(semanticdbJavacKey)
1616
)
1717
def semanticdbVersion(scalaVersion: String): Option[String] =
18-
cachedSemanticdbVersionsByScalaVersion.get(scalaVersion)
18+
// Scala 3 has semanticdb generation built in, so we don't need to
19+
// add the semanticdb-scalac plugin
20+
if (scalaVersion.startsWith("3."))
21+
None
22+
else
23+
cachedSemanticdbVersionsByScalaVersion.get(scalaVersion)
1924
lazy val cachedSemanticdbVersionsByScalaVersion: Map[String, String] = {
2025
val key = "/sbt-sourcegraph/semanticdb.properties"
2126
val in = this.getClass().getResourceAsStream(key)
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
scalaVersion := "3.3.0"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=1.9.3
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
addSbtPlugin(
2+
"com.sourcegraph" % "sbt-sourcegraph" % sys.props("plugin.version")
3+
)
4+
5+
libraryDependencies += "com.sourcegraph" % "scip-semanticdb" %
6+
sys.props("scip-java.version")
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
object Hello:
2+
def world() = ""
3+
4+
@main def entrypoint = println(Hello.world())

0 commit comments

Comments
 (0)