Search code examples
erlangerlang-shell

How to reload all OTP code when developing an OTP application?


While I'm learning OTP I've been making a lot of changes to the .app and .erl files and re-running my application to see the effect of the changes.

I've tried the following sequence of commands to pick up all my new changes, but it doesn't seem to work:

Compile src files ...

erlc -o ebin src/*.erl

... followed by this is the erlang shell:

application:stop(my_app).
application:unload(my_app).
application:load(my_app).
application:start(my_app).

However, this doesn't seem to work. The only way I have found to work is to exit the erlang shell, recompile the app and then run application:start(my_app)..

Is there an easier way of picking up my changes?


Solution

  • Calling application:load(App) (after stopping and unloading) will reload the .app file but not the modules. As the documentation says: "Note that the function does not load the actual Erlang object code."

    If you were to do an upgrade using releases, you would ship an .appup file that specified which modules to reload on upgrade to the new version (no need to reload all of them if only one or two have changed), but if you're just developing and don't want to stop and restart everything, you'll have to set up your own help functions for reloading code.

    Edit: Since OTP 20 (2017), the interactive Erlang shell now has the lm() function for loading all modules whose .beam files have changed, so there is no need to roll your own utility function for this anymore. See https://erlang.org/doc/man/c.html#lm-0