mirror of https://github.com/sbt/sbt.git
[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:
parent
55e75faee2
commit
398af2eaaa
|
|
@ -1066,14 +1066,19 @@ object Defaults extends BuildCommon {
|
||||||
selectMainClass := mainClass.value orElse askForMainClass(discoveredMainClasses.value),
|
selectMainClass := mainClass.value orElse askForMainClass(discoveredMainClasses.value),
|
||||||
run / mainClass := (run / selectMainClass).value,
|
run / mainClass := (run / selectMainClass).value,
|
||||||
mainClass := Def.uncached {
|
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 {
|
val logWarning = state.value.currentCommand.forall(!_.commandLine.split(" ").exists {
|
||||||
case "run" | "runMain" => true
|
case s if isRunCommand(s) => true
|
||||||
case r =>
|
case r =>
|
||||||
r.split("/") match {
|
// Handle both "/" (new syntax like Test/run) and ":" (old syntax like test:run)
|
||||||
|
r.split("[/:]") match {
|
||||||
case Array(parts*) =>
|
case Array(parts*) =>
|
||||||
parts.lastOption match {
|
parts.lastOption match {
|
||||||
case Some("run" | "runMain") => true
|
case Some(s) if isRunCommand(s) => true
|
||||||
case _ => false
|
case _ => false
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1 @@
|
||||||
|
scalaVersion := "2.13.16"
|
||||||
|
|
@ -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")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -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
|
||||||
Loading…
Reference in New Issue