Skip to content

Commit d1cfa7b

Browse files
authored
Merge pull request #14552 from som-snytt/backport/14541
[backport] Runner sets residual args instead of append
2 parents f80848f + b4a7c85 commit d1cfa7b

File tree

3 files changed

+20
-4
lines changed

3 files changed

+20
-4
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

+5-4
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ object MainGenericRunner {
169169
else
170170
val newSettings = if arg.startsWith("-") then settings else settings.withPossibleEntryPaths(arg).withModeShouldBePossibleRun
171171
process(tail, newSettings.withResidualArgs(arg))
172-
172+
end process
173173

174174
def main(args: Array[String]): Unit =
175175
val scalaOpts = envOrNone("SCALA_OPTS").toArray.flatMap(_.split(" ")).filter(_.nonEmpty)
@@ -200,11 +200,12 @@ object MainGenericRunner {
200200
Option.when(Jar.isJarOrZip(dotty.tools.io.Path(entryPath)))(Jar(entryPath).mainClass).flatten
201201
}.isDefined
202202
}
203-
targetToRun match
203+
val newSettings = targetToRun match
204204
case Some(fqName) =>
205-
run(settings.withTargetToRun(fqName).withResidualArgs(settings.residualArgs.filter { _ != fqName }*).withExecuteMode(ExecuteMode.Run))
205+
settings.withTargetToRun(fqName).copy(residualArgs = settings.residualArgs.filterNot(fqName.==)).withExecuteMode(ExecuteMode.Run)
206206
case None =>
207-
run(settings.withExecuteMode(ExecuteMode.Repl))
207+
settings.withExecuteMode(ExecuteMode.Repl)
208+
run(newSettings)
208209
case ExecuteMode.Run =>
209210
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
210211
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)

tests/run-with-compiler/i14541.check

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
hello raw world
2+
hello run world

tests/run-with-compiler/i14541.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
// test argument processing and "execution mode" in runner
3+
object Test:
4+
import dotty.tools.runner.RichClassLoader.*
5+
val classpath = dotty.tools.dotc.util.ClasspathFromClassloader(getClass.getClassLoader)
6+
def main(args: Array[String]): Unit =
7+
getClass.getClassLoader.run("echo", List("hello", "raw", "world"))
8+
// caution: uses "SCALA_OPTS"
9+
dotty.tools.MainGenericRunner.main(Array("--class-path", classpath, "echo", "hello", "run", "world"))
10+
11+
@main def echo(args: String*): Unit = println {
12+
args.mkString(" ")
13+
}

0 commit comments

Comments
 (0)