[2.0.x] perf: short-circuit --version from build.properties (#9332)

Avoid launching sbt just to render --version by reading sbt.version directly from project/build.properties in the shell script, batch script, and sbtw wrapper. Tighten launcher integration assertions to verify version output no longer depends on the sbtVersion command output.

Co-authored-by: bitloi <[email protected]>
This commit is contained in:
eugene yokota
2026-06-14 00:27:14 -04:00
committed by GitHub
co-authored by bitloi
parent de1b5ee9ea
commit 5f52e52dbf
4 changed files with 54 additions and 25 deletions
@@ -12,11 +12,9 @@ object RunnerScriptTest extends verify.BasicTestSuite with ShellScriptUtil:
private def assertVersionOutput(out: List[String]): Unit =
val lines =
out.mkString(System.lineSeparator()).linesIterator.map(_.stripPrefix("[0J").trim).toList
assert(
lines.exists(_.matches("^sbt version in this project: " + versionPattern + "\\r?$")) ||
lines.contains("sbtVersion")
)
assert(lines.exists(_.matches("^sbt version in this project: " + versionPattern + "\\r?$")))
assert(lines.exists(_.matches("^sbt runner version: " + versionPattern + "\\r?$")))
assert(!lines.exists(_.contains("sbtVersion")))
assert(!lines.exists(_.contains("failed to connect to server")))
testOutput("sbt -no-colors")("compile", "-no-colors", "-v"): (out: List[String]) =>
+16 -2
View File
@@ -754,8 +754,12 @@ if !sbt_args_print_sbt_version! equ 1 (
if !sbt_args_print_version! equ 1 (
if !is_this_dir_sbt! equ 1 (
call :set_sbt_version
echo sbt version in this project: !sbt_version!
if defined build_props_sbt_version (
call :set_sbt_version_from_build_props
) else (
call :set_sbt_version
)
if defined sbt_version echo sbt version in this project: !sbt_version!
)
echo sbt runner version: !init_sbt_version!
>&2 echo.
@@ -1183,6 +1187,16 @@ for /F "usebackq tokens=1,2 delims= " %%a in (`CALL "!_JAVACMD!" -jar "!sbt_jar!
if not defined sbt_version if defined build_props_sbt_version set "sbt_version=!build_props_sbt_version!"
exit /B 0
:set_sbt_version_from_build_props
set "sbt_version=!build_props_sbt_version!"
for /F "tokens=* delims= " %%a in ("!sbt_version!") do set "sbt_version=%%a"
:trim_version_end
if "!sbt_version:~-1!" == " " (
set "sbt_version=!sbt_version:~0,-1!"
goto trim_version_end
)
exit /B 0
:error
@endlocal
exit /B 1
+14 -1
View File
@@ -587,7 +587,10 @@ run() {
execRunner "$java_cmd" -jar "$sbt_jar" "sbtVersion" | tail -1 | sed -e 's/\[info\]//g'
elif [[ $print_version ]]; then
if [[ -n "$is_this_dir_sbt" ]]; then
execRunner "$java_cmd" -jar "$sbt_jar" "sbtVersion" | tail -1 | sed -e 's/\[info\]/sbt version in this project:/g'
local project_sbt_version
if project_sbt_version="$(projectSbtVersion)"; then
echo "sbt version in this project: $project_sbt_version"
fi
fi
echo "sbt runner version: $init_sbt_version"
echoerr ""
@@ -810,6 +813,16 @@ loadPropFile() {
done <<< "$(cat "$1" | sed $'/^\#/d;s/\r$//')"
}
projectSbtVersion() {
local version
version="$(trimString "$build_props_sbt_version")"
if [[ -n "$version" ]]; then
echo "$version"
return 0
fi
return 1
}
detectNativeClient() {
if [[ "$sbtn_command" != "" ]]; then
:
+22 -18
View File
@@ -116,34 +116,38 @@ object Main:
if opts.scriptVersion then
println(LauncherOptions.initSbtVersion)
return 0
val javaCmd = Runner.findJavaCmd(opts.javaHome)
val sbtJar = opts.sbtJar
.filter(p => new File(p).isFile)
.getOrElse(new File(sbtBinDir, "sbt-launch.jar").getAbsolutePath)
if !new File(sbtJar).isFile then
System.err.println("[error] Launcher jar not found for version check")
return 1
if opts.numericVersion then
try
val out = Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
println(out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse(""))
return 0
catch { case _: Exception => return 1 }
if opts.version then
if ConfigLoader.isSbtProjectDir(cwd) then
val out =
try Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
catch { case _: Exception => "" }
val ver = out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse("")
println("sbt version in this project: " + ver)
projectSbtVersion(cwd).foreach: version =>
println("sbt version in this project: " + version)
println("sbt runner version: " + LauncherOptions.initSbtVersion)
System.err.println("[info] sbt runner (sbtw) is a runner to run any declared version of sbt.")
System.err.println(
"[info] Actual version of sbt is declared using project\\build.properties for each build."
)
return 0
if opts.numericVersion then
val javaCmd = Runner.findJavaCmd(opts.javaHome)
val sbtJar = opts.sbtJar
.filter(p => new File(p).isFile)
.getOrElse(new File(sbtBinDir, "sbt-launch.jar").getAbsolutePath)
if !new File(sbtJar).isFile then
System.err.println("[error] Launcher jar not found for version check")
return 1
try
val out = Process(Seq(javaCmd, "-jar", sbtJar, "sbtVersion")).!!
println(out.linesIterator.toSeq.lastOption.map(_.trim).getOrElse(""))
return 0
catch { case _: Exception => return 1 }
0
private def projectSbtVersion(cwd: File): Option[String] =
ConfigLoader.sbtVersionFromBuildProperties(cwd).flatMap(normalizeVersion)
private def normalizeVersion(version: String): Option[String] =
val trimmed = version.trim
if trimmed.nonEmpty then Some(trimmed) else None
private def printUsage(): Int =
println("""
|Usage: sbtw [options]