Fork me on GitHub
#dev-tooling
<
2024-04-20
>
mafcocinco14:04:46

Question about clojure.tools.namespace . In past projects, I have used ns-tracker to reload namespaces on a locally running server. This worked well but I would like to transition to tools.namespace as it seems to be more robust in its handling of dependencies and namespace reloading. However, tools.namespace.repl/refresh does not work when I run it in a separate thread, which makes it challenging to use in combination with ns-tracker. The specific error I receive is this:

Exception in thread "Thread-0" java.lang.IllegalStateException: Can't change/establish root binding of: *ns* with set
There could be something I'm doing incorrectly when setting this up. I'm not particularly wedded to ns-tracker. Primarily, I would like to know what the proper way to do hot reloading of server code (running on a local machine) with clojure.tools.namespace?

vemv14:04:18

It's more idiomatic to stop using ns-tracker and use t.n in conjunction with Component to also manage your local server or other such 'components'

vemv14:04:59

Here's a nice template https://github.com/stuartsierra/reloaded (even if you weren't using Lein, you can just peek at the files)

mafcocinco15:04:24

We are using polylith but I’m sure we can modify to make it work.

vemv15:04:57

You can use poly with Component, or Integrant. t.n plays well with both

thumbsup_all 1
vemv15:04:40

Lastly, generally, don't do any sort of code (re)loading from a parallel thread - require and friends are thread-unsafe, as of today

thumbsup_all 1
mafcocinco15:04:03

So is it best practice now to just move off of ns-tracker?

vemv15:04:33

I'd say it's a pretty old-school tool, very rare to see

thumbsup_all 1
vemv15:04:33

https://github.com/tonsky/clj-reload aspires to be tools.namespace's successor , I'd say it's doing a good job at that and it already has CIDER support (personally I keep using t.n but I'm a cranky dev)

1