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 running JARs from scala script #13246

Merged
merged 2 commits into from
Aug 5, 2021
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
38 changes: 36 additions & 2 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ import scala.annotation.internal.sharable
import dotty.tools.dotc.util.ClasspathFromClassloader
import dotty.tools.runner.ObjectRunner
import dotty.tools.dotc.config.Properties.envOrNone
import java.util.jar._
import java.util.jar.Attributes.Name

enum ExecuteMode:
case Guess
Expand All @@ -31,6 +33,7 @@ case class Settings(
targetScript: String = "",
save: Boolean = false,
modeShouldBeRun: Boolean = false,
compiler: Boolean = false,
) {
def withExecuteMode(em: ExecuteMode): Settings = this.executeMode match
case ExecuteMode.Guess =>
Expand Down Expand Up @@ -65,6 +68,9 @@ case class Settings(

def withModeShouldBeRun: Settings =
this.copy(modeShouldBeRun = true)

def withCompiler: Settings =
this.copy(compiler = true)
}

object MainGenericRunner {
Expand Down Expand Up @@ -97,6 +103,8 @@ object MainGenericRunner {
)
case "-save" :: tail =>
process(tail, settings.withSave)
case "-with-compiler" :: tail =>
process(tail, settings.withCompiler)
case (o @ javaOption(striped)) :: tail =>
process(tail, settings.withJavaArgs(striped).withScalaArgs(o))
case (o @ scalaOption(_*)) :: tail =>
Expand Down Expand Up @@ -128,8 +136,34 @@ object MainGenericRunner {
repl.Main.main(properArgs.toArray)
case ExecuteMode.Run =>
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
val newClasspath = (settings.classPath ++ scalaClasspath :+ ".").map(File(_).toURI.toURL)
errorFn("", ObjectRunner.runAndCatch(newClasspath, settings.residualArgs.head, settings.residualArgs.drop(1)))

def removeCompiler(cp: Array[String]) =
if (!settings.compiler) then // Let's remove compiler from the classpath
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extraneous parens

val compilerLibs = Seq("scala3-compiler", "scala3-interfaces", "tasty-core", "scala-asm", "scala3-staging", "scala3-tasty-inspector")
cp.filterNot(c => compilerLibs.exists(c.contains))
else
cp
val newClasspath = (settings.classPath ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)

val res = ObjectRunner.runAndCatch(newClasspath, settings.residualArgs.head, settings.residualArgs.drop(1)).flatMap {
case ex: ClassNotFoundException if ex.getMessage == settings.residualArgs.head =>
val file = settings.residualArgs.head
def withJarInput[T](f: JarInputStream => T): T =
val in = new JarInputStream(java.io.FileInputStream(file))
try f(in)
finally in.close()
val manifest = withJarInput(s => Option(s.getManifest))
manifest match
case None => Some(IllegalArgumentException(s"Cannot find manifest in jar: $file"))
case Some(f) =>
f.getMainAttributes.get(Name.MAIN_CLASS) match
case mainClass: String =>
ObjectRunner.runAndCatch(newClasspath :+ File(file).toURI.toURL, mainClass, settings.residualArgs)
case _ =>
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $file"))
case ex => Some(ex)
}
errorFn("", res)
case ExecuteMode.Script =>
val properArgs =
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,12 @@ class CoursierScalaTests:
assertTrue(source.getParentFile.listFiles.find(_.getName == "myfile.jar").isDefined)
jar()

def runThatJar() =
val source = new File(getClass.getResource("/run/myfile.jar").getPath)
val output = CoursierScalaTests.csCmd(source.absPath)
assertEquals(output.mkString("\n"), "Hello")
runThatJar()

object CoursierScalaTests:

def execCmd(command: String, options: String*): List[String] =
Expand Down