Skip to content

Commit

Permalink
Separate REPL classpath from user dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
Gedochao committed Dec 5, 2023
1 parent d98be3b commit 531e3f5
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 27 deletions.
47 changes: 27 additions & 20 deletions modules/build/src/main/scala/scala/build/ReplArtifacts.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,17 @@ import scala.build.internal.CsLoggerUtil._

final case class ReplArtifacts(
replArtifacts: Seq[(String, os.Path)],
depArtifacts: Seq[(String, os.Path)],
extraClassPath: Seq[os.Path],
extraSourceJars: Seq[os.Path],
replMainClass: String,
replJavaOpts: Seq[String],
addSourceJars: Boolean
) {
lazy val replClassPath: Seq[os.Path] = {
val cp =
if (addSourceJars)
extraClassPath ++ extraSourceJars ++ replArtifacts.map(_._2)
else
extraClassPath ++ replArtifacts.map(_._2)
cp.distinct
}
lazy val replClassPath: Seq[os.Path] = replArtifacts.map(_._2).distinct
lazy val depsClassPath: Seq[os.Path] =
(if addSourceJars then extraClassPath ++ extraSourceJars ++ depArtifacts.map(_._2)
else extraClassPath ++ depArtifacts.map(_._2)).distinct
}

object ReplArtifacts {
Expand Down Expand Up @@ -68,11 +65,13 @@ object ReplArtifacts {
classifiersOpt = Some(Set("sources"))
)
ReplArtifacts(
value(replArtifacts) ++ value(replSourceArtifacts),
extraClassPath,
extraSourceJars,
"ammonite.Main",
Nil,
replArtifacts = value(replArtifacts) ++ value(replSourceArtifacts),
depArtifacts =
Nil, // amm does not support a -cp option, deps are passed directly to Ammonite cp
extraClassPath = extraClassPath,
extraSourceJars = extraSourceJars,
replMainClass = "ammonite.Main",
replJavaOpts = Nil,
addSourceJars = true
)
}
Expand All @@ -92,24 +91,32 @@ object ReplArtifacts {
else dep"org.scala-lang::scala3-compiler:${scalaParams.scalaVersion}"
val scalapyDeps =
addScalapy.map(ver => dep"${Artifacts.scalaPyOrganization(ver)}::scalapy-core::$ver").toSeq
val allDeps = dependencies ++ Seq(replDep) ++ scalapyDeps
val externalDeps = dependencies ++ scalapyDeps
val replArtifacts =
Artifacts.artifacts(
allDeps.map(Positioned.none),
Seq(replDep).map(Positioned.none),
repositories,
Some(scalaParams),
logger,
cache.withMessage(s"Downloading Scala compiler ${scalaParams.scalaVersion}")
)
val depArtifacts = Artifacts.artifacts(
externalDeps.map(Positioned.none),
repositories,
Some(scalaParams),
logger,
cache.withMessage(s"Downloading REPL dependencies")
)
val mainClass =
if (isScala2) "scala.tools.nsc.MainGenericRunner"
else "dotty.tools.repl.Main"
ReplArtifacts(
value(replArtifacts),
extraClassPath,
Nil,
mainClass,
Seq("-Dscala.usejavacp=true"),
replArtifacts = value(replArtifacts),
depArtifacts = value(depArtifacts),
extraClassPath = extraClassPath,
extraSourceJars = Nil,
replMainClass = mainClass,
replJavaOpts = Seq("-Dscala.usejavacp=true"),
addSourceJars = false
)
}
Expand Down
22 changes: 15 additions & 7 deletions modules/cli/src/main/scala/scala/cli/commands/repl/Repl.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import coursier.cache.FileCache
import coursier.error.{FetchError, ResolutionError}
import dependency.*

import java.io.File
import java.util.zip.ZipFile

import scala.build.EitherCps.{either, value}
Expand All @@ -29,7 +30,7 @@ import scala.cli.config.{ConfigDb, Keys}
import scala.cli.packaging.Library
import scala.cli.util.ArgHelpers.*
import scala.cli.util.ConfigDbUtils
import scala.jdk.CollectionConverters._
import scala.jdk.CollectionConverters.*
import scala.util.Properties

object Repl extends ScalaCommand[ReplOptions] {
Expand Down Expand Up @@ -380,16 +381,23 @@ object Repl extends ScalaCommand[ReplOptions] {
if (dryRun)
logger.message("Dry run, not running REPL.")
else {
val depClassPathArgs: Seq[String] =
if replArtifacts.depsClassPath.nonEmpty then
Seq(
"-classpath",
replArtifacts.depsClassPath.map(_.toString).mkString(File.pathSeparator)
)
else Nil
val retCode = Runner.runJvm(
options.javaHome().value.javaCommand,
scalapyJavaOpts ++
javaCommand = options.javaHome().value.javaCommand,
javaArgs = scalapyJavaOpts ++
replArtifacts.replJavaOpts ++
options.javaOptions.javaOpts.toSeq.map(_.value.value) ++
extraProps.toVector.sorted.map { case (k, v) => s"-D$k=$v" },
mainJarOrClassDir.toSeq ++ replArtifacts.replClassPath,
replArtifacts.replMainClass,
maybeAdaptForWindows(replArgs),
logger,
classPath = mainJarOrClassDir.toSeq ++ replArtifacts.replClassPath,
mainClass = replArtifacts.replMainClass,
args = maybeAdaptForWindows(depClassPathArgs ++ replArgs),
logger = logger,
allowExecve = allowExit,
extraEnv = scalapyExtraEnv ++ extraEnv
).waitFor()
Expand Down

0 comments on commit 531e3f5

Please sign in to comment.