Skip to content

Commit 0a834ff

Browse files
authored
Merge pull request #14252 from philwalk/fixes-with-workaround-for-13760
fix 2 problems plus add workaround for #13760
2 parents 84b26ac + bfee6be commit 0a834ff

File tree

3 files changed

+37
-3
lines changed

3 files changed

+37
-3
lines changed

compiler/src/dotty/tools/MainGenericRunner.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ case class Settings(
8181
def withSave: Settings =
8282
this.copy(save = true)
8383

84+
def noSave: Settings =
85+
this.copy(save = false)
86+
8487
def withModeShouldBePossibleRun: Settings =
8588
this.copy(modeShouldBePossibleRun = true)
8689

@@ -135,6 +138,8 @@ object MainGenericRunner {
135138
)
136139
case "-save" :: tail =>
137140
process(tail, settings.withSave)
141+
case "-nosave" :: tail =>
142+
process(tail, settings.noSave)
138143
case "-with-compiler" :: tail =>
139144
process(tail, settings.withCompiler)
140145
case (o @ javaOption(striped)) :: tail =>
@@ -207,18 +212,20 @@ object MainGenericRunner {
207212
case ExecuteMode.Script =>
208213
val targetScript = Paths.get(settings.targetScript).toFile
209214
val targetJar = settings.targetScript.replaceAll("[.][^\\/]*$", "")+".jar"
210-
val precompiledJar = Paths.get(targetJar).toFile
215+
val precompiledJar = File(targetJar)
211216
val mainClass = if !precompiledJar.isFile then "" else Jar(targetJar).mainClass.getOrElse("")
212-
val jarIsValid = mainClass.nonEmpty && precompiledJar.lastModified >= targetScript.lastModified
217+
val jarIsValid = mainClass.nonEmpty && precompiledJar.lastModified >= targetScript.lastModified && settings.save
213218
if jarIsValid then
214219
// precompiledJar exists, is newer than targetScript, and manifest defines a mainClass
215220
sys.props("script.path") = targetScript.toPath.toAbsolutePath.normalize.toString
216221
val scalaClasspath = ClasspathFromClassloader(Thread.currentThread().getContextClassLoader).split(classpathSeparator)
217222
val newClasspath = (settings.classPath.flatMap(_.split(classpathSeparator).filter(_.nonEmpty)) ++ removeCompiler(scalaClasspath) :+ ".").map(File(_).toURI.toURL)
218-
if mainClass.nonEmpty then
223+
val res = if mainClass.nonEmpty then
219224
ObjectRunner.runAndCatch(newClasspath :+ File(targetJar).toURI.toURL, mainClass, settings.scriptArgs)
220225
else
221226
Some(IllegalArgumentException(s"No main class defined in manifest in jar: $precompiledJar"))
227+
errorFn("", res)
228+
222229
else
223230
val properArgs =
224231
List("-classpath", settings.classPath.mkString(classpathSeparator)).filter(Function.const(settings.classPath.nonEmpty))
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
#!bin/scala -nosave
2+
3+
def main(args: Array[String]): Unit = {
4+
println(new java.sql.Date(100L))
5+
System.err.println("SCALA_OPTS="+Option(System.getenv("SCALA_OPTS")).getOrElse(""))
6+
}

compiler/test/dotty/tools/scripting/BashScriptsTests.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,3 +188,24 @@ class BashScriptsTests:
188188
if valid then printf(s"\n===> success: classpath begins with %s, as reported by [%s]\n", workingDirectory, scriptFile.getName)
189189
assert(valid, s"script ${scriptFile.absPath} did not report valid java.class.path first entry")
190190

191+
/*
192+
* verify that individual scripts can override -save with -nosave (needed to address #13760).
193+
*/
194+
@Test def sqlDateTest =
195+
val scriptBase = "sqlDateError"
196+
val scriptFile = testFiles.find(_.getName == s"$scriptBase.sc").get
197+
val testJar = testFile(s"$scriptBase.jar") // jar should not be created when scriptFile runs
198+
printf("===> verify '-save' is cancelled by '-nosave' in script hashbang.`\n")
199+
val (validTest, exitCode, stdout, stderr) = bashCommand(s"SCALA_OPTS=-save ${scriptFile.absPath}")
200+
printf("stdout: %s\n", stdout.mkString("\n","\n",""))
201+
if verifyValid(validTest) then
202+
// the script should print '1969-12-31' or '1970-01-01', depending on time zone
203+
// stdout can be polluted with an ANSI color prefix, in some test environments
204+
val valid = stdout.mkString("").matches(""".*\d{4}-\d{2}-\d{2}.*""")
205+
if (!valid) then
206+
stdout.foreach { printf("stdout[%s]\n", _) }
207+
stderr.foreach { printf("stderr[%s]\n", _) }
208+
if valid then printf(s"\n===> success: scripts can override -save via -nosave\n")
209+
assert(valid, s"script ${scriptFile.absPath} reported unexpected value for java.sql.Date ${stdout.mkString("\n")}")
210+
assert(!testJar.exists,s"unexpected, jar file [$testJar] was created")
211+

0 commit comments

Comments
 (0)