[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:
bitloi 2026-02-21 14:59:46 -05:00 committed by GitHub
parent ae1066ed12
commit 861fbccaea
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 22 additions and 3 deletions

View File

@ -38,6 +38,7 @@ private[sbt] object ClassLoaders {
*/
private[sbt] def testTask: Def.Initialize[Task[ClassLoader]] = Def.task {
val si = scalaInstance.value
val useScalaLibraryLayer = autoScalaLibrary.value
val converter = fileConverter.value
val cp = fullClasspath.value
.map(_.data)
@ -47,17 +48,21 @@ private[sbt] object ClassLoaders {
def getLm(f: File): Long = dependencyStamps.getOrElse(f, IO.getModifiedTimeOrZero(f))
val rawCP = cp.map(f => f -> getLm(f))
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
val exclude: Set[File] = dependencyJars(exportedProducts).value
.map(converter.toPath)
.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 close = closeClassLoaders.value
val allowZombies = allowZombieClassLoaders.value
buildLayers(
strategy = classLoaderLayeringStrategy.value,
strategy = strategy,
si = si,
fullCP = fullCP,
allDependenciesSet = dependencyJars(dependencyClasspath).value

View File

@ -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

View File

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

View File

@ -0,0 +1 @@
> test