Skip to content

Scala 3 REPL does not appear to be loading resources properly #14281

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

Closed
kapunga opened this issue Jan 17, 2022 · 3 comments · Fixed by #14293
Closed

Scala 3 REPL does not appear to be loading resources properly #14281

kapunga opened this issue Jan 17, 2022 · 3 comments · Fixed by #14293
Milestone

Comments

@kapunga
Copy link
Contributor

kapunga commented Jan 17, 2022

I'm unable to properly load resources from the Scala 3 REPL. Reference project/code is here:
https://github.com/kapunga/whats-up-resources/branches

core/src/main/scala/wur/Main.scala

package wur

object Hello:
  def world: String = io.Source.fromResource("hello.txt").getLines.mkString("\n")

@main def run = println(Hello.world)

core/src/main/resources/hello.txt

Hello,
World!

For this project, I can read them just fine when running the project:

kapunga ~/whats-up-resources (master)$ sbt core/run
[info] welcome to sbt 1.6.1 (AdoptOpenJDK Java 1.8.0_282)
 ...
[info] running wur.run
Hello,
World!
[success] Total time: 4 s, completed Jan 17, 2022 12:12:32 AM

However, when I try and load the resources from the repl, they are not found:

kapunga ~/whats-up-resources (master)$ scala3-repl -classpath core/target/scala-3.1.0/core_3-0.1.0-SNAPSHOT.jar
Welcome to Scala 3.1.0 (11.0.10, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> import wur._

scala> Hello.world
java.io.FileNotFoundException: resource 'hello.txt' was not found in the classpath from the given classloader.
  at scala.io.Source$.fromResource(Source.scala:183)
  at wur.Hello$.world(Main.scala:5)
  ... 34 elided

scala>

The resource file is definitely there:

kapunga ~/whats-up-resources (master)$ jar tf core/target/scala-3.1.0/core_3-0.1.0-SNAPSHOT.jar
META-INF/MANIFEST.MF
wur/
hello.txt
wur/Hello$.class
wur/Hello.class
wur/Hello.tasty
wur/Main$package$.class
wur/Main$package.class
wur/Main$package.tasty
wur/run.class
wur/run.tasty

I've confirmed that this is not an issue with SBT or in Scala 2.13.8, where the resource loads just fine.

@som-snytt
Copy link
Contributor

It works using

io.Source.fromResource("hello.txt", getClass.getClassLoader)

as the default arg is Thread.currentThread().getContextClassLoader()

➜  whats-up-resources git:(master) scala -classpath core/target/scala-3.1.0/core_3-0.1.0-SNAPSHOT.jar
Welcome to Scala 3.1.0 (17.0.1, Java OpenJDK 64-Bit Server VM).
Type in expressions for evaluation. Or try :help.

scala> wur.run
java.io.FileNotFoundException: resource 'hello.txt' was not found in the classpath from the given classloader.
  at scala.io.Source$.fromResource(Source.scala:183)
  at wur.Hello$.world(Main.scala:5)
  at wur.Main$package$.run(Main.scala:7)
  ... 30 elided

scala> Thread.currentThread.getContextClassLoader
val res0: ClassLoader = jdk.internal.loader.ClassLoaders$AppClassLoader@30946e09

scala> getClass.getClassLoader
val res1: ClassLoader = dotty.tools.repl.AbstractFileClassLoader@40bf0e15

scala>

Note that the ScalaClassLoader is on a reducing diet at #14261, where one of the conveniences in Scala 2 was to set and restore the context class loader when running script code.

@kapunga
Copy link
Contributor Author

kapunga commented Jan 17, 2022

Is this something I can set myself as a default? I've trimmed this problem here to the source, but the actual practical repercussions of this problem for me are that I am unable to read configuration files from the SBT console with Typesafe config (and in turn with pure-config).

Edit: Looks like I may be able to work around this in typesafe config.

@som-snytt
Copy link
Contributor

I'm glad there is a workaround for your use case. I don't see a way to automate in Scala 3.

I think it's reasonable for REPL to set context class loader for REPL environment, and I will PR that fix.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants