Skip to content

Commit

Permalink
Fix dependency main class detection throwing an NPE when JAR manifest…
Browse files Browse the repository at this point in the history
… doesn't list the main class correctly (#3319)
  • Loading branch information
Gedochao authored Nov 27, 2024
1 parent 1e23e17 commit 7846f80
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,11 @@ object MainClass {
def findInDependency(jar: os.Path): Option[String] =
jar match {
case jar if os.isFile(jar) && jar.last.endsWith(".jar") =>
val jarFile = new JarFile(jar.toIO)
val manifest = jarFile.getManifest()
val mainClass = manifest.getMainAttributes().getValue(Attributes.Name.MAIN_CLASS)
Option(mainClass).map(_.asInstanceOf[String])
for {
manifest <- Option(new JarFile(jar.toIO).getManifest)
mainAttributes <- Option(manifest.getMainAttributes)
mainClass: String <- Option(mainAttributes.getValue(Attributes.Name.MAIN_CLASS))
} yield mainClass
case _ => None
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2270,4 +2270,25 @@ abstract class RunTestDefinitions
}
}
}

if (actualScalaVersion.startsWith("3")) test(
"fail with a valid error when multiple main classes are present and a dependency doesn't define main classes in the manifest"
) {
val (main1, main2) = "main1" -> "main2"
val input = "example.scala"
TestInputs(
os.rel / input ->
s"""//> using dep io.get-coursier:coursier_2.13:2.1.18
|@main def $main1() = println("$main1")
|@main def $main2() = println("$main2")
|""".stripMargin
).fromRoot { root =>
val res = os.proc(TestUtil.cli, "run", input, extraOptions)
.call(cwd = root, stderr = os.Pipe, check = false)
val err = res.err.trim()
expect(err.contains("Found several main classes"))
expect(err.contains(main1))
expect(err.contains(main2))
}
}
}

0 comments on commit 7846f80

Please sign in to comment.