Skip to content

Commit

Permalink
Improve mill build classpath
Browse files Browse the repository at this point in the history
  • Loading branch information
joan38 committed Oct 21, 2020
1 parent bc5c203 commit d9161ce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 10 deletions.
14 changes: 4 additions & 10 deletions bsp/src/mill/bsp/MillBuildServer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -133,12 +133,9 @@ class MillBuildServer(evaluator: Evaluator, bspVersion: String, serverVersion: S

val items = dependencySourcesParams.getTargets.asScala
.foldLeft(Seq.empty[DependencySourcesItem]) { (items, targetId) =>
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 all = if (targetId == millBuildTargetId)
getMillBuildClasspath(evaluator, source = true)
else {
val module = getModule(targetId, modules)
val sources = evaluateInformativeTask(
evaluator,
Expand Down Expand Up @@ -375,10 +372,7 @@ 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 => !isSourceJar(url) && exists(Path(url.getFile))).map(_.toURI.toString)
)
val classpath = getMillBuildClasspath(evaluator, source = false)
Some(new ScalacOptionsItem(
targetId,
Seq.empty.asJava,
Expand Down
15 changes: 15 additions & 0 deletions bsp/src/mill/bsp/ModuleUtils.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package mill.bsp

import ammonite.runtime.SpecialClassLoader
import ch.epfl.scala.bsp4j._
import java.net.URL
import java.net.URLClassLoader
import mill._
import mill.api.Result.Success
import mill.api.{PathRef, Strict}
Expand All @@ -13,7 +15,9 @@ import mill.scalalib.api.Util
import mill.scalalib.{JavaModule, ScalaModule, TestModule}
import mill.scalanativelib._
import mill.util.Ctx
import os.{Path, exists}
import scala.collection.JavaConverters._
import scala.util.Try

/**
* Utilities for translating the mill build into
Expand Down Expand Up @@ -101,6 +105,17 @@ object ModuleUtils {
target
}

def getMillBuildClasspath(evaluator: Evaluator, source: Boolean): Seq[String] = {
val all = Try(evaluator.rootModule.getClass.getClassLoader.asInstanceOf[SpecialClassLoader]).fold(
_ => Seq.empty,
_.allJars
)
val filtered =
if (source) all.filter(url => isSourceJar(url))
else all.filter(url => !isSourceJar(url))
filtered.filter(url => exists(Path(url.getFile))).map(_.toURI.toString)
}

/**
* Compute the BuildTarget associated with the given mill
* JavaModule, which is any module present in the working
Expand Down

0 comments on commit d9161ce

Please sign in to comment.