Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Advertize Kotlin modules as Scala ones via BSP
Browse files Browse the repository at this point in the history
So that they're handled fine by intellij-scala in IntelliJ, that's fine
with Kotlin sources in Scala modules apparently
alexarchambault committed Oct 1, 2024

Unverified

The email in this signature doesn’t match the committer email.
1 parent d419c31 commit 01bba7f
Showing 21 changed files with 282 additions and 27 deletions.
2 changes: 1 addition & 1 deletion bsp/src/mill/bsp/Constants.scala
Original file line number Diff line number Diff line change
@@ -6,6 +6,6 @@ private[mill] object Constants {
val bspProtocolVersion = BuildInfo.bsp4jVersion
val bspWorkerImplClass = "mill.bsp.worker.BspWorkerImpl"
val bspWorkerBuildInfoClass = "mill.bsp.worker.BuildInfo"
val languages: Seq[String] = Seq("java", "scala")
val languages: Seq[String] = Seq("java", "scala", "kotlin")
val serverName = "mill-bsp"
}
3 changes: 2 additions & 1 deletion build.mill
Original file line number Diff line number Diff line change
@@ -428,7 +428,8 @@ trait MillBaseTestsModule extends TestModule {
s"-DTEST_SCALATEST_VERSION=${Deps.TestDeps.scalaTest.dep.version}",
s"-DTEST_TEST_INTERFACE_VERSION=${Deps.sbtTestInterface.dep.version}",
s"-DTEST_ZIOTEST_VERSION=${Deps.TestDeps.zioTest.dep.version}",
s"-DTEST_ZINC_VERSION=${Deps.zinc.dep.version}"
s"-DTEST_ZINC_VERSION=${Deps.zinc.dep.version}",
s"-DMILL_TEST_KOTLIN_VERSION=${Deps.kotlinCompiler.dep.version}"
)
}

Original file line number Diff line number Diff line change
@@ -8,6 +8,16 @@
"file:///workspace/hello-java/compile-resources"
]
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"classpath": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>.jar",
"file:///workspace/hello-kotlin/compile-resources"
]
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -6,6 +6,17 @@
},
"modules": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"modules": [
{
"name": "org.jetbrains.kotlin:kotlin-stdlib",
"version": "<kotlin-version>"
}
]
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -6,6 +6,15 @@
},
"sources": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"sources": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>-sources.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>-sources.jar"
]
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -10,6 +10,18 @@
],
"classDirectory": "file:///workspace/out/hello-java/compile.dest/classes"
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"options": [],
"classpath": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>.jar",
"file:///workspace/hello-kotlin/compile-resources"
],
"classDirectory": "file:///workspace/out/hello-kotlin/compile.dest/classes"
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -14,6 +14,22 @@
"environmentVariables": {},
"mainClasses": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"classpath": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>.jar",
"file:///workspace/hello-kotlin/compile-resources",
"file:///workspace/hello-kotlin/resources",
"file:///workspace/out/hello-kotlin/compile.dest/classes"
],
"jvmOptions": [],
"workingDirectory": "/workspace",
"environmentVariables": {},
"mainClasses": []
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -14,6 +14,22 @@
"environmentVariables": {},
"mainClasses": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"classpath": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>.jar",
"file:///workspace/hello-kotlin/compile-resources",
"file:///workspace/hello-kotlin/resources",
"file:///workspace/out/hello-kotlin/compile.dest/classes"
],
"jvmOptions": [],
"workingDirectory": "/workspace",
"environmentVariables": {},
"mainClasses": []
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -6,6 +6,12 @@
},
"outputPaths": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"outputPaths": []
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -6,6 +6,12 @@
},
"resources": []
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"resources": []
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -10,6 +10,18 @@
],
"classDirectory": "file:///workspace/out/hello-java/compile.dest/classes"
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"options": [],
"classpath": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/kotlin/kotlin-stdlib/<kotlin-version>/kotlin-stdlib-<kotlin-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jetbrains/annotations/<annotations-version>/annotations-<annotations-version>.jar",
"file:///workspace/hello-kotlin/compile-resources"
],
"classDirectory": "file:///workspace/out/hello-kotlin/compile.dest/classes"
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -12,6 +12,18 @@
}
]
},
{
"target": {
"uri": "file:///workspace/hello-kotlin"
},
"sources": [
{
"uri": "file:///workspace/hello-kotlin/src",
"kind": 2,
"generated": false
}
]
},
{
"target": {
"uri": "file:///workspace/hello-scala"
Original file line number Diff line number Diff line change
@@ -6,19 +6,22 @@
"compileProvider": {
"languageIds": [
"java",
"scala"
"scala",
"kotlin"
]
},
"testProvider": {
"languageIds": [
"java",
"scala"
"scala",
"kotlin"
]
},
"runProvider": {
"languageIds": [
"java",
"scala"
"scala",
"kotlin"
]
},
"debugProvider": {
Original file line number Diff line number Diff line change
@@ -26,6 +26,48 @@
"javaVersion": "<java-version>"
}
},
{
"id": {
"uri": "file:///workspace/hello-kotlin"
},
"displayName": "hello-kotlin",
"baseDirectory": "file:///workspace/hello-kotlin",
"tags": [
"library",
"application"
],
"languageIds": [
"java",
"scala",
"kotlin"
],
"dependencies": [],
"capabilities": {
"canCompile": true,
"canTest": false,
"canRun": true,
"canDebug": false
},
"dataKind": "scala",
"data": {
"scalaOrganization": "org.scala-lang",
"scalaVersion": "<scala-version>",
"scalaBinaryVersion": "2.13",
"platform": 1,
"jars": [
"file:///coursier-cache/https/repo1.maven.org/maven2/org/scala-lang/scala-compiler/<scala-version>/scala-compiler-<scala-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/scala-lang/scala-reflect/<scala-version>/scala-reflect-<scala-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/scala-lang/scala-library/<scala-version>/scala-library-<scala-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/io/github/java-diff-utils/java-diff-utils/<java-diff-utils-version>/java-diff-utils-<java-diff-utils-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/org/jline/jline/<jline-version>/jline-<jline-version>.jar",
"file:///coursier-cache/https/repo1.maven.org/maven2/net/java/dev/jna/jna/<jna-version>/jna-<jna-version>.jar"
],
"jvmBuildTarget": {
"javaHome": "file:///java-home/",
"javaVersion": "<java-version>"
}
}
},
{
"id": {
"uri": "file:///workspace/hello-scala"
9 changes: 6 additions & 3 deletions integration/ide/bsp-server/resources/project/build.mill
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package build

import mill._
import mill.scalalib._

object `hello-java` extends JavaModule
object `hello-java` extends scalalib.JavaModule

object `hello-scala` extends ScalaModule {
object `hello-scala` extends scalalib.ScalaModule {
def scalaVersion = Option(System.getenv("MILL_TEST_SCALA_2_13_VERSION")).getOrElse(???)
}

object `hello-kotlin` extends kotlinlib.KotlinModule {
def kotlinVersion = Option(System.getenv("MILL_TEST_KOTLIN_VERSION")).getOrElse(???)
}
28 changes: 25 additions & 3 deletions integration/ide/bsp-server/src/BspServerTests.scala
Original file line number Diff line number Diff line change
@@ -42,9 +42,31 @@ object BspServerTests extends UtestIntegrationTestSuite {
}
}

val replaceAll = replaceAllValues(workspacePath) ++ scalaTransitiveSubstitutions ++ Seq(
scalaVersion -> "<scala-version>"
)
val kotlinVersion = sys.props.getOrElse("MILL_TEST_KOTLIN_VERSION", ???)
val kotlinTransitiveSubstitutions = {
val scalaFetchRes = coursierapi.Fetch.create()
.addDependencies(coursierapi.Dependency.of(
"org.jetbrains.kotlin",
"kotlin-stdlib",
kotlinVersion
))
.fetchResult()
scalaFetchRes.getDependencies.asScala
.filter(dep => dep.getModule.getOrganization != "org.jetbrains.kotlin")
.map { dep =>
def basePath(version: String) =
s"${dep.getModule.getOrganization.split('.').mkString("/")}/${dep.getModule.getName}/$version/${dep.getModule.getName}-$version"
basePath(dep.getVersion) -> basePath(s"<${dep.getModule.getName}-version>")
}
}

val replaceAll = replaceAllValues(workspacePath) ++
scalaTransitiveSubstitutions ++
kotlinTransitiveSubstitutions ++
Seq(
scalaVersion -> "<scala-version>",
kotlinVersion -> "<kotlin-version>"
)

compareWithGsonFixture(
initRes,
53 changes: 51 additions & 2 deletions kotlinlib/src/mill/kotlinlib/KotlinModule.scala
Original file line number Diff line number Diff line change
@@ -5,11 +5,12 @@
package mill
package kotlinlib

import mill.api.{Loose, PathRef, Result}
import mill.api.{Loose, PathRef, Result, internal}
import mill.define.{Command, ModuleRef, Task}
import mill.kotlinlib.worker.api.KotlinWorker
import mill.scalalib.api.{CompilationResult, ZincWorkerApi}
import mill.scalalib.{JavaModule, Lib, ZincWorkerModule}
import mill.scalalib.bsp.{BspBuildTarget, BspModule}
import mill.scalalib.{JavaModule, Lib, ScalaModule, ZincWorkerModule}
import mill.util.Jvm
import mill.util.Util.millProjectModule
import mill.{Agg, T}
@@ -326,6 +327,54 @@ trait KotlinModule extends JavaModule { outer =>

private[kotlinlib] def internalReportOldProblems: Task[Boolean] = zincReportCachedProblems

@internal
override def bspBuildTarget: BspBuildTarget = super.bspBuildTarget.copy(
// Mark the module as a Scala one, so that intellij-scala handles it (and deals fine with Kotlin code)
languageIds =
Seq(BspModule.LanguageId.Java, BspModule.LanguageId.Scala, BspModule.LanguageId.Kotlin),
canCompile = true,
canRun = true
)

/**
* Classpath of the Scala Compiler, to be passed to IntelliJ
*/
def kotlinIntellijScalaCompilerClasspath: T[Agg[PathRef]] =
this match {
case scalaMod: ScalaModule => Task { scalaMod.scalaCompilerClasspath() }
case _ =>
Task {
defaultResolver().resolveDeps(
mill.scalalib.Lib.scalaCompilerIvyDeps(
"org.scala-lang",
scala.util.Properties.versionNumberString
) ++
mill.scalalib.Lib.scalaRuntimeIvyDeps(
"org.scala-lang",
scala.util.Properties.versionNumberString
)
)
}
}

@internal
override def bspBuildTargetData: Task[Option[(String, AnyRef)]] =
this match {
case scalaMod: ScalaModule =>
ScalaModule.bspBuildTargetData(scalaMod)
case _ =>
Task.Anon {
Some(
ScalaModule.bspBuildTargetData(
"org.scala-lang",
scala.util.Properties.versionNumberString,
kotlinIntellijScalaCompilerClasspath(),
jvmBuildTarget
)
)
}
}

/**
* A test sub-module linked to its parent module best suited for unit-tests.
*/
Loading

0 comments on commit 01bba7f

Please sign in to comment.