2

I can import a script's methods/variables in Ammonite using import $file.MyScript, MyScript._. It works well the first time but what if I wish to import an updated version of MyScript.sc?

Documentation suggests to do

@ repl.sess.load()

Which initialises the REPL, allowing me to import the script again. Sounds good, however, I then get a java.lang.NoClassDefFoundError when I import the script again -- see an example session below.

repl.sess.load() may not be the right functionality here, but then how can I re-import a script after updating it?

I know that there is a functionality to re-run a script after making changes using Ammonite script with a --watch argument, but that is not using the REPL, so it is not a good workaround.

Example session

// As per docs: "If you want to re-load a script, you should use Ammonite's Save/Load Session functionality to 
// sess.save() the session before importing the script, and sess.load()ing to reset the script 
// before re-importing it."
@ repl.sess.save()


@ import $file.MyScript, MyScript._
Compiling /File/Path/MyScript.sc
import $file.$       , MyScript._

@ myWord 
res2: String = "mountain"

@ surroundWord(myWord) 
res3: String = "barwordfoo"

@ repl.sess.load() 
res4: ammonite.repl.api.SessionChanged = SessionChanged(
  HashSet(
    Symbol(notify),
    Symbol(prefixWord),
    Symbol(wait),
    Symbol(equals),
    Symbol(asInstanceOf),
    Symbol(synchronized),
    Symbol(notifyAll),
    Symbol(isInstanceOf),
    Symbol(!=),
    Symbol(res2),
    Symbol(ne),
    Symbol(hashCode),
    Symbol(surroundWord),
    Symbol(myWord),
    Symbol(res3),
    Symbol(==),
...

@ myWord 
cmd5.sc:1: not found: value myWord
val res5 = myWord
           ^
Compilation Failed

@ import $file.MyScript, MyScript._ 
import $file.$       , MyScript._

@ myWord 
java.lang.NoClassDefFoundError: ammonite/$sess/MyScript$
  ammonite.$sess.cmd6$.<clinit>(cmd6.sc:1)
java.lang.ClassNotFoundException: ammonite.$sess.MyScript$
  java.net.URLClassLoader.findClass(URLClassLoader.java:435)
  ammonite.runtime.SpecialClassLoader.findClass(ClassLoaders.scala:241)
  java.lang.ClassLoader.loadClass(ClassLoader.java:589)
  java.lang.ClassLoader.loadClass(ClassLoader.java:522)
  ammonite.$sess.cmd6$.<clinit>(cmd6.sc:1)
mchl_k
  • 314
  • 1
  • 10

1 Answers1

0

Think it's an issue, which I raised here.

I also understand that there's alternative functionality that almost achieves the same outcome and works well. Use

amm --watch --predef MyScript.sc

This launches the Ammonite REPL, with the predef argument loading the script, and the watch argument reloading the script with any saved changes whenever I exit the REPL.

So my typical workflow is

  1. Start scripting a few methods in the file
  2. Launch the REPL and poke the code around to experiment with new stuff
  3. Add that stuff to the file
  4. Re-load the script to check to check that everything runs as expected
  5. Go back to 2.
mchl_k
  • 314
  • 1
  • 10