Skip to content

insert compiler libraries head of user-specified classpath - fix for 13552 #13562

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

Merged
merged 10 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from 6 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
27 changes: 21 additions & 6 deletions compiler/src/dotty/tools/MainGenericRunner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import java.util.jar._
import java.util.jar.Attributes.Name
import dotty.tools.io.Jar
import dotty.tools.runner.ScalaClassLoader
import java.nio.file.{Files, Paths, Path}
import scala.collection.JavaConverters._
import dotty.tools.dotc.config.CommandLineParser

enum ExecuteMode:
case Guess
Expand Down Expand Up @@ -102,7 +105,18 @@ object MainGenericRunner {
case "-run" :: fqName :: tail =>
process(tail, settings.withExecuteMode(ExecuteMode.Run).withTargetToRun(fqName))
case ("-cp" | "-classpath" | "--class-path") :: cp :: tail =>
process(tail, settings.copy(classPath = settings.classPath.appended(cp)))
val (tailargs, cpstr) = if classpathSeparator != ";" || cp.contains(classpathSeparator) then
(tail, cp)
else
// combine globbed classpath entries into a classpath
val globdir = cp.replaceAll("[\\/][^\\/]*$","") // must be forward-backward-slash-agnostic
val jarfiles = cp :: tail
val cpfiles = jarfiles.takeWhile( f => f.startsWith(globdir) && ((f.toLowerCase.endsWith(".jar") || f.endsWith(".zip"))) )
val tailargs = jarfiles.drop(cpfiles.size)
(tailargs, cpfiles.mkString(classpathSeparator))

process(tailargs, settings.copy(classPath = settings.classPath.appended(cpstr)))

case ("-version" | "--version") :: _ =>
settings.copy(
executeMode = ExecuteMode.Repl,
Expand All @@ -123,7 +137,8 @@ object MainGenericRunner {
case (o @ javaOption(striped)) :: tail =>
process(tail, settings.withJavaArgs(striped).withScalaArgs(o))
case (o @ scalaOption(_*)) :: tail =>
process(tail, settings.withScalaArgs(o))
val remainingArgs = (CommandLineParser.expandArg(o) ++ tail).toList
process(remainingArgs, settings)
case (o @ colorOption(_*)) :: tail =>
process(tail, settings.withScalaArgs(o))
case arg :: tail =>
Expand Down Expand Up @@ -151,7 +166,7 @@ object MainGenericRunner {
repl.Main.main(properArgs.toArray)

case ExecuteMode.PossibleRun =>
val newClasspath = (settings.classPath :+ ".").map(File(_).toURI.toURL)
val newClasspath = (settings.classPath :+ ".").flatMap(_.split(classpathSeparator).filter(_.nonEmpty)).map(File(_).toURI.toURL)
import dotty.tools.runner.RichClassLoader._
val newClassLoader = ScalaClassLoader.fromURLsParallelCapable(newClasspath)
val targetToRun = settings.possibleEntryPaths.to(LazyList).find { entryPath =>
Expand All @@ -173,8 +188,7 @@ object MainGenericRunner {
cp.filterNot(c => compilerLibs.exists(c.contains))
else
cp
val newClasspath = (settings.classPath ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)

val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
val res = ObjectRunner.runAndCatch(newClasspath, settings.targetToRun, settings.residualArgs).flatMap {
case ex: ClassNotFoundException if ex.getMessage == settings.targetToRun =>
val file = settings.targetToRun
Expand All @@ -187,12 +201,13 @@ object MainGenericRunner {
}
errorFn("", res)
case ExecuteMode.Script =>
val targetScriptPath: String = settings.targetScript.toString.replace('\\', '/')
val properArgs =
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
++ settings.residualArgs
++ (if settings.save then List("-save") else Nil)
++ List("-script", settings.targetScript)
++ settings.scalaArgs
++ List("-script", settings.targetScript)
++ settings.scriptArgs
scripting.Main.main(properArgs.toArray)
case ExecuteMode.Guess =>
Expand Down
19 changes: 2 additions & 17 deletions compiler/src/dotty/tools/dotc/config/CliCommand.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import Settings._
import core.Contexts._
import Properties._

import scala.PartialFunction.cond
import scala.collection.JavaConverters._
import scala.PartialFunction.cond

trait CliCommand:

Expand Down Expand Up @@ -42,24 +41,10 @@ trait CliCommand:

/** Distill arguments into summary detailing settings, errors and files to main */
def distill(args: Array[String], sg: Settings.SettingGroup)(ss: SettingsState = sg.defaultState)(using Context): ArgsSummary =
/**
* Expands all arguments starting with @ to the contents of the
* file named like each argument.
*/
def expandArg(arg: String): List[String] =
def stripComment(s: String) = s takeWhile (_ != '#')
val path = Paths.get(arg stripPrefix "@")
if (!Files.exists(path))
report.error(s"Argument file ${path.getFileName} could not be found")
Nil
else
val lines = Files.readAllLines(path) // default to UTF-8 encoding
val params = lines.asScala map stripComment mkString " "
CommandLineParser.tokenize(params)

// expand out @filename to the contents of that filename
def expandedArguments = args.toList flatMap {
case x if x startsWith "@" => expandArg(x)
case x if x startsWith "@" => CommandLineParser.expandArg(x)
case x => List(x)
}

Expand Down
17 changes: 17 additions & 0 deletions compiler/src/dotty/tools/dotc/config/CommandLineParser.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ package dotty.tools.dotc.config
import scala.annotation.tailrec
import scala.collection.mutable.ArrayBuffer
import java.lang.Character.isWhitespace
import java.nio.file.{Files, Paths}
import scala.collection.JavaConverters._

/** A simple enough command line parser.
*/
Expand Down Expand Up @@ -93,4 +95,19 @@ object CommandLineParser:

def tokenize(line: String): List[String] = tokenize(line, x => throw new ParseException(x))

/**
* Expands all arguments starting with @ to the contents of the
* file named like each argument.
*/
def expandArg(arg: String): List[String] =
def stripComment(s: String) = s takeWhile (_ != '#')
val path = Paths.get(arg stripPrefix "@")
if (!Files.exists(path))
System.err.println(s"Argument file ${path.getFileName} could not be found")
Nil
else
val lines = Files.readAllLines(path) // default to UTF-8 encoding
val params = lines.asScala map stripComment mkString " "
tokenize(params)

class ParseException(msg: String) extends RuntimeException(msg)
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,16 @@ class CoursierScalaTests:
assertTrue(output.mkString("\n").contains("Unable to create a system terminal")) // Scala attempted to create REPL so we can assume it is working
replWithArgs()

def argumentFile() =
// verify that an arguments file is accepted
// verify that setting a user classpath does not remove compiler libraries from the classpath.
// arguments file contains "-classpath .", adding current directory to classpath.
val source = new File(getClass.getResource("/run/myfile.scala").getPath)
val argsFile = new File(getClass.getResource("/run/myargs.txt").getPath)
val output = CoursierScalaTests.csScalaCmd(s"@$argsFile", source.absPath)
assertEquals(output.mkString("\n"), "Hello")
argumentFile()

object CoursierScalaTests:

def execCmd(command: String, options: String*): List[String] =
Expand Down
1 change: 1 addition & 0 deletions compiler/test-coursier/run/myargs.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-classpath .
9 changes: 9 additions & 0 deletions compiler/test-resources/scripting/argfileClasspath.sc
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!dist/target/pack/bin/scala @compiler/test-resources/scripting/cpArgumentsFile.txt

import java.nio.file.Paths

def main(args: Array[String]): Unit =
val cwd = Paths.get(".").toAbsolutePath.toString.replace('\\', '/').replaceAll("/$", "")
printf("cwd: %s\n", cwd)
printf("classpath: %s\n", sys.props("java.class.path"))

9 changes: 6 additions & 3 deletions compiler/test-resources/scripting/classpathReport.sc
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
#!dist/target/pack/bin/scala -classpath 'dist/target/pack/lib/*'
#!dist/target/pack/bin/scala -classpath dist/target/pack/lib/*

import java.nio.file.Paths

def main(args: Array[String]): Unit =
val cwd = Paths.get(".").toAbsolutePath.toString.replace('\\', '/').replaceAll("/$", "")
val cwd = Paths.get(".").toAbsolutePath.normalize.toString.norm
printf("cwd: %s\n", cwd)
printf("classpath: %s\n", sys.props("java.class.path"))
printf("classpath: %s\n", sys.props("java.class.path").norm)

extension(s: String)
def norm: String = s.replace('\\', '/')

1 change: 1 addition & 0 deletions compiler/test-resources/scripting/cpArgumentsFile.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
-classpath dist/target/pack/lib/*
7 changes: 5 additions & 2 deletions compiler/test-resources/scripting/scriptPath.sc
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#!/usr/bin/env scala
#!dist/target/pack/bin/scala

def main(args: Array[String]): Unit =
args.zipWithIndex.foreach { case (arg,i) => printf("arg %d: [%s]\n",i,arg) }
val path = Option(sys.props("script.path")) match {
case None => printf("no script.path property is defined\n")
case Some(path) =>
printf("script.path: %s\n",path)
printf("script.path: %s\n",path.norm)
assert(path.endsWith("scriptPath.sc"),s"actual path [$path]")
}

extension(s: String)
def norm: String = s.replace('\\', '/')
28 changes: 16 additions & 12 deletions compiler/test/dotty/tools/scripting/BashScriptsTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,12 @@ class BashScriptsTests:
@Test def verifyScriptPathProperty =
val scriptFile = testFiles.find(_.getName == "scriptPath.sc").get
val expected = s"/${scriptFile.getName}"
printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
System.err.printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
val (exitCode, stdout, stderr) = bashCommand(scriptFile.absPath)
if exitCode == 0 && ! stderr.exists(_.contains("Permission denied")) then
// var cmd = Array(bashExe, "-c", scriptFile.absPath)
// val stdout = Process(cmd).lazyLines_!
stdout.foreach { printf("######### [%s]\n", _) }
stdout.foreach { System.err.printf("######### [%s]\n", _) }
val valid = stdout.exists { _.endsWith(expected) }
if valid then printf("# valid script.path reported by [%s]\n", scriptFile.getName)
assert(valid, s"script ${scriptFile.absPath} did not report valid script.path value")
Expand All @@ -99,35 +99,39 @@ class BashScriptsTests:
*/
@Test def verifyScalaOpts =
val scriptFile = testFiles.find(_.getName == "classpathReport.sc").get
printf("===> verify valid system property script.path is reported by script [%s]\n", scriptFile.getName)
printf("===> verify SCALA_OPTS -classpath setting in argument file seen by script [%s]\n", scriptFile.getName)
val argsfile = createArgsFile() // avoid problems caused by drive letter
val envPairs = List(("SCALA_OPTS", s"@$argsfile"))
val (exitCode, stdout, stderr) = bashCommand(scriptFile.absPath, envPairs:_*)
printf("\n")
if exitCode != 0 || stderr.exists(_.contains("Permission denied")) then
stderr.foreach { System.err.printf("stderr [%s]\n", _) }
printf("unable to execute script, return value is %d\n", exitCode)
else
// val stdout: Seq[String] = Process(cmd, cwd, envPairs:_*).lazyLines_!.toList
val expected = s"${cwd.toString}"
val expected = cwd
val List(line1: String, line2: String) = stdout.take(2)
printf("line1 [%s]\n", line1)
val valid = line2.dropWhile( _ != ' ').trim.startsWith(expected)
val psep = if osname.startsWith("Windows") then ';' else ':'
printf("line2 start [%s]\n", line2.take(100))
if valid then printf(s"\n===> success: classpath begins with %s, as reported by [%s]\n", cwd, scriptFile.getName)
assert(valid, s"script ${scriptFile.absPath} did not report valid java.class.path first entry")
assert(valid, s"script ${scriptFile.getName} did not report valid java.class.path first entry")

lazy val cwd = Paths.get(dotty.tools.dotc.config.Properties.userDir).toFile
lazy val cwd: String = Paths.get(".").toAbsolutePath.normalize.toString.norm

def createArgsFile(): String =
val utfCharset = java.nio.charset.StandardCharsets.UTF_8.name
val text = s"-classpath ${cwd.absPath}"
val text = s"-classpath $cwd"
val path = Files.createTempFile("scriptingTest", ".args")
Files.write(path, text.getBytes(utfCharset))
path.toFile.getAbsolutePath.replace('\\', '/')
path.toFile.getAbsolutePath.norm

extension (str: String) def dropExtension: String =
str.reverse.dropWhile(_ != '.').drop(1).reverse
extension(str: String)
def norm: String = str.replace('\\', '/')
def dropExtension: String = str.reverse.dropWhile(_ != '.').drop(1).reverse

extension(f: File) def absPath: String =
f.getAbsolutePath.replace('\\', '/')
f.getAbsolutePath.norm

lazy val osname = Option(sys.props("os.name")).getOrElse("").toLowerCase

Expand Down
Loading