diff --git a/compiler/src/dotty/tools/MainGenericRunner.scala b/compiler/src/dotty/tools/MainGenericRunner.scala index b7ef90dcdc60..151c7d7b8faf 100644 --- a/compiler/src/dotty/tools/MainGenericRunner.scala +++ b/compiler/src/dotty/tools/MainGenericRunner.scala @@ -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) @@ -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) diff --git a/tests/run-with-compiler/i14541.check b/tests/run-with-compiler/i14541.check new file mode 100644 index 000000000000..38b710956004 --- /dev/null +++ b/tests/run-with-compiler/i14541.check @@ -0,0 +1,2 @@ +hello raw world +hello run world diff --git a/tests/run-with-compiler/i14541.scala b/tests/run-with-compiler/i14541.scala new file mode 100644 index 000000000000..0fdfb89674d5 --- /dev/null +++ b/tests/run-with-compiler/i14541.scala @@ -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(" ") +}