Handle ThisProject in aggregate and dependencies

Fixes #3616 - Scope's resolveProjectBuild and resolveProjectRef
mishandle ThisProject.

Previously, using ThisProject in aggregate() or dependsOn() would
cause a runtime error: 'Cannot resolve ThisProject w/o the current
project'.

This change adds proper handling for ThisProject in Load.scala:
- In checkAll: Skip validation for ThisProject (refers to self)
- In resolveProjects: Resolve ThisProject to ProjectRef(uri, p.id)
This commit is contained in:
bitloi 2026-02-01 01:06:07 +01:00
parent a6e4c9c3c1
commit 1a6039c17a
3 changed files with 20 additions and 0 deletions

View File

@ -668,6 +668,7 @@ private[sbt] object Load {
do
ref match
case LocalAggregate => ()
case ThisProject => ()
case _ =>
val ProjectRef(refURI, refID) = Scope.resolveProjectRef(uri, rootProject, ref)
val loadedUnit = builds(refURI)
@ -714,6 +715,7 @@ private[sbt] object Load {
val resolve: Project => ResolvedProject = (p: Project) =>
p.resolve:
case LocalAggregate => resolveAutoAggregate(uri, p, ps)
case ThisProject => Vector(ProjectRef(uri, p.id))
case ref => Vector(Scope.resolveProjectRef(uri, rootProject, ref))
LoadedBuildUnit(
unit.unit,

View File

@ -0,0 +1,15 @@
ThisBuild / scalaVersion := "2.13.16"
lazy val root = project
.in(file("."))
.aggregate(ThisProject)
.settings(
name := "root-project"
)
lazy val check = taskKey[Unit]("Verify ThisProject in aggregate works")
root / check := {
val n = (root / name).value
assert(n == "root-project", s"Expected 'root-project' but got '$n'")
}

View File

@ -0,0 +1,3 @@
# Verify that ThisProject in aggregate doesn't cause runtime error
# This tests the fix for https://github.com/sbt/sbt/issues/3616
> check