Skip to content

Commit ce6ada2

Browse files
Add default output directory scalac in dotty project (#19226)
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.
2 parents 037c8fa + fca00c9 commit ce6ada2

File tree

2 files changed

+10
-4
lines changed

2 files changed

+10
-4
lines changed

project/Build.scala

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,6 +712,10 @@ object Build {
712712
val externalDeps = externalCompilerClasspathTask.value
713713
val jars = packageAll.value
714714

715+
val argsWithDefaultClasspath: List[String] =
716+
if (args.contains("-classpath")) args
717+
else insertClasspathInArgs(args, (baseDirectory.value / ".." / "out" / "default-last-scalac-out.jar").getPath)
718+
715719
val scalaLib = findArtifactPath(externalDeps, "scala-library")
716720
val dottyLib = jars("scala3-library")
717721

@@ -725,7 +729,7 @@ object Build {
725729
} else if (scalaLib == "") {
726730
println("Couldn't find scala-library on classpath, please run using script in bin dir instead")
727731
} else if (args.contains("-with-compiler")) {
728-
val args1 = args.filter(_ != "-with-compiler")
732+
val args1 = argsWithDefaultClasspath.filter(_ != "-with-compiler")
729733
val asm = findArtifactPath(externalDeps, "scala-asm")
730734
val dottyCompiler = jars("scala3-compiler")
731735
val dottyStaging = jars("scala3-staging")
@@ -734,7 +738,7 @@ object Build {
734738
val tastyCore = jars("tasty-core")
735739
val compilerInterface = findArtifactPath(externalDeps, "compiler-interface")
736740
run(insertClasspathInArgs(args1, List(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface).mkString(File.pathSeparator)))
737-
} else run(args)
741+
} else run(argsWithDefaultClasspath)
738742
},
739743

740744
run := scalac.evaluated,
@@ -750,6 +754,8 @@ object Build {
750754
val decompile = args0.contains("-decompile")
751755
val printTasty = args0.contains("-print-tasty")
752756
val debugFromTasty = args0.contains("-Ythrough-tasty")
757+
val defaultOutputDirectory =
758+
if (printTasty || decompile || debugFromTasty || args0.contains("-d")) Nil else List("-d", (baseDirectory.value / ".." / "out" / "default-last-scalac-out.jar").getPath)
753759
val args = args0.filter(arg => arg != "-repl" && arg != "-decompile" &&
754760
arg != "-with-compiler" && arg != "-Ythrough-tasty" && arg != "-print-tasty")
755761
val main =
@@ -783,7 +789,7 @@ object Build {
783789
extraClasspath ++= Seq(dottyCompiler, dottyInterfaces, asm, dottyStaging, dottyTastyInspector, tastyCore, compilerInterface)
784790
}
785791

786-
val fullArgs = main :: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator)))
792+
val fullArgs = main :: defaultOutputDirectory ::: (if (printTasty) args else insertClasspathInArgs(args, extraClasspath.mkString(File.pathSeparator)))
787793

788794
(Compile / runMain).toTask(fullArgs.mkString(" ", " ", ""))
789795
}.evaluated,

project/scripts/cmdTests

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ grep -qe "$EXPECTED_OUTPUT" "$tmp"
2525

2626
echo "testing sbt scalac -print-tasty"
2727
clear_out "$OUT"
28-
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $TASTY" > "$tmp"
28+
"$SBT" ";scalac $SOURCE -d $OUT ;scalac -print-tasty -color:never $OUT/$TASTY" > "$tmp"
2929
grep -qe "0: ASTs" "$tmp"
3030
grep -qe "0: 41 \[tests/pos/HelloWorld.scala\]" "$tmp"
3131

0 commit comments

Comments
 (0)