From 1852b4a4547bbda54c17753438f7845a30eb2b78 Mon Sep 17 00:00:00 2001 From: BitToby <218712309+bittoby@users.noreply.github.com> Date: Fri, 20 Mar 2026 21:45:36 +0200 Subject: [PATCH] [2.x] fix: Fix scripted test loading build definition twice on first test in batch (#8946) --- .../scala/sbt/scriptedtest/ScriptedTests.scala | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripted-sbt/src/main/scala/sbt/scriptedtest/ScriptedTests.scala b/scripted-sbt/src/main/scala/sbt/scriptedtest/ScriptedTests.scala index 3750163ea..837661fb1 100644 --- a/scripted-sbt/src/main/scala/sbt/scriptedtest/ScriptedTests.scala +++ b/scripted-sbt/src/main/scala/sbt/scriptedtest/ScriptedTests.scala @@ -238,19 +238,21 @@ final class ScriptedTests( IO.copyDirectory(originalDir, tempTestDir) val runTest = () => { - // Reload and initialize (to reload contents of .sbtrc files) def sbtHandlerError = sys error "Missing sbt handler. Scripted is misconfigured." val sbtHandler = handlers.getOrElse('>', sbtHandlerError) - val statement = Statement("reload;initialize", Nil, successExpected = true, line = -1) - // Run reload inside the hook to reuse error handling for pending tests val wrapHook = (file: File) => { preHook(file) - try runner.processStatement(sbtHandler, statement, states) - catch { - case t: Throwable => - val newMsg = "Reload for scripted batch execution failed." - throw new TestException(statement, newMsg, t) + // Reload only when a previous sbt instance exists; a new instance already loads the project on boot + if (states(sbtHandler) != None) { + val statement = + Statement("reload;initialize", Nil, successExpected = true, line = -1) + try runner.processStatement(sbtHandler, statement, states) + catch { + case t: Throwable => + val newMsg = "Reload for scripted batch execution failed." + throw new TestException(statement, newMsg, t) + } } }