diff --git a/main/src/main/scala/sbt/ClassLoaderLayeringStrategy.scala b/main/src/main/scala/sbt/ClassLoaderLayeringStrategy.scala index 89eae4f05..7245c2e8b 100644 --- a/main/src/main/scala/sbt/ClassLoaderLayeringStrategy.scala +++ b/main/src/main/scala/sbt/ClassLoaderLayeringStrategy.scala @@ -112,7 +112,9 @@ object ClassLoaderLayeringStrategy { * Add the TestDependencies layer on top of the RuntimeDependencies layer on top of the * ScalaInstance layer. This differs from TestDependencies in that it will not reload the * runtime classpath. The drawback to using this is that if the test dependencies evict - * classes provided in the runtime layer, then tests can fail. + * classes provided in the runtime layer, then tests can fail. In order for sharing the runtime + * layer to work, it is necessary to set [[Keys.bgCopyClasspath]] to false. Otherwise the + * runtime and test classpaths are completely different. */ case object ShareRuntimeDependenciesLayerWithTestDependencies extends ScalaInstance diff --git a/main/src/main/scala/sbt/Defaults.scala b/main/src/main/scala/sbt/Defaults.scala index 107ff54b3..b504dd5c0 100755 --- a/main/src/main/scala/sbt/Defaults.scala +++ b/main/src/main/scala/sbt/Defaults.scala @@ -1781,7 +1781,15 @@ object Defaults extends BuildCommon { // by the test task layers may be cached without evicting the runtime classloader layers. The // cache size should be a multiple of two to support snapshot layers. ClassLoaderCache(4) - ) + ), + bgCopyClasspath in bgRun := { + val old = (bgCopyClasspath in bgRun).value + old && (Test / classLoaderLayeringStrategy).value != ClassLoaderLayeringStrategy.ShareRuntimeDependenciesLayerWithTestDependencies + }, + bgCopyClasspath in bgRunMain := { + val old = (bgCopyClasspath in bgRunMain).value + old && (Test / classLoaderLayeringStrategy).value != ClassLoaderLayeringStrategy.ShareRuntimeDependenciesLayerWithTestDependencies + }, ) lazy val testSettings: Seq[Setting[_]] = configSettings ++ testTasks ++