### Namespace Declaration Example Source: https://github.com/clojure/tools.namespace/blob/master/README.md An example of a Clojure namespace declaration, showing how to require other namespaces with aliases. This is relevant for understanding how dependencies are managed and how aliases are restored by tools.namespace after errors. ```clojure (ns dev (:require [com.example.foo :as foo] [com.example.bar :as bar] [clojure.tools.namespace.repl :as tns])) ``` -------------------------------- ### Managed Application Lifecycle for Reloading Source: https://github.com/clojure/tools.namespace/blob/master/README.md Provides a workflow for safely restarting an application using `refresh` and custom `start`/`stop` functions. The `start` function initializes resources and state, while the `stop` function cleans them up. This cycle allows for quick iteration by reloading code without restarting the entire JVM. ```clojure ; Define start and stop functions (implementation details omitted) ; (defn start-my-app [] ...) ; (defn stop-my-app [app-state] ...) ; Workflow: user=> (require '[clojure.tools.namespace.repl :refer [refresh]]) user=> (refresh) user=> (def my-app (start-my-app)) ; After modifying files: user=> (stop-my-app my-app) user=> (refresh) user=> (def my-app (start-my-app)) ``` -------------------------------- ### Clojure CLJS Dependency Graph Generation Source: https://github.com/clojure/tools.namespace/wiki/Snippets Generates a dependency graph for CLJS namespaces by scanning directories and tracking dependencies. It requires the clojure.tools.namespace library and takes a directory path and a starting namespace as input, returning a nested map representing the dependency structure. ```clojure (ns scratch.xy (:require [clojure.tools.namespace.find :as ctnf] [clojure.tools.namespace.dir :as ctnd] [clojure.tools.namespace.dependency :as ctndeps] [clojure.tools.namespace.track :as ctnt])) (def cljs-graph (::ctnt/deps (ctnd/scan-dirs (ctnt/tracker) [(io/file "src" "cljs")] {:platform ctnf/cljs}))) (defn deps-graph [graph ns] (let [deps (ctndeps/immediate-dependencies graph ns)] {ns (mapv #(deps-graph graph %) deps)})) (deps-graph cljs-graph 'your.ns.core) ``` -------------------------------- ### API Documentation for tools.namespace Source: https://github.com/clojure/tools.namespace/blob/master/README.md This entry provides a consolidated view of the tools.namespace API, focusing on the core functionality for reloading code. It details the primary 'refresh' function, its usage, and the underlying components that manage namespace dependencies and reloading. ```APIDOC clojure.tools.namespace.repl: refresh - Reloads namespaces based on dependency graph. - Scans classpath for Clojure source files, reads ns declarations, and builds a dependency graph. - Reloads changed namespaces and their dependents in the correct order. - Usage Example: (require '[clojure.tools.namespace.repl :refer [refresh]]) (refresh) - Output Example: :reloading (com.example.util com.example.app com.example.app-test) :ok Modules: - clojure.tools.namespace.parse: Parses namespace declarations and :require/:use clauses from Clojure source files. - clojure.tools.namespace.find: Utilities to search for Clojure namespaces on the filesystem and JAR files. - clojure.tools.namespace.dependency: Generic dependency graph data structure. - clojure.tools.namespace.track: Namespace dependency tracker. - clojure.tools.namespace.file: File-reader extension to tracker. - clojure.tools.namespace.dir: Directory-scanner extension to tracker. - clojure.tools.namespace.reload: Namespace-reloading extension to tracker. - clojure.tools.namespace.move: Utilities for moving and renaming Clojure namespaces (ALPHA). ClojureScript Support: - Namespaces usable from both Clojure(JVM) and ClojureScript (.cljc): dependency, track, parse. - Namespaces usable on Clojure(JVM) only but can analyze Clojure(JVM) and ClojureScript source: file, dir, find. - Namespaces still Clojure(JVM) only: reload, repl, move. - Functions may take an optional "platform" argument ('clj' or 'cljs'). ``` -------------------------------- ### Clojure: Restarting Application with Refresh Source: https://github.com/clojure/tools.namespace/blob/master/README.md Demonstrates a common pattern for restarting an application after a code refresh in Clojure. It highlights a potential issue where a function relying on old namespace state might fail after `refresh` due to the namespace being dropped. ```clojure (def my-app nil) (defn restart [] (stop-my-app my-app) (refresh) (alter-var-root #'my-app (constantly (start-my-app)))) ``` -------------------------------- ### Clojure: Using :after option with refresh Source: https://github.com/clojure/tools.namespace/blob/master/README.md Shows the correct way to execute code after a successful namespace reload using the `:after` option with `refresh`. This option takes a fully namespace-qualified symbol pointing to a function to be run post-reload. ```clojure (def my-app nil) (defn start [] (alter-var-root #'my-app (constantly (start-my-app)))) (defn restart [] (stop-my-app my-app) (refresh :after 'dev/start)) ``` -------------------------------- ### Handle Refresh Errors with tools.namespace Source: https://github.com/clojure/tools.namespace/blob/master/README.md Demonstrates how tools.namespace reports errors during namespace reloading. When an exception occurs, refresh stops, identifies the problematic namespace, and returns the exception. The stacktrace can be printed using `clojure.repl/pst`. ```clojure user=> (refresh) :reloading (com.example.app com.example.app-test) :error-while-loading com.example.app # ``` -------------------------------- ### Avoid Global State for Reload Safety Source: https://github.com/clojure/tools.namespace/blob/master/README.md Illustrates the anti-pattern of using global Vars for application state and the recommended approach of storing state locally within an application object. This pattern ensures that state is managed by a constructor function, making it easier to reinitialize after code reloads. ```clojure ; Anti-pattern: Global state (def state-of-world (ref {})) (def object-handle (atom nil)) ; Recommended: Local state managed by a constructor (defn create-application [] {:state-of-world (ref {}) :object-handle (atom nil)}) ``` -------------------------------- ### Clojure CLI/deps.edn Dependency Source: https://github.com/clojure/tools.namespace/blob/master/README.md Specifies the dependency for the tools.namespace library when using the Clojure CLI or `deps.edn` configuration. This entry provides the Maven coordinates for the library. ```clojure org.clojure/tools.namespace {:mvn/version "1.5.0"} ``` -------------------------------- ### Print Stacktrace of Last Exception Source: https://github.com/clojure/tools.namespace/blob/master/README.md Shows how to print the full stacktrace of the last encountered exception in Clojure. This is useful for debugging errors reported by tools.namespace or other operations. ```clojure user=> (clojure.repl/pst) IllegalArgumentException Parameter declaration cond should be a vector clojure.core/assert-valid-fdecl (core.clj:6567) clojure.core/sigs (core.clj:220) clojure.core/defn (core.clj:294) clojure.lang.Var.invoke (Var.java:427) ... ``` -------------------------------- ### Leiningen Dependency for tools.namespace Source: https://github.com/clojure/tools.namespace/blob/master/README.md Provides the Leiningen dependency information required to include the tools.namespace library in a Clojure project. It specifies the artifact coordinates and the repository where snapshot versions can be found. ```clojure :dependencies [[org.clojure/tools.namespace "1.5.1-SNAPSHOT"]] :repositories [["sonatype-oss-public" "https://oss.sonatype.org/content/groups/public/"]] ``` -------------------------------- ### Configure JVM PermGen Space (JDK 1.7 and before) Source: https://github.com/clojure/tools.namespace/blob/master/README.md Mitigate OutOfMemoryError: PermGen space by increasing the Permanent Generation size for older JVMs. This involves adding a command-line argument during startup. ```shell -XX:MaxPermSize= ``` ```shell java -XX:+PrintFlagsFinal | grep MaxPermSize ``` -------------------------------- ### Reload Clojure Namespaces with Refresh Source: https://github.com/clojure/tools.namespace/blob/master/README.md Demonstrates the basic usage of the `refresh` function to reload namespaces that have changed. It shows the typical REPL output during a reload operation, indicating which namespaces are being reloaded. This function reloads only changed namespaces in dependency order after unloading old definitions. ```clojure user=> (require '[clojure.tools.namespace.repl :refer [refresh]]) user=> (refresh) :reloading (com.example.app com.example.app-test) :ok ``` -------------------------------- ### Leiningen Dependency Source: https://github.com/clojure/tools.namespace/blob/master/README.md Specifies the dependency for the tools.namespace library when using Leiningen for project management. This entry provides the standard Leiningen dependency format. ```clojure [org.clojure/tools.namespace "1.5.0"] ``` -------------------------------- ### Resume Reloading After Fixing Errors Source: https://github.com/clojure/tools.namespace/blob/master/README.md Illustrates the process of resuming namespace reloading after an error has been fixed. After correcting the issue, calling `refresh` again allows tools.namespace to continue reloading from where it left off. ```clojure ; After fixing the error, call refresh again user=> (refresh) ``` -------------------------------- ### Clojure: Protocol Implementation Error after Refresh Source: https://github.com/clojure/tools.namespace/blob/master/README.md Explains the issue where reloading namespaces containing protocols can lead to `IllegalArgumentException` if old instances of records implementing those protocols are still in use. The JVM treats old and new protocol versions as distinct. ```clojure (ns com.example.foo) (defprotocol IFoo (foo [this])) (defrecord FooRecord [] IFoo (foo [this] nil)) user=> (def my-foo (->FooRecord)) user=> (clojure.tools.namespace.repl/refresh) user=> (foo my-foo) IllegalArgumentException No implementation of method: :foo of protocol: #'com.example.foo/IFoo found for class: com.example.foo.FooRecord clojure.core/-cache-protocol-fn (core_deftype.clj:527) ``` -------------------------------- ### Maven Dependency Source: https://github.com/clojure/tools.namespace/blob/master/README.md Specifies the dependency for the tools.namespace library when using Maven for project management. This entry provides the XML dependency declaration for Maven projects. ```xml org.clojure tools.namespace 1.5.0 ``` -------------------------------- ### Clojure: Recreating Namespace Alias after Refresh Source: https://github.com/clojure/tools.namespace/blob/master/README.md Illustrates how namespace aliases created at the REPL can continue to refer to old namespaces after a `refresh`. It shows the error encountered when trying to recreate an alias and the correct method to remove the old alias before creating a new one. ```clojure user=> (require '[com.example.foo :as foo]) user=> foo/bar user=> (refresh) :reloading (com.example.foo) :ok user=> foo/bar ; this is the *old* foo/bar user=> (require '[com.example.foo :as foo]) IllegalStateException Alias foo already exists in namespace user, aliasing com.example.foo clojure.lang.Namespace.addAlias (Namespace.java:224) user=> (ns-unalias *ns* 'foo) nil user=> (alias 'foo 'com.example.foo) ``` -------------------------------- ### Clojure: Multimethod Preference Conflict Warning Source: https://github.com/clojure/tools.namespace/blob/master/README.md Addresses the potential for `java.lang.IllegalStateException: Preference conflict in multimethod` when modifying `prefer-method` calls and reloading the containing namespace. The workaround involves calling `remove-method` before reloading. ```clojure ; Example scenario: Modifying prefer-method and reloading namespace ; If a preference conflict arises after reload, call remove-method first. ; tools.namespace cannot detect this situation automatically. ``` -------------------------------- ### Disable Namespace Reloading in Clojure Source: https://github.com/clojure/tools.namespace/blob/master/README.md Prevent the `refresh` function from automatically un/reloading specific namespaces, useful for maintaining state in project REPLs or scratch namespaces during development. Use sparingly and ensure code is reload-safe. ```clojure ;; In clojure.tools.namespace.repl disable-unload! disable-reload! ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.