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

Allow to use object as a script wrapper #2117

Closed
dacr opened this issue May 13, 2023 · 8 comments · Fixed by #2136
Closed

Allow to use object as a script wrapper #2117

dacr opened this issue May 13, 2023 · 8 comments · Fixed by #2136
Assignees
Labels
bug Something isn't working script-wrapper

Comments

@dacr
Copy link

dacr commented May 13, 2023

Version(s)
Only from 1.0.0-RC2, everything is fine with previous releases.

Describe the bug
The script compile, then a *** RUN ABORTED*** message is printed
image.

(Of course I could use the new test.dep and scl test myscript.sc approach but it should run as before the old way)

To Reproduce

// ---------------------
//> using scala  "3.2.2"
//> using dep "org.scalatest::scalatest:3.2.15"
// ---------------------

import org.scalatest.*, flatspec.*, matchers.*

class PiTest extends AnyFlatSpec with should.Matchers {
  "pi calculus" should "return a precise enough pi value" in {
    math.Pi shouldBe 3.14158d +- 0.001d
  }
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[PiTest].getName))

Expected behaviour
running fine without this error message and with this output :
image

@dacr dacr added the bug Something isn't working label May 13, 2023
@dacr
Copy link
Author

dacr commented May 13, 2023

In the KO script, if I change the way the test is started, it runs as expected :

//org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[PiTest].getName))
(new PiTest).execute()

image

@dacr
Copy link
Author

dacr commented May 13, 2023

This has probably a link with the classOf Operation, If I use the REPL I get :

$ scl --dep "org.scalatest::scalatest:3.2.15"
Welcome to Scala 3.2.2 (17.0.6, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.
                                                                                                                                                                                                                                                                                       
scala> import org.scalatest.*, flatspec.*, matchers.*
     | 
                                                                                                                                                                                                                                                                                       
scala> class PiTest extends AnyFlatSpec with should.Matchers {
     |   "pi calculus" should "return a precise enough pi value" in {
     |     math.Pi shouldBe 3.14158d +- 0.001d
     |   }
     | }
     | 
// defined class PiTest
                                                                                                                                                                                                                                                                                       
scala> (new PiTest).execute()
rs$line$2$PiTest:
pi calculus
- should return a precise enough pi value
                                                                                                                                                                                                                                                                                       
scala> org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[PiTest].getName))
*** RUN ABORTED ***
  java.lang.ClassNotFoundException: rs$line$2$PiTest
  at java.base/java.net.URLClassLoader.findClass(URLClassLoader.java:445)

So using the REPL we can see that we got RUN ABORTED because of a ClassNotFoundException issue.

May be a classloader issue ? wrong parent classloader ? Something scalatest is doing that is no longer compatible with the new behavior of scala-cli ?

@dacr
Copy link
Author

dacr commented May 13, 2023

For your information I've automated the execution of most of my scripts using scala-cli, and with the latest release the failure has increased quite a lot :
image

results :

  • 1.0.0-RC1 : 19 scripts KO (which requires some work at my side)
  • 1.0.0-RC2 : 58 scripts KO, amount them
    • 36 RUN ABORTED message
      • all of them are using the org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[TestClass].getName)) :)

So the good news is that 264 scala-cli scripts are running fine, and I didn't see any other issue related to the scala-cli 1.0.0-RC2, all new failures are only related to the RUN ABORTED message.

Depending on your feedback on this issue, I'll keep them as they are or rewrite them to use the test.dep and scala-cli test xxx.sc.

I'll make more checks next week as I've some other specialized scripts I'm using at work...

@Gedochao
Copy link
Contributor

Gedochao commented May 15, 2023

It seems that the reason for this issue is the new script wrapper for Scala 3 scripts introduced in #2033.
The following replicates the problem on earlier versions:

Main.scala

//> using scala  "3.2.2"
//> using dep "org.scalatest::scalatest:3.2.16"

import org.scalatest.*, flatspec.*, matchers.*

class Outer {
  class PiTest extends AnyFlatSpec with should.Matchers {
    "pi calculus" should "return a precise enough pi value" in {
      math.Pi shouldBe 3.14158d +- 0.001d
    }
  }
}

object Main extends App {
  org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[Outer#PiTest].getName))
}

The problem seems to be the loading of the inner class.

scala-cli --cli-version 1.0.0-RC1 Main.scala
# *** RUN ABORTED ***

Needs further investigation on how can we address this.
We need the new wrapper to avoid deadlocks in scripts (see #532 & #1933).

BTW. the example in the REPL (given in #2117 (comment)) is actually replicable in earlier versions of Scala CLI and may be a separate issue.

@tgodzik
Copy link
Member

tgodzik commented May 15, 2023

Ok, so it seems there is no way around it unless we go back to the old behaviour (but that has issues with deadlocks, so I think it's more problematic) or we provide an additional using directive to use object. Neither approach is great but I would stay with the current approach.

In the meantime, I would however suggest using a normal Scala file in this case, sorry about the inconvenience.

We can leave this open to see if more people need this fixed (please react with +1).

@tgodzik tgodzik changed the title got some RUN ABORTED on some scripts with scala-cli 1.0.0-RC2 Allow to use object as a script wrapper May 15, 2023
@SethTisue
Copy link
Contributor

SethTisue commented May 15, 2023

fwiw, I agree that these deadlocks are so common and so pernicious that it's worth prioritizing avoiding them, even if it means compromising a bit elsewhere. the deadlock-proneness of the Scala 2 REPL, before we similarly switched to class-based wrappers in Scala 2.13.2, was reported on scala/bug and elsewhere about a million times over the years, and it was often language newcomers reporting it and getting a bad impression of Scala

(for evidence, see scala/bug#8119 and the various similar/duplicate tickets that are a link or two away)

cc @som-snytt

@MaciejG604
Copy link
Contributor

You can now use --object-wrapper or using objectWrapper as a workaround for the mentioned problems.

@dacr
Copy link
Author

dacr commented May 27, 2023

Thanks for all, I've made all the necessary changes, and the results are good, the only remaining errors I have, are not related to scala-cli anymore, just some work in progress or networking issues :
image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working script-wrapper
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

5 participants