Skip to content

Commit

Permalink
Merge pull request #14552 from som-snytt/backport/14541
Browse files Browse the repository at this point in the history
[backport] Runner sets residual args instead of append
  • Loading branch information
smarter committed Mar 7, 2022
2 parents f80848f + b4a7c85 commit d1cfa7b
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 4 deletions.
9 changes: 5 additions & 4 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ object MainGenericRunner {
else
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
process(tail, newSettings.withResidualArgs(arg))

end process

def main(args: Array[String]): Unit =
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
Expand Down Expand Up @@ -200,11 +200,12 @@ 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 d1cfa7b

Please sign in to comment.