Nimble_publisher and recompiling while running iex or mix

I have made a simple static site using nimble publisher and followed along with the tut from fly.io Crafting your own Static Site Generator using Phoenix · The Phoenix Files

That said I have a simular build() function as from in the tut that just simply compiles the markdown files using earmark and so on with heex and then writes the to a given dir via File.write!

I’ve noticed that if I start a iex session or even run a cowboy server that if I call said build function while in IEx that it will not write anything different from when it started.

If I call recompile after I’ve made a change to a .md file but before I run said build() then it works. I assume this is because the other markdown files are still in memory even though I’ve edited a file, until I recompile that new edit wont be in memory. This is my assumption.

All of that is a non issue when calling from a mix task in a separate terminal session and build can be recalled and works as expected. Only time I see an unexpected behavor is when running the command from in iex or from a genserver that was watching files.

My question is: Is this assumption correct and what can I do about it?

Yes, I think since NimblePublisher is storing the posts as a module attr, you’d need to recompile. I just ran into this issue myself! I was trying to code up a filesystem watcher just as I’m assuming you are.

I solved this for now by using System.cmd("mix", ["site.build"]) instead of Mix.Task.run("site.build").

What if you used rerun/2 instead?

The issue is reenabling tasks was kinda flawed and had been fixed days ago. The following would do:

Enum.each(~w|compile compile.all compile.elixir|, &Mix.Task.reenable/1)
Mix.Task.run("site.build")

On trunk we now have Mix.Task.Compiler.reenable(compilers: compilers) which would do, too.

1 Like

rerun/2 also didn’t work for me. it would work to run the mix tasks, but because of the recompile, it wouldn’t work. . @mudasobwa do you know when that’s going to hit a point release?

v1.19 here is a PR Add `Mix.Tasks.Compile.reenable` by am-kantox · Pull Request #13771 · elixir-lang/elixir · GitHub merged.

1 Like