From fca00c994540d443318e1ea22a5d8fc95e9db8c0 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 8 Dec 2023 13:54:33 +0100 Subject: [PATCH] Add default output directory scalac in dotty project The previous default output directory when executing `scalac`, `run`, `scala3-bootstrapped/scalac` and `scala3-bootstrapped/run` was the root directory of the project. This could easily pollute the project with `.class`/`.tasty` files. In some situations, these could be mistakenly loaded by some of the projects or scripts. Now when we execute `scalac` with no `-d` option set, we output `./out/default-last-scalac-out.jar`. We also make `scala` and `run` use that JAR by default in the classpath if no `-classpath` option is set. --- project/Build.scala | 12 +++++++++--- project/scripts/cmdTests | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/project/Build.scala b/project/Build.scala index 89dc6051ecc4..606d992a2561 100644 --- a/project/Build.scala +++ b/project/Build.scala @@ -712,6 +712,10 @@ object Build { val externalDeps = externalCompilerClasspathTask.value val jars = packageAll.value + val argsWithDefaultClasspath: List[String] = + if (args.contains("-classpath")) args + else insertClasspathInArgs(args, (baseDirectory.value / ".." / "out" / "default-last-scalac-out.jar").getPath) + val scalaLib = findArtifactPath(externalDeps, "scala-library") val dottyLib = jars("scala3-library") @@ -725,7 +729,7 @@ object Build { } else if (scalaLib == "") { println("Couldn't find scala-library on classpath, please run using script in bin dir instead") } else if (args.contains("-with-compiler")) { - val args1 = args.filter(_ != "-with-compiler") + val args1 = argsWithDefaultClasspath.filter(_ != "-with-compiler") val asm = findArtifactPath(externalDeps, "scala-asm") val dottyCompiler = jars("scala3-compiler") val dottyStaging = jars("scala3-staging") @@ -734,7 +738,7 @@ object Build { val tastyCore = jars("tasty-core") val compilerInterface = findArtifactPath(externalDeps, "compiler-interface") run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface).mkString(File.pathSeparator))) - } else run(args) + } else run(argsWithDefaultClasspath) }, run := scalac.evaluated, @@ -750,6 +754,8 @@ object Build { val decompile = args0.contains("-decompile") val printTasty = args0.contains("-print-tasty") val debugFromTasty = args0.contains("-Ythrough-tasty") + val defaultOutputDirectory = + if (printTasty || decompile || debugFromTasty || args0.contains("-d")) Nil else List("-d", (baseDirectory.value / ".." / "out" / "default-last-scalac-out.jar").getPath) val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" && arg != "-with-compiler" && arg != "-Ythrough-tasty" && arg != "-print-tasty") val main = @@ -783,7 +789,7 @@ object Build { extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface) } - val fullArgs = main :: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator))) + val fullArgs = main :: defaultOutputDirectory ::: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator))) (Compile / runMain).toTask(fullArgs.mkString(" ", " ", "")) }.evaluated, diff --git a/project/scripts/cmdTests b/project/scripts/cmdTests index 6208237c324d..453590084b00 100755 --- a/project/scripts/cmdTests +++ b/project/scripts/cmdTests @@ -25,7 +25,7 @@ grep -qe "$EXPECTED_OUTPUT" "$tmp" echo "testing sbt scalac -print-tasty" clear_out "$OUT" -"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp" +"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $OUT/$TASTY" > "$tmp" grep -qe "0: ASTs" "$tmp" grep -qe "0: 41 \[tests/pos/HelloWorld.scala\]" "$tmp"