Skip to content

Commit

Permalink
Resolve Mill build source dependencies (#946)
Browse files Browse the repository at this point in the history
Pull request: #946
  • Loading branch information
joan38 authored Aug 21, 2020
1 parent 5e164d3 commit d359a96
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 20 deletions.
3 changes: 1 addition & 2 deletions contrib/bsp/src/mill/contrib/BSP.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package mill.contrib

import ammonite.runtime.SpecialClassLoader
import java.io.PrintWriter
import java.nio.file.FileAlreadyExistsException
import java.util.concurrent.Executors
Expand All @@ -22,7 +21,7 @@ case class BspConfigJson(
millVersion: String,
bspVersion: String,
languages: Seq[String]
) extends BspConnectionDetails(name, argv.asJava, millVersion, bspVersion, languages.asJava) {}
) extends BspConnectionDetails(name, argv.asJava, millVersion, bspVersion, languages.asJava)

object BspConfigJson {
implicit val rw: ReadWriter[BspConfigJson] = macroRW
Expand Down
42 changes: 24 additions & 18 deletions contrib/bsp/src/mill/contrib/bsp/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -132,24 +132,28 @@ class MillBuildServer(evaluator: Evaluator, bspVersion: String, serverVersion: S
val millBuildTargetId = getMillBuildTargetId(evaluator)

val items = dependencySourcesParams.getTargets.asScala
.filter(_ != millBuildTargetId)
.foldLeft(Seq.empty[DependencySourcesItem]) { (items, targetId) =>
val module = getModule(targetId, modules)
val sources = evaluateInformativeTask(
evaluator,
module.resolveDeps(T.task(module.compileIvyDeps() ++ module.transitiveIvyDeps()), sources = true),
Agg.empty[PathRef]
)
val unmanaged = evaluateInformativeTask(
evaluator,
module.unmanagedClasspath,
Agg.empty[PathRef]
)
val all = if (targetId == millBuildTargetId) {
Try(getClass.getClassLoader.asInstanceOf[SpecialClassLoader]).fold(
_ => Seq.empty,
_.allJars.filter(url => isSourceJar(url) && exists(Path(url.getFile))).map(_.toURI.toString)
)
} else {
val module = getModule(targetId, modules)
val sources = evaluateInformativeTask(
evaluator,
module.resolveDeps(T.task(module.compileIvyDeps() ++ module.transitiveIvyDeps()), sources = true),
Agg.empty[PathRef]
)
val unmanaged = evaluateInformativeTask(
evaluator,
module.unmanagedClasspath,
Agg.empty[PathRef]
)

items :+ new DependencySourcesItem(
targetId,
(sources ++ unmanaged).map(_.path.toIO.toURI.toString).iterator.toSeq.asJava
)
(sources ++ unmanaged).map(_.path.toIO.toURI.toString).iterator.toSeq
}
items :+ new DependencySourcesItem(targetId, all.asJava)
}

new DependencySourcesResult(items.asJava)
Expand Down Expand Up @@ -371,8 +375,10 @@ class MillBuildServer(evaluator: Evaluator, bspVersion: String, serverVersion: S
.foldLeft(Seq.empty[ScalacOptionsItem]) { (items, targetId) =>
val newItem =
if (targetId == millBuildTargetId) {
val classpath = Try(getClass.getClassLoader.asInstanceOf[SpecialClassLoader])
.fold(_ => Seq.empty, _.allJars.filter(url => exists(Path(url.getFile))).map(_.toURI.toString))
val classpath = Try(getClass.getClassLoader.asInstanceOf[SpecialClassLoader]).fold(
_ => Seq.empty,
_.allJars.filter(url => !isSourceJar(url) && exists(Path(url.getFile))).map(_.toURI.toString)
)
Some(new ScalacOptionsItem(
targetId,
Seq.empty.asJava,
Expand Down
3 changes: 3 additions & 0 deletions contrib/bsp/src/mill/contrib/bsp/ModuleUtils.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mill.contrib.bsp

import ch.epfl.scala.bsp4j._
import java.net.URL
import mill._
import mill.api.Result.Success
import mill.api.{PathRef, Strict}
Expand Down Expand Up @@ -182,6 +183,8 @@ object ModuleUtils {
def getTargetId(moduleHashCode: Int, modules: Seq[JavaModule]): Option[BuildTargetIdentifier] =
modules.find(_.hashCode == moduleHashCode).map(getTargetId)

def isSourceJar(url: URL): Boolean = url.getFile.endsWith("-sources.jar")

// Compute the ScalaBuildTarget from information about the given JavaModule.
private[this] def computeBuildTargetData(module: JavaModule, evaluator: Evaluator): ScalaBuildTarget = {
module match {
Expand Down

0 comments on commit d359a96

Please sign in to comment.