[2.x] fix: Suppress "Multiple main classes" warning for runMain commands (#8613)

Changed the scope separator parsing from `/` only to `[/:]` to handle both new slash syntax (`Test/runMain`) and old colon syntax (`test:runMain`).
This commit is contained in:
calm 2026-01-22 09:58:53 -08:00 committed by GitHub
parent 55e75faee2
commit 398af2eaaa
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 48 additions and 5 deletions

View File

@ -1066,14 +1066,19 @@ object Defaults extends BuildCommon {
selectMainClass := mainClass.value orElse askForMainClass(discoveredMainClasses.value),
run / mainClass := (run / selectMainClass).value,
mainClass := Def.uncached {
// Suppress warning for run commands (user is actively running, warning is noise)
def isRunCommand(s: String): Boolean = s match
case "run" | "runMain" | "bgRun" | "bgRunMain" | "fgRun" | "fgRunMain" => true
case _ => false
val logWarning = state.value.currentCommand.forall(!_.commandLine.split(" ").exists {
case "run" | "runMain" => true
case r =>
r.split("/") match {
case s if isRunCommand(s) => true
case r =>
// Handle both "/" (new syntax like Test/run) and ":" (old syntax like test:run)
r.split("[/:]") match {
case Array(parts*) =>
parts.lastOption match {
case Some("run" | "runMain") => true
case _ => false
case Some(s) if isRunCommand(s) => true
case _ => false
}
}
})

View File

@ -0,0 +1 @@
scalaVersion := "2.13.16"

View File

@ -0,0 +1,19 @@
package foo
object MainA {
def main(args: Array[String]): Unit = {
println("MainA")
}
}
object MainB {
def main(args: Array[String]): Unit = {
println("MainB")
}
}
object MainC {
def main(args: Array[String]): Unit = {
println("MainC")
}
}

View File

@ -0,0 +1,18 @@
# Test that run commands work with multiple main classes without showing useless warning
# This tests issue #3739
# First compile to discover main classes
> compile
# Test runMain - user explicitly specifies main class, no warning needed
> runMain foo.MainA
> runMain foo.MainB
# Test scoped runMain with slash syntax (new syntax)
> Compile/runMain foo.MainA
# Test bgRunMain - background run with explicit main class
> bgRunMain foo.MainA
# Test fgRunMain - foreground run with explicit main class
> fgRunMain foo.MainB