Skip to content

[2.x]fix: Non-fork test classloader respects autoScalaLibrary#8776

Merged
eed3si9n merged 2 commits intosbt:developfrom
bitloi:fix/4009-autoScalaLibrary-test-classloader
Feb 21, 2026
Merged

[2.x]fix: Non-fork test classloader respects autoScalaLibrary#8776
eed3si9n merged 2 commits intosbt:developfrom
bitloi:fix/4009-autoScalaLibrary-test-classloader

Conversation

@bitloi
Copy link
Copy Markdown
Contributor

@bitloi bitloi commented Feb 21, 2026

Problem

With Test / autoScalaLibrary := false, the non-fork test classloader still included the ScalaInstance-based scala-library layer. Tests could load the wrong scala-library (e.g. sbt's) and hit NoSuchMethodError or version mismatch (e.g. scala/scala had to use fork in Test := true as a workaround).

Solution

In ClassLoaders.testTask:

  • When autoScalaLibrary.value is false, use ClassLoaderLayeringStrategy.Flat so the test classloader is built without the ScalaInstance layer.
  • Use project classpath only (rawCP) for fullCP in that case (do not prepend si.libraryJars).
  • Do not add si.libraryJars to the exclude set when autoScalaLibrary is false.

No change to forked tests or to the run task.

Verification

  1. Reproduce (without fix): In a project with Test / autoScalaLibrary := false and a test framework that pulls in Scala (e.g. ScalaCheck), run test without forking; previously the framework could load scala-library from the ScalaInstance layer and fail (e.g. NoSuchMethodError: scala.collection.immutable.Set$.apply).
  2. Verify: Same project runs test successfully with this fix (test classloader uses only project classpath).
  3. Scripted: ./sbt "scripted tests/autoScalaLibrary-test-loader" — runs tests with Test / autoScalaLibrary := false.
  4. Regression: ./sbt "scripted classloader-cache/scalatest" — passes.

Tests / lint

  • ./sbt compile — success
  • ./sbt "scripted tests/autoScalaLibrary-test-loader" — success
  • ./sbt "scripted classloader-cache/scalatest" — success

Closes #4009

bitloi and others added 2 commits February 21, 2026 19:47
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.
@eed3si9n eed3si9n changed the title fix:Non-fork test classloader respects autoScalaLibrary [2.x]fix: Non-fork test classloader respects autoScalaLibrary Feb 21, 2026
Copy link
Copy Markdown
Member

@eed3si9n eed3si9n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks!

@eed3si9n eed3si9n merged commit 861fbcc into sbt:develop Feb 21, 2026
15 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Non-fork test ends up with unwanted scala-library even with autoScalaLibrary := false

2 participants