Skip to content

Commit

Permalink
Add default output directory scalac in dotty project (#19226)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
nicolasstucki authored Dec 14, 2023
2 parents 037c8fa + fca00c9 commit ce6ada2
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions project/Build.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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")

Expand All @@ -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")
Expand All @@ -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,
Expand All @@ -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 =
Expand Down Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion project/scripts/cmdTests
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down

0 comments on commit ce6ada2

Please sign in to comment.