[2.x] fix: Fixes forked run baseDirectory, take 2 (#9531)

**Problem**
Forked run baseDirectory was changed to current directory in sbt 2.0.4,
which on its own is fine, but it doesn't respect
Compile / run / baseDirectory.

**Solution**
This fixes that.
This commit is contained in:
eugene yokota 2026-07-31 00:04:22 -04:00 committed by GitHub
parent 3a79f269c7
commit 130ee707b9
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 74 additions and 2 deletions

View File

@ -1428,7 +1428,11 @@ object Defaults extends BuildCommon with DefExtra {
/** Fork options for run-like tasks: the forked process inherits sbt's working directory. */
private[sbt] def runForkOptionsTask: Initialize[Task[ForkOptions]] =
Def.task(forkOptionsTask.value.withWorkingDirectory(None))
Def.task {
// this uses Compile / run / baseDirectory, which defaults to ThisBuild / baseDirectory
forkOptionsTask.value
.withWorkingDirectory(Some(baseDirectory.value))
}
def testExecutionTask(task: Scoped): Initialize[Task[Tests.Execution]] =
Def.task {
@ -2609,7 +2613,8 @@ object Defaults extends BuildCommon with DefExtra {
private lazy val newRunnerSettings: Seq[Setting[?]] =
Seq(
runner := Def.uncached(ClassLoaders.runner.value),
forkOptions := Def.uncached(runForkOptionsTask.value)
forkOptions := Def.uncached(runForkOptionsTask.value),
baseDirectory := (ThisBuild / baseDirectory).value,
)
lazy val baseTasks: Seq[Setting[?]] = projectTasks ++ packageBase

View File

@ -0,0 +1,10 @@
package example
import java.io.File
import java.nio.file.{ Files, Path }
@main
def hello(arg: String*): Unit =
val x = new File(".").getAbsolutePath
println(s"hi $x")
Files.createFile(Path.of("flag"))

View File

@ -0,0 +1,17 @@
scalaVersion := "3.8.4"
@transient
lazy val check = taskKey[Unit]("")
lazy val root = rootProject
.autoAggregate
lazy val app = project
.settings(
check := {
val b = (ThisBuild / baseDirectory).value
val fo = (Compile / run / forkOptions).value
assert(fo.workingDirectory == Some(b), s"${fo.workingDirectory}")
},
Compile / run / fork := true,
)

View File

@ -0,0 +1,20 @@
scalaVersion := "3.8.4"
@transient
lazy val check = taskKey[Unit]("")
lazy val root = rootProject
.autoAggregate
lazy val app = project
.settings(
check := {
val b = baseDirectory.value
val fo = (Compile / run / forkOptions).value
assert(fo.workingDirectory == Some(b), s"${fo.workingDirectory}")
},
Compile / run / fork := true,
// app's own baseDirectory is explicitly requested as run's working
// directory, so `app/run` is expected to execute from app/.
Compile / run / baseDirectory := baseDirectory.value,
)

View File

@ -0,0 +1,20 @@
# app sets Compile / run / baseDirectory to its own baseDirectory, so
# `app/run` is expected to execute with app/ as its working directory.
$ copy-file changes/a.sbt build.sbt
> reload
> app/check
> app/run
$ exists flag
$ absent app/flag
$ delete flag
$ copy-file changes/b.sbt build.sbt
> reload
> app/check
> app/run
$ exists app/flag
$ absent flag
$ delete app/flag