Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix dependency main class detection throwing an NPE when JAR manifest doesn't list the main class correctly #3319

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))
}
}
}
Loading