Skip to content

Commit

Permalink
Runner sets residual args instead of append
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Feb 28, 2022
1 parent 5109909 commit 2cc3070
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
15 changes: 9 additions & 6 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ object MainGenericRunner {
.withTargetScript(arg)
.withScriptArgs(tail*)
else
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
process(tail, newSettings.withResidualArgs(arg))

val modalSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
val newSettings = modalSettings.withResidualArgs(arg)
process(tail, newSettings)
end process

def main(args: Array[String]): Unit =
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
Expand Down Expand Up @@ -199,11 +200,13 @@ object MainGenericRunner {
Option.when(Jar.isJarOrZip(dotty.tools.io.Path(entryPath)))(Jar(entryPath).mainClass).flatten
}.isDefined
}
targetToRun match
val newSettings =
targetToRun match
case Some(fqName) =>
run(settings.withTargetToRun(fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }*).withExecuteMode(ExecuteMode.Run))
settings.withTargetToRun(fqName).copy(residualArgs = settings.residualArgs.filterNot(fqName.==)).withExecuteMode(ExecuteMode.Run)
case None =>
run(settings.withExecuteMode(ExecuteMode.Repl))
settings.withExecuteMode(ExecuteMode.Repl)
run(newSettings)
case ExecuteMode.Run =>
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
Expand Down
2 changes: 2 additions & 0 deletions tests/run-with-compiler/i14541.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
hello raw world
hello run world
13 changes: 13 additions & 0 deletions tests/run-with-compiler/i14541.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

// test argument processing and "execution mode" in runner
object Test:
import dotty.tools.runner.RichClassLoader.*
val classpath = dotty.tools.dotc.util.ClasspathFromClassloader(getClass.getClassLoader)
def main(args: Array[String]): Unit =
getClass.getClassLoader.run("echo", List("hello", "raw", "world"))
// caution: uses "SCALA_OPTS"
dotty.tools.MainGenericRunner.main(Array("--class-path", classpath, "echo", "hello", "run", "world"))

@main def echo(args: String*): Unit = println {
args.mkString(" ")
}

0 comments on commit 2cc3070

Please sign in to comment.