Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespaces should be reloaded after compilation #41

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

ztellman
Copy link
Collaborator

@ztellman ztellman commented Jan 29, 2025

I noticed that when I was using the newer version of Virgil, I had to manually reload the namespace before the recompiled class was available. Part of the intended workflow is that the updated classes should be immediately available. It appears that you removed this functionality when doing the refactor, but I assume that was unintentional? If not, we can discuss this further.

@alexander-yakushev
Copy link
Collaborator

alexander-yakushev commented Feb 10, 2025

Hi, thanks for bringing this up! Let me explain my reasoning.

Previous versions of Virgil were Leiningen-centric, and the lein-virgil plugin was the primary driver of the recompilation process. Because the customization options of a plugin are very limited, it made sense to provide namespace reloading out-of-the-box because users didn't have the ability to do it on their own.

The new version of Virgil supports both Leiningen and tools.deps and no longer ships a plugin. Users are expected to control Virgil from the REPL which gives much more flexibility. For example, in the basic case of manually calling virgil/compile-java, the user can include the namespace reloading call like this:

(do (virgil/compile-java ["src"])
    (clojure.tools.namespace.repl/refresh-all))

I think this flexibility is beneficial for several reasons:

  • Instead of refresh-all, you can also use refresh or fine-tune the reloading behavior even further. In certain projects where I used Virgil, reloading all namespaces after I recompile a Java class would be inconvenient.
  • There is now also https://github.com/tonsky/clj-reload, an alternative code reloading library that gains popularity. Users can decide to use that instead of tools.namespace.

Regarding the automatic Virgil recompilation workflow with virgil/watch-and-recompile, that function has a :post-hook argument where you can trigger reloading:

(virgil/watch-and-recompile ["src"] :post-hook #(clojure.tools.namespace.repl/refresh-all))

Finally, regarding tools.namespace dependency itself. I'm not a fan of bringing unnecessary transitive dependencies, and it is especially so for tools.namespace itself and also for org.clojure/tools.reader which it brings. Both these dependencies are very popular and are prone to causing version conflicts. So, it is better when the user has to provide such dependency explicitly if they need it, and avoid it if they don't need reloading.

@behrica
Copy link

behrica commented Feb 14, 2025

Just a question here.
I noticed that I needed sometimes re-execute the "import" statement, usually I do that by re-evaluation the "ns" macro (as it contains the 'import')

Is '(clojure.tools.namespace.repl/refresh-all)' taking care of this as well ?

@alexander-yakushev
Copy link
Collaborator

Right, refreshing the namespaces "updates" the class shortname aliases to the newly compiled classes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants