<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Clojure Q&amp;A - Recent questions tagged code-reloading</title>
<link>https://ask.clojure.org/index.php/tag/code-reloading</link>
<description></description>
<item>
<title>Why does running an uberjar reload namespaces?</title>
<link>https://ask.clojure.org/index.php/13839/why-does-running-an-uberjar-reload-namespaces</link>
<description>&lt;p&gt;Consider this basic project:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(ns whatever.core
  (:gen-class))

(def some-var (doto (System/getenv &quot;FOO&quot;) println))

(defn -main [&amp;amp; args] (println some-var))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;If I make an uberjar out of this, I can see that during compilation, &lt;code&gt;nil&lt;/code&gt; is printed (as expected). However, when I run it with environment variables, I expect nil, but I can see that the correct value is printed and &lt;code&gt;some-var&lt;/code&gt; is actually re-bound! Even though that sounds kind of 'convenient' (for this use-case), I fail to understand why it happens...Since I AOT'ed the namespace, I would expect to have to defer getting the env-var until runtime (e.g. via &lt;code&gt;delay&lt;/code&gt; or something), for this work. Also what are the implications of this double loading? What if I had some very expensive computation in a def, and really wanted to pre-compute it once (and only once)? From what I can tell all def expressions (reachable from &lt;code&gt;-main&lt;/code&gt;) are loaded anew when the program starts, regardless of AOT... Help me understand this please, as it's driving me crazy!&lt;/p&gt;
&lt;p&gt;Many thanks in advance...&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/13839/why-does-running-an-uberjar-reload-namespaces</guid>
<pubDate>Thu, 25 Apr 2024 18:43:53 +0000</pubDate>
</item>
<item>
<title>How to restrict namespaces reloaded by tools.namespace to only dependencies of a root ns?</title>
<link>https://ask.clojure.org/index.php/12724/restrict-namespaces-reloaded-tools-namespace-dependencies</link>
<description>&lt;p&gt;Hey,&lt;/p&gt;
&lt;p&gt;At my organisation, we use Component and would like to start using the reloaded workflow to reload the code more easily.&lt;/p&gt;
&lt;p&gt;The problem we’re facing is that our dev classpath contains too much. In addition to the app proper (loaded by &lt;code&gt;(require 'our-org.repl)&lt;/code&gt;), there are tests and some one-off code: migrations and illustrative REPL sessions that we keep around as documentation.&lt;/p&gt;
&lt;p&gt;Some of these auxiliary namespaces have names that don’t match their location in the filesystem, so they are not loaded correctly by tools.namespace. We’d like to prevent them from being reloaded at all, even though they are present on the classpath.&lt;/p&gt;
&lt;p&gt;We could use &lt;code&gt;clojure.tools.namespace.repl/set-refresh-dirs&lt;/code&gt;, but I feel this is clunky (need to explicitly opt-in for directories) and not quite expressive enough. What we’d really want is to reload only the namespaces that are (possibly indirect) dependencies of &lt;code&gt;our-org.repl&lt;/code&gt;.&lt;/p&gt;
&lt;p&gt;To this end, I’ve monkey-patched tools-namespace:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(defn relevant-deps [deps ns]
  (conj
   (ns.dependency/transitive-dependencies deps ns)
   ns))

(alter-var-root
 #'ns.repl/remove-disabled
 (fn [orig-impl]
   (fn [{:keys [::ns.track/deps] :as tracker}]
     (-&amp;gt; tracker
         (orig-impl)
         (update ::ns.track/unload (partial filter (loaded-libs)))
         (update ::ns.track/load
                 #(set/intersection (set %1) %2)
                 (relevant-deps deps 'our-org.repl))))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This works, but is obviously kludgy. Is there a better way to achieve the same goal? Perhaps the functionality of restricting dependencies could be added to tools.namespace?&lt;/p&gt;
</description>
<category>tools.namespace</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/12724/restrict-namespaces-reloaded-tools-namespace-dependencies</guid>
<pubDate>Fri, 03 Mar 2023 20:17:19 +0000</pubDate>
</item>
<item>
<title>How to call reloadable Clojure from Java?</title>
<link>https://ask.clojure.org/index.php/9431/how-to-call-reloadable-clojure-from-java</link>
<description>&lt;p&gt;Given a Java application already having Clojure's &lt;code&gt;.jar&lt;/code&gt; on classpath. And a file directory &lt;code&gt;/home/me/clojure-extensions/&lt;/code&gt; which contains &lt;code&gt;.clj&lt;/code&gt; files, structured like an usual clojure source directory, which is not on the applications classpath and can not be added to the classpath, because it is configurable and not part of the application.&lt;/p&gt;
&lt;p&gt;How to call the function &lt;code&gt;myns.core/myfn&lt;/code&gt; from Java Code? So that &lt;code&gt;/home/me/clojure/extensions/myns/core.clj&lt;/code&gt; and all dependent files are loaded? So that these files are reloaded if changed when the Java call is invoked again?&lt;/p&gt;
</description>
<category>Compiler</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9431/how-to-call-reloadable-clojure-from-java</guid>
<pubDate>Thu, 09 Jul 2020 12:20:51 +0000</pubDate>
</item>
<item>
<title>Role of application state in a Ring webserver</title>
<link>https://ask.clojure.org/index.php/9165/role-of-application-state-in-a-ring-webserver</link>
<description>&lt;p&gt;Getting started with Ring. I am most familiar with web development in php, in which the php application is booted afresh on every request, which is proxied to by the web server, e.g. apache.&lt;/p&gt;
&lt;p&gt;That of course means it is not possible that the php application itself can maintain any in-memory state (PHP enforcing a &quot;shared nothing&quot; architecture). So,  I do note that in other languages such as java and Clojure the server is a long running process and there is therefore an option for global state.   I aim to stick to 12-factor principles, though, and so I understand application state should be avoided, as if I was programming in php, perhaps.&lt;/p&gt;
&lt;p&gt;I've got initial awareness about the reloaded workflow and such like. Of course, that makes sense in many cases.&lt;/p&gt;
&lt;p&gt;However, since I am aiming that the web app I now write have no global state, would that mean I don't actually need a reloaded workflow (and hence lein ring would be enough - just to reload the code, not the state).&lt;/p&gt;
&lt;p&gt;I wonder if there is anything I am missing.  e.g. what if I change a config variable, maybe that counts as 'state', and would require a reload. Yes, I'll test this myself in due course to find out.&lt;/p&gt;
&lt;p&gt;But just looking for any general tips / discussion on these points to help get off on the right foot.&lt;/p&gt;
</description>
<category>Clojure</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/9165/role-of-application-state-in-a-ring-webserver</guid>
<pubDate>Thu, 19 Mar 2020 17:18:33 +0000</pubDate>
</item>
<item>
<title>Namespace.java: addAlias exception could be more informative</title>
<link>https://ask.clojure.org/index.php/8705/namespace-java-addalias-exception-could-more-informative</link>
<description>&lt;p&gt;In &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/jvm/clojure/lang/Namespace.java#L224-L225&quot;&gt;https://github.com/clojure/clojure/blob/653b8465845a78ef7543e0a250078eea2d56b659/src/jvm/clojure/lang/Namespace.java#L224-L225&lt;/a&gt;, the &lt;code&gt;ns&lt;/code&gt; argument is not included in the exception message.&lt;/p&gt;
&lt;p&gt;This hinders debuggabiliity, especially when the &lt;code&gt;.addAlias&lt;/code&gt; call didn't originate from intentful aliasing, but rather, as a result of re-evaluating a namespace (which is the AST-building strategy that tools such as &lt;a rel=&quot;nofollow&quot; href=&quot;https://github.com/jonase/eastwood&quot;&gt;https://github.com/jonase/eastwood&lt;/a&gt; use).&lt;/p&gt;
&lt;p&gt;Have you considered improving the message?&lt;/p&gt;
</description>
<category>Errors</category>
<guid isPermaLink="true">https://ask.clojure.org/index.php/8705/namespace-java-addalias-exception-could-more-informative</guid>
<pubDate>Thu, 10 Oct 2019 15:55:01 +0000</pubDate>
</item>
</channel>
</rss>