Interactive / hot reload / live reload workflows

Are there any interactive / live tools for Scala that allow redefining methods without restarting a running program?

For a bit more context, I am thinking of long running programs with lots of state that you don’t want to lose by restarting, like a game engine or data analysis pipeline. You want to make a change to behaviour while preserving current state.

Some language ecosystems have developed interactive / live reload / hot reload techniques that make this possible, so I am curious if this exists for Scala.

(I recently asked this on Discord as well, but I thought posting here may solicit additional responses.)

1 Like

I’m not aware of any tooling to do this easily – others might come up with options, but I haven’t noticed any. It’s a just-plain-challenging problem in the JVM and similar environments.

I’m pretty sure it can be done, by using class loaders and encapsulating the hot-swappable code, but there are inevitably limits to how much you can do there without compromising type safety and the like, since it means that you’re making runtime changes outside the scope of the compiler. I wouldn’t recommend doing it casually: it wants really careful upfront design of what you’re trying to accomplish and where the boundaries are.

I’ve done stuff like this in C#/.NET, a million years ago (when I was building what amounted to a container-ish middleware engine), and I’m reasonably confident it can be done on the JVM. But it’s not a common use case, so I’m not sure there are any libraries to make it straightforward.

I should also note: the same goals can often be accomplished other ways. For example, this sort of warm-update isn’t rare in the Akka world, where the application and its state is distributed across multiple nodes. You don’t swap out methods per se, but you can bring up a node on a newer version of the application, and then migrate actors (which contain the state) over to the new nodes. The system as a whole keeps running, even while individual nodes are gradually updating under the hood. That’s still not simple to do reliably (you need to be careful about protocol evolution), but it’s not a bad approach when always-up is a priority.

2 Likes

When using Metals language server with Bloop there is an option to use hot reload, though I haven’t really looked how well it works recently.

It’s done via Scala Debug Adapter library and introduced in 4.0.0 version: Release v4.0.0 · scalacenter/scala-debug-adapter · GitHub

I haven’t seen any issues related to it, but it might have been because people didn’t actually use it :sweat_smile: If you have any issues let us know! It should just be a case of starting the program within metals and using the reload button (thunderbolt)

3 Likes

I thought the play framework supported hot reloading, and there’s sbt-revolver though the hot reloading part doesn’t seem to be actively supported anymore IIUC. But AFAIK that functionality is usually only meant to be used during development for quick turn-around, not for rolling out new versions in production.