From 8e4186a55dbe63df57e72cc37a1e8e92aa3b4bcd Mon Sep 17 00:00:00 2001 From: Lukas Rytz Date: Tue, 24 Oct 2023 11:57:59 +0200 Subject: [PATCH] Don't use autoBoot / filterLibrary for 2.13 and 3 Passing `-bootclasspath /path/to/scala-library.jar` to the Scala compiler is only done for historical reasons. Putting the Scala library on the ordinary classspath is equivalent. This commit adds a `noboot` factory for ClasspathOptions which doesn't use `-bootclasspath` for Scala 2.13 and Scala 3. --- .../internal/inc/CompilingSpecification.scala | 2 +- .../xsbti/compile/ClasspathOptions.java | 6 ++-- .../src/main/contraband/incremental.contra | 6 ++-- .../xsbti/compile/ClasspathOptionsUtil.java | 34 ++++++++++++++++++- .../sbt/internal/inc/javac/JavaCompiler.scala | 1 + .../scala/sbt/internal/inc/IncHandler.scala | 4 +-- .../scala/sbt/internal/inc/ZincUtil.scala | 12 +++++-- 7 files changed, 53 insertions(+), 12 deletions(-) diff --git a/internal/compiler-bridge-test/src/test/scala/sbt/internal/inc/CompilingSpecification.scala b/internal/compiler-bridge-test/src/test/scala/sbt/internal/inc/CompilingSpecification.scala index fa420b77a5..6ae5315879 100644 --- a/internal/compiler-bridge-test/src/test/scala/sbt/internal/inc/CompilingSpecification.scala +++ b/internal/compiler-bridge-test/src/test/scala/sbt/internal/inc/CompilingSpecification.scala @@ -39,7 +39,7 @@ trait CompilingSpecification extends AbstractBridgeProviderTestkit { def scalaCompiler(instance: xsbti.compile.ScalaInstance, bridgeJar: Path): AnalyzingCompiler = { val bridgeProvider = ZincUtil.constantBridgeProvider(instance, bridgeJar) - val classpath = ClasspathOptionsUtil.boot + val classpath = ClasspathOptionsUtil.noboot(instance.version) val cache = Some(new ClassLoaderCache(new URLClassLoader(Array()))) new AnalyzingCompiler(instance, bridgeProvider, classpath, _ => (), cache) } diff --git a/internal/compiler-interface/src/main/contraband-java/xsbti/compile/ClasspathOptions.java b/internal/compiler-interface/src/main/contraband-java/xsbti/compile/ClasspathOptions.java index f7228fd26f..3fac455fc5 100644 --- a/internal/compiler-interface/src/main/contraband-java/xsbti/compile/ClasspathOptions.java +++ b/internal/compiler-interface/src/main/contraband-java/xsbti/compile/ClasspathOptions.java @@ -32,7 +32,7 @@ protected ClasspathOptions(boolean _bootLibrary, boolean _compiler, boolean _ext } /** * If true, includes the Scala library on the boot classpath. - * This should usually be true. + * This should usually be false. */ public boolean bootLibrary() { return this.bootLibrary; @@ -53,14 +53,14 @@ public boolean extra() { } /** * If true, automatically configures the boot classpath. - * This should usually be true. + * This should usually be false. */ public boolean autoBoot() { return this.autoBoot; } /** * If true, the Scala library jar is filtered from the standard classpath. - * This should usually be true because the library should be included on the boot + * This should usually be false unless the library is included on the boot * classpath of the Scala compiler and not the standard classpath. */ public boolean filterLibrary() { diff --git a/internal/compiler-interface/src/main/contraband/incremental.contra b/internal/compiler-interface/src/main/contraband/incremental.contra index 09842b2ede..a0617698ce 100644 --- a/internal/compiler-interface/src/main/contraband/incremental.contra +++ b/internal/compiler-interface/src/main/contraband/incremental.contra @@ -377,7 +377,7 @@ type Compilers { ## compiler-related parameters. Usually, values are all false for Java compilation. type ClasspathOptions { ## If true, includes the Scala library on the boot classpath. - ## This should usually be true. + ## This should usually be false. bootLibrary: Boolean! ## If true, includes the Scala compiler on the standard classpath. @@ -389,11 +389,11 @@ type ClasspathOptions { extra: Boolean! ## If true, automatically configures the boot classpath. - ## This should usually be true. + ## This should usually be false. autoBoot: Boolean! ## If true, the Scala library jar is filtered from the standard classpath. - ## This should usually be true because the library should be included on the boot + ## This should usually be false unless the library is included on the boot ## classpath of the Scala compiler and not the standard classpath. filterLibrary: Boolean! } diff --git a/internal/zinc-compile-core/src/main/java/xsbti/compile/ClasspathOptionsUtil.java b/internal/zinc-compile-core/src/main/java/xsbti/compile/ClasspathOptionsUtil.java index 1f65c17c5a..ec2ce09a49 100644 --- a/internal/zinc-compile-core/src/main/java/xsbti/compile/ClasspathOptionsUtil.java +++ b/internal/zinc-compile-core/src/main/java/xsbti/compile/ClasspathOptionsUtil.java @@ -16,7 +16,6 @@ * that create typical classpath options based on the desired use-case. */ public interface ClasspathOptionsUtil { - /** * Define a manual {@link ClasspathOptions} where the client manages everything. */ @@ -34,6 +33,16 @@ public static ClasspathOptions boot() { return ClasspathOptions.of(true, false, false, true, true); } + /** + * Define {@link ClasspathOptions} where the Scala standard library is present in the classpath. + */ + public static ClasspathOptions noboot(String scalaVersion) { + if (!scalaVersion.startsWith("2.") || scalaVersion.startsWith("2.13.")) + return ClasspathOptions.of(false, false, false, false, false); + else + return boot(); + } + /** * Define auto {@link ClasspathOptions} where: * 1. the Scala standard library is present in the classpath; @@ -46,6 +55,19 @@ public static ClasspathOptions auto() { return ClasspathOptions.of(true, true, true, true, true); } + /** + * Define auto {@link ClasspathOptions} where: + * 1. the Scala standard library is present in the classpath; + * 2. the Compiler JAR is present in the classpath; + * 3. the extra JARs present in the Scala instance are added to the classpath. + */ + public static ClasspathOptions autoNoboot(String scalaVersion) { + if (!scalaVersion.startsWith("2.") || scalaVersion.startsWith("2.13.")) + return ClasspathOptions.of(false, true, true, false, false); + else + return auto(); + } + /** * Define javac {@link ClasspathOptions} where the Compiler JAR may or may not * be present in the classpath. Note that the classpath won't be @@ -69,4 +91,14 @@ public static ClasspathOptions javac(Boolean compilerInClasspath) { public static ClasspathOptions repl() { return auto(); } + + /** + * Define repl {@link ClasspathOptions} where: + * 1. the Scala standard library is present in the classpath; + * 2. the Compiler JAR is present in the classpath; + * 3. the extra JARs present in the Scala instance are added to the classpath. + */ + public static ClasspathOptions replNoboot(String scalaVersion) { + return autoNoboot(scalaVersion); + } } diff --git a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/JavaCompiler.scala b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/JavaCompiler.scala index 610c36354c..2c357a00f2 100644 --- a/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/JavaCompiler.scala +++ b/internal/zinc-compile-core/src/main/scala/sbt/internal/inc/javac/JavaCompiler.scala @@ -93,6 +93,7 @@ object JavaCompiler { cpOptions: ClasspathOptions ): Seq[String] = { val cp = classpath.map(converter.toPath) + // this seems to duplicate scala-library val augmentedClasspath: Seq[Path] = if (!cpOptions.autoBoot) cp else cp ++ scalaInstance.libraryJars.map(_.toPath) diff --git a/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala b/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala index 29305fb7cb..b3c22d9691 100644 --- a/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala +++ b/internal/zinc-scripted/src/test/scala/sbt/internal/inc/IncHandler.scala @@ -196,7 +196,7 @@ class IncHandler(directory: Path, cacheDir: Path, scriptedLog: ManagedLogger, co toCache } val analyzingCompiler = scalaCompiler(si, compilerBridge) - val cs = incrementalCompiler.compilers(si, ClasspathOptionsUtil.boot, None, analyzingCompiler) + val cs = incrementalCompiler.compilers(si, ClasspathOptionsUtil.noboot(si.version), None, analyzingCompiler) IncState(si, cs, 0) } @@ -204,7 +204,7 @@ class IncHandler(directory: Path, cacheDir: Path, scriptedLog: ManagedLogger, co def scalaCompiler(instance: XScalaInstance, bridgeJar: Path): AnalyzingCompiler = { val bridgeProvider = ZincUtil.constantBridgeProvider(instance, bridgeJar) - val classpath = ClasspathOptionsUtil.boot + val classpath = ClasspathOptionsUtil.noboot(instance.version) new AnalyzingCompiler(instance, bridgeProvider, classpath, unit, IncHandler.classLoaderCache) } diff --git a/zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala b/zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala index d433085e00..4a3ddec802 100644 --- a/zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala +++ b/zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala @@ -82,7 +82,11 @@ object ZincUtil { * @return A Scala compiler ready to be used. */ def scalaCompiler(scalaInstance: ScalaInstance, compilerBridgeJar: Path): AnalyzingCompiler = { - scalaCompiler(scalaInstance, compilerBridgeJar, ClasspathOptionsUtil.boot) + scalaCompiler( + scalaInstance, + compilerBridgeJar, + ClasspathOptionsUtil.noboot(scalaInstance.version) + ) } /** @@ -98,7 +102,11 @@ object ZincUtil { * @return A Scala compiler ready to be used. */ def scalaCompiler(scalaInstance: ScalaInstance, compilerBridgeJar: File): AnalyzingCompiler = { - scalaCompiler(scalaInstance, compilerBridgeJar.toPath, ClasspathOptionsUtil.boot) + scalaCompiler( + scalaInstance, + compilerBridgeJar.toPath, + ClasspathOptionsUtil.noboot(scalaInstance.version) + ) } // def compilers(