[2.0.x] fix: Fixes metabuild reloading (#9019)

**Problem**
There seems to be multiple problems around metabuild reloading (reload plugins).

1. Originally reported issue 9006 states that the build can't come back
   from reload plugins.
2. During the course of fixing, I discovered that when reload plugins is used
   the project name is set to "project" instead of foo-build etc.
3. Also there was a bug in the rootPaths when reload plugins is used,
   which results in class not defined error.

**Solution**
1. Fix the plugin context so reload plugin still behaves like a metabuild.
2. Fix the rootPaths.
This commit is contained in:
eugene yokota 2026-04-05 03:13:23 -04:00 committed by Eugene Yokota
parent c89f14caa1
commit 75edb0afe0
5 changed files with 66 additions and 4 deletions

View File

@ -73,10 +73,10 @@ private[sbt] object Load {
val launcher = scalaProvider.launcher
val stagingDirectory = getStagingDirectory(state, globalBase).getCanonicalFile
val javaHome = Util.javaHome
val out = baseDirectory.toPath.resolve("target").resolve("out")
val out = app.baseDirectory.toPath.resolve("target").resolve("out")
val rootPaths = Map(
"OUT" -> out,
"BASE" -> baseDirectory.toPath,
"BASE" -> app.baseDirectory.toPath,
"SBT_BOOT" -> launcher.bootDirectory.toPath,
"IVY_HOME" -> launcher.ivyHome.toPath,
"JAVA_HOME" -> javaHome,
@ -119,7 +119,9 @@ private[sbt] object Load {
)
val evalPluginDef: (BuildStructure, State) => PluginData = EvaluateTask.evalPluginDef
val delegates = defaultDelegates
val pluginMgmt = PluginManagement(loader)
import sbt.ProjectExtra.projectReturn
val pluginContext = PluginManagement.Context(false, Project.projectReturn(state).size - 1)
val pluginMgmt = PluginManagement(loader, pluginContext)
val inject = InjectSettings(injectGlobal(state), Nil, const(Nil))
SysProp.setSwovalTempDir()
SysProp.setIpcSocketTempDir()

View File

@ -54,12 +54,15 @@ object PluginManagement {
val emptyContext: Context = Context(false, 0)
def apply(initialLoader: ClassLoader): PluginManagement =
PluginManagement(initialLoader, emptyContext)
def apply(initialLoader: ClassLoader, context: Context): PluginManagement =
PluginManagement(
Set.empty,
Set.empty,
new PluginClassLoader(initialLoader),
initialLoader,
emptyContext
context
)
def extractOverrides(classpath: Classpath): Set[ModuleID] =

View File

@ -0,0 +1,24 @@
package example
import sbt.*
import Keys.*
import complete.DefaultParsers.{ *, given }
object FooPlugin extends AutoPlugin:
override def requires = empty
override def trigger = allRequirements
lazy object autoImport:
@transient
lazy val foo = taskKey[Unit]("foo")
lazy val check = inputKey[Unit]("check")
import autoImport.*
override def projectSettings: Seq[Def.Setting[?]] = Seq(
foo := println("foo"),
check := {
val args = spaceDelimited("<arg>").parsed
assert(name.value.endsWith(args.head), s"${name.value} does not end with ${args.head}")
},
)
end FooPlugin

View File

@ -0,0 +1,24 @@
package example
import sbt.*
import Keys.*
import complete.DefaultParsers.{ *, given }
object FooPlugin extends AutoPlugin:
override def requires = empty
override def trigger = allRequirements
lazy object autoImport:
@transient
lazy val foo = taskKey[Unit]("foo")
lazy val check = inputKey[Unit]("check")
import autoImport.*
override def projectSettings: Seq[Def.Setting[?]] = Seq(
foo := println("foo"),
check := {
val args = spaceDelimited("<arg>").parsed
assert(name.value.endsWith(args.head), s"${name.value} does not end with ${args.head}")
},
)
end FooPlugin

View File

@ -0,0 +1,9 @@
> foo
> reload plugins
> check -build
> foo
> reload plugins
> reload return
> reload return
> compile
> foo