Phoenix live code reload - how to set it up?

I want to auto compile/reload/hotswap the Elixir server side code whenever a file changes. For example, reload the controller, or reload the app inside the business logic/contexts section. How do you setup the config.

Here is how I have setup the config in the dev.exs section inside the config folder:

config :discuss, DiscussWeb.Endpoint,
reloadable_compilers: [:gettext, :phoenix, :elixir],
reloadable_apps: [:ui, :backend]

of course, if I change the code inside one of the controllers, it doesn’t work. How do you do this?

The hotcode reloader is meant for development only. It only watches source files in the project directory, nothing else.

In production there is usually no need to watch sources, as you usually do not replace single source files, but the application as a whole.

If though those files are some data files, then I’m not sure why you want to reload a controller…

Usually there are better means of communication inside the application or with external applications than the local file system.

Hi I mean in dev.

I’ve just discovered that the phoenix code reloader does work… it just doesn’t work when I edit the controller.ex files in vscode…

Have no idea why… It even happens when no plugins are installed.

Its as if vscode saves the file under some format which elixir doesn’t like.

If you wish to reproduce this, its easy. Download vscode, make a new elixir project. try editing some .ex file like UserController.ex, refresh the page, notice that the page doesn’t get updated.

So you want to use it for development? Okay, your intial post read as if you want to use the code reloader in production.

Since the code reloader works for me quite fine, and I do use VScode(ium), I have to ask what operating system you use and if you have installed the proper filesystem tools to make filewatches possible at all. Otherwise the reloader will fallback into a “polling” mode, which scans the filesystem only every few seconds and is inherently slow compared to proper watches.

Having said that, there are still files left the reloader refuses to trigger a recompilation on. IIRC the endpoint and the routes as well as changes at the mix.exs or config/*.exs wont trigger a reload, as the require a restart anyway.

Last but not least, newly added files won’t trigger a reload as well, only files that existed when you started the server.

so I am on ubuntu 18.04 and vscode version 1.35.1, I tested it by modifying the router.ex file. I tried both with ElixirLS plugin enabled and disabled, and I do have inotify-tools installed.

I tried it with atom editor and the atom editor works… not sure what specifically is going on with VScode, it has nothing to do with the plugins.

I also tried changing MIX_ENV=test then running the compile, as suggested by several posts, this did not solve the problem.

Lastly on my system, I have an issue with recompile. I think this recompile error is related to the above problem AND its easier to reproduce

so if you create a new project: mix new foo

Then open that project in vscode file -> open -> foo

Then inside the integrated terminal, do iex -S mix

then do recompile -> answer is :noop (as expected because iex -S mix compiles your files)

BUT!

if you change something in an ex file in the lib folder(for example, inside foo.ex inside the hello world function, change :world to “hi there!”, then save it with vscode

THEN inside the integrated terminal do recompile

it shows :noop! when it should show :ok

This is definitely caused by vscode because if I perform the above steps in atom or in terminal(using gedit as the file editor) it show :ok instead of :noop.

When you run iex -S mix then afaik the reloader won’t be started (AFAIR not even the server), you need to run iex -S mix phx.server to actually start phoenix and related tools…

my mistake, I didn’t communicate what I was trying to say well enough.

I am talking about 2 issues I have. The first issue is the phoenix server does not auto reload the code. But I have a 2nd separate issue. This issue is, inside a normal project(not a phoenix project) if I try the recompile command it show :noop instead of :ok(it shows noop even when it should show ok. i.e. it shows noop even while ex files have been edited). I think the second issue is related to the first issue. I think the first issue is caused by the second issue. I think if the 2nd issue was resolved, the first issue would be resolved. Even though the 1st issue and the 2nd issue are completely separate, independent issues that I have.

I have never experienced such issues. Are you strictly sure that the code you edit and the iex session you start are in the same project?

Hello, I have a similar problem of code reloading : when I edit js or eex files, the reload works, but not when I edit a controller. Any idea ?

I am using Sublime Text and Mac OS

How does your live reload configuration looks like? per default it does only relead on static assets and templates.

That was it, sorry for the newbish question, I thought watching the controllers was a default (didn’t it use to ?) so I did not check the docs.

Thank you !

Does phoenix “know” that it must wait for the new code to be loaded before sending the page reload message ?