mirror of https://github.com/sbt/sbt.git
[2.x] fix: Fixes autoScalaLibrary in test classloader (#8776)
When Test / autoScalaLibrary := false, build the non-fork test classloader without the ScalaInstance layer: use Flat strategy and project classpath only (rawCP), so tests do not see sbt's scala-library and avoid NoSuchMethodError / version mismatch (e.g. scala/scala build). - ClassLoaders.testTask: read autoScalaLibrary; when false use Flat and rawCP-only fullCP, and do not add si.libraryJars to exclude. - Add scripted test tests/autoScalaLibrary-test-loader that runs test with Test / autoScalaLibrary := false.
This commit is contained in:
parent
ae1066ed12
commit
861fbccaea
|
|
@ -38,6 +38,7 @@ private[sbt] object ClassLoaders {
|
||||||
*/
|
*/
|
||||||
private[sbt] def testTask: Def.Initialize[Task[ClassLoader]] = Def.task {
|
private[sbt] def testTask: Def.Initialize[Task[ClassLoader]] = Def.task {
|
||||||
val si = scalaInstance.value
|
val si = scalaInstance.value
|
||||||
|
val useScalaLibraryLayer = autoScalaLibrary.value
|
||||||
val converter = fileConverter.value
|
val converter = fileConverter.value
|
||||||
val cp = fullClasspath.value
|
val cp = fullClasspath.value
|
||||||
.map(_.data)
|
.map(_.data)
|
||||||
|
|
@ -47,17 +48,21 @@ private[sbt] object ClassLoaders {
|
||||||
def getLm(f: File): Long = dependencyStamps.getOrElse(f, IO.getModifiedTimeOrZero(f))
|
def getLm(f: File): Long = dependencyStamps.getOrElse(f, IO.getModifiedTimeOrZero(f))
|
||||||
val rawCP = cp.map(f => f -> getLm(f))
|
val rawCP = cp.map(f => f -> getLm(f))
|
||||||
val fullCP =
|
val fullCP =
|
||||||
if si.isManagedVersion then rawCP
|
if !useScalaLibraryLayer then rawCP
|
||||||
|
else if si.isManagedVersion then rawCP
|
||||||
else si.libraryJars.map(j => j -> IO.getModifiedTimeOrZero(j)).toSeq ++ rawCP
|
else si.libraryJars.map(j => j -> IO.getModifiedTimeOrZero(j)).toSeq ++ rawCP
|
||||||
val exclude: Set[File] = dependencyJars(exportedProducts).value
|
val exclude: Set[File] = dependencyJars(exportedProducts).value
|
||||||
.map(converter.toPath)
|
.map(converter.toPath)
|
||||||
.map(_.toFile)
|
.map(_.toFile)
|
||||||
.toSet ++ si.libraryJars
|
.toSet ++ (if useScalaLibraryLayer then si.libraryJars.toSeq else Seq.empty)
|
||||||
|
val strategy =
|
||||||
|
if useScalaLibraryLayer then classLoaderLayeringStrategy.value
|
||||||
|
else ClassLoaderLayeringStrategy.Flat
|
||||||
val logger = state.value.globalLogging.full
|
val logger = state.value.globalLogging.full
|
||||||
val close = closeClassLoaders.value
|
val close = closeClassLoaders.value
|
||||||
val allowZombies = allowZombieClassLoaders.value
|
val allowZombies = allowZombieClassLoaders.value
|
||||||
buildLayers(
|
buildLayers(
|
||||||
strategy = classLoaderLayeringStrategy.value,
|
strategy = strategy,
|
||||||
si = si,
|
si = si,
|
||||||
fullCP = fullCP,
|
fullCP = fullCP,
|
||||||
allDependenciesSet = dependencyJars(dependencyClasspath).value
|
allDependenciesSet = dependencyJars(dependencyClasspath).value
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,5 @@
|
||||||
|
ThisBuild / scalaVersion := "2.12.21"
|
||||||
|
|
||||||
|
Test / autoScalaLibrary := false
|
||||||
|
libraryDependencies += "org.scala-lang" % "scala-library" % scalaVersion.value % Test
|
||||||
|
libraryDependencies += "org.scalatest" %% "scalatest" % "3.2.19" % Test
|
||||||
|
|
@ -0,0 +1,8 @@
|
||||||
|
import org.scalatest.flatspec.AnyFlatSpec
|
||||||
|
import org.scalatest.matchers.should.Matchers
|
||||||
|
|
||||||
|
class ExampleSpec extends AnyFlatSpec with Matchers {
|
||||||
|
"test with autoScalaLibrary false" should "use project classpath only" in {
|
||||||
|
1 shouldBe 1
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
> test
|
||||||
Loading…
Reference in New Issue