scala-cli icon indicating copy to clipboard operation
scala-cli copied to clipboard

`scala-cli compile -d` always clears the directory

Open bishabosha opened this issue 3 years ago • 1 comments

Version(s) 0.1.16

Describe the bug for backwards compatibility, using the -d flag should not clear the directory before adding classes.

Instead, each compilation to the directory clears it, this makes it impossible to incrementally add classes to a directory.

To Reproduce

In this example, we simulate three compilation runs of "artefacts" that output to the same directory, the idea is that at runtime the lib classes should still exist, but they do not in this case:

// lib.scala
object lib:
  def foo = 23
// utils.scala
object utils:
  def bar = lib.foo
// app.scala
@main def app = println(utils.bar)
> mkdir out
> scala compile -d out lib.scala
> scala compile -d out -cp out utils.scala
> scala -cp out app.scala
Compiling project (Scala 3.2.0, JVM)
Compiled project (Scala 3.2.0, JVM)
Exception in thread "main" java.lang.NoClassDefFoundError: lib$
	at utils$.bar(Macros.scala:5)
	at App$package$.app(App.scala:3)
	at app.main(App.scala:3)
Caused by: java.lang.ClassNotFoundException: lib$
	at java.net.URLClassLoader.findClass(URLClassLoader.java:387)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:418)
	at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:352)
	at java.lang.ClassLoader.loadClass(ClassLoader.java:351)
	... 3 more

Expected behaviour no crash - program prints 23

bishabosha avatar Oct 21 '22 11:10 bishabosha

maybe this is not an important goal because the old scala command never had a compile subcommand. However if in the future it was planned to have scalac underneath use scala-cli then this issue would be relevant

bishabosha avatar Oct 21 '22 11:10 bishabosha