Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use scala library from classpath on 2.13 #1271

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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!
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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("3.") || 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;
Expand All @@ -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("3.") || 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
Expand All @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ final class CompilerArguments(
if (compilerClasspath.isEmpty) dummy
else absString(compilerClasspath)
val classpathOption = Seq("-classpath", stringClasspath)
val bootClasspath = bootClasspathOption(hasLibrary(classpath))
val bootClasspath = bootClasspathOption(hasLibrary(classpath), classpath)
options ++ bootClasspath ++ classpathOption ++ abs(sources)
}

Expand All @@ -95,13 +95,13 @@ final class CompilerArguments(
}

def createBootClasspathFor(classpath: Seq[Path]): String =
createBootClasspath(hasLibrary(classpath) || cpOptions.compiler || cpOptions.extra)
createBootClasspath(hasLibrary(classpath) || cpOptions.compiler || cpOptions.extra, classpath)

/**
* Return the Scala library to the boot classpath if `addLibrary` is true.
* @param addLibrary Flag to return the Scala library.
*/
def createBootClasspath(addLibrary: Boolean): String = {
def createBootClasspath(addLibrary: Boolean, classpath: Seq[Path]): String = {
def findBoot: String = {
import scala.collection.JavaConverters._
System.getProperties.asScala.iterator
Expand All @@ -115,7 +115,14 @@ final class CompilerArguments(
val newBootPrefix =
if (originalBoot.isEmpty) ""
else originalBoot + java.io.File.pathSeparator
newBootPrefix + absString(scalaInstance.libraryJars.map(_.toPath))
val useInstanceLibrary = {
val v = scalaInstance.version
v.startsWith("3.") || v.startsWith("2.13.")
}
val library: String =
if (useInstanceLibrary) absString(scalaInstance.libraryJars.map(_.toPath))
else absString(classpath.filter(isScalaLibrary))
newBootPrefix + library
} else originalBoot
}

Expand All @@ -125,16 +132,16 @@ final class CompilerArguments(

def hasLibrary(classpath: Seq[Path]) = classpath.exists(isScalaLibrary)

def bootClasspathOption(addLibrary: Boolean): Seq[String] =
def bootClasspathOption(addLibrary: Boolean, classpath: Seq[Path]): Seq[String] =
if (!cpOptions.autoBoot) Nil
else Seq(BootClasspathOption, createBootClasspath(addLibrary))
else Seq(BootClasspathOption, createBootClasspath(addLibrary, classpath))

def bootClasspath(addLibrary: Boolean): Seq[Path] =
def bootClasspath(addLibrary: Boolean, classpath: Seq[Path]): Seq[Path] =
if (!cpOptions.autoBoot) Nil
else IO.parseClasspath(createBootClasspath(addLibrary)).map(_.toPath)
else IO.parseClasspath(createBootClasspath(addLibrary, classpath)).map(_.toPath)

def bootClasspathFor(classpath: Seq[Path]) =
bootClasspath(hasLibrary(classpath))
bootClasspath(hasLibrary(classpath), classpath)

def extClasspath: Seq[Path] =
List("java.ext.dirs", "scala.ext.dirs").flatMap(k =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,15 +196,15 @@ 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)
}

private final val unit = (_: Seq[String]) => ()

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)
}

Expand Down
12 changes: 10 additions & 2 deletions zinc/src/main/scala/sbt/internal/inc/ZincUtil.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}

/**
Expand All @@ -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(
Expand Down