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

ScalaCLI resourceDir doesn't work with package #509

Closed
lwronski opened this issue Dec 17, 2021 · 2 comments · Fixed by #519
Closed

ScalaCLI resourceDir doesn't work with package #509

lwronski opened this issue Dec 17, 2021 · 2 comments · Fixed by #519
Assignees

Comments

@lwronski
Copy link
Contributor

Thanks for @teigen detailed description of the problem
https://gist.github.com/teigen/16059fa6413a023d939d7ed35925a613

A little project demonstrating problems with resourceDir.

16:28 λ tree
.
├── Test.scala
└── resources
    └── data.txt

1 directory, 2 files
16:28 λ cat Test.scala 
// using scala 3.0.2
// using resourceDir "./resources"
@main def main = 
  println(Thread.currentThread().getContextClassLoader.getResource("data.txt"))
16:28 λ cat resources/data.txt 
Hello!

Running it works great :-)

16:29 λ scala-cli run .
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
file:/Users/jteigen/Development/cli-resource/resources/data.txt
16:29 λ scala-cli package .
Compiling project (Scala 3.0.2, JVM)
Compiled project (Scala 3.0.2, JVM)
Wrote app, run it with
  ./app
16:29 λ ./app
null

Running the packaged version does not :-(

16:29 λ jar xf app coursier/bootstrap/launcher/jars/app-content.jar

Inspecting the packaged jar-file shows the file is missing

16:30 λ jar tf coursier/bootstrap/launcher/jars/app-content.jar 
META-INF/MANIFEST.MF
Test$package.tasty
main.tasty
main.class
Test$package$.class
Test$package.class

Lets export to sbt!

16:30 λ scala-cli export --sbt .
16:30 λ cd dest
16:30 λ cat build.sbt 

// Scala CLI doesn't distinguish main and test sources for now.
Test / sources ++= (Compile / sources).value

scalaVersion := "3.0.2"






Compile / unmanagedClasspath ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))
Runtime / unmanagedClasspath ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))




16:30 λ sbt run
[info] welcome to sbt 1.5.5 (Homebrew Java 11.0.12)
[info] loading global plugins from /Users/jteigen/.sbt/1.0/plugins
[info] loading project definition from /Users/jteigen/Development/cli-resource/dest/project
[info] loading settings for project dest from build.sbt ...
[info] set current project to dest (in build file:/Users/jteigen/Development/cli-resource/dest/)
[info] compiling 1 Scala source to /Users/jteigen/Development/cli-resource/dest/target/scala-3.0.2/classes ...
[info] running main 
file:/Users/jteigen/Development/cli-resource/dest/target/bg-jobs/sbt_a8cc4955/target/0020f180/a4662cdd/resources/data.txt
[success] Total time: 4 s, completed Dec 17, 2021, 4:30:56 PM

Running it works. But just like with scala-cli run it find the file on the filesystem, not in the jar.
Inspecting the jar reveals why

16:30 λ jar tf target/scala-3.0.2/dest_3-0.1.0-SNAPSHOT.jar 
META-INF/MANIFEST.MF
Test$package$.class
Test$package.class
Test$package.tasty
main.class
main.tasty

If we make some minor adjustments to build.sbt to match what I thought resourceDir did
(https://www.scala-sbt.org/1.x/docs/Howto-Customizing-Paths.html#Add+an+additional+resource+directory)

16:31 λ vim build.sbt 
16:31 λ cat build.sbt 

// Scala CLI doesn't distinguish main and test sources for now.
Test / sources ++= (Compile / sources).value

scalaVersion := "3.0.2"

Compile / unmanagedResourceDirectories ++= Seq(file("/Users/jteigen/Development/cli-resource/resources"))

Running shows that the resource file is now resolved from the jar-file, not the filesystem.

16:32 λ sbt run
[info] welcome to sbt 1.5.5 (Homebrew Java 11.0.12)
[info] loading global plugins from /Users/jteigen/.sbt/1.0/plugins
[info] loading project definition from /Users/jteigen/Development/cli-resource/dest/project
[info] loading settings for project dest from build.sbt ...
[info] set current project to dest (in build file:/Users/jteigen/Development/cli-resource/dest/)
[info] running main 
jar:file:/Users/jteigen/Development/cli-resource/dest/target/bg-jobs/sbt_c7561821/job-1/target/4ed69241/c326b934/dest_3-0.1.0-SNAPSHOT.jar!/data.txt
[success] Total time: 1 s, completed Dec 17, 2021, 4:32:14 PM

As our jar file contains the resource-file

16:32 λ jar tf target/scala-3.0.2/dest_3-0.1.0-SNAPSHOT.jar 
META-INF/MANIFEST.MF
Test$package$.class
Test$package.class
Test$package.tasty
data.txt
main.class
main.tasty
@lwronski lwronski self-assigned this Dec 17, 2021
@lwronski
Copy link
Contributor Author

lwronski commented Dec 21, 2021

ScalaCLI by default copy all files from build.output directory to bootstrap file - link

Build.output is the path to classesDir in bloop. And there is problem, that bloop not copy resource files to clasessDir. Therefore resourceDir doesn't work with the package command.

I reported this problem to the bloop repository - #1631

@lwronski lwronski linked a pull request Jan 10, 2022 that will close this issue
@lwronski
Copy link
Contributor Author

Closed via #519.

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

Successfully merging a pull request may close this issue.

1 participant