0

In emacs cider repl, now I know how to use clojure.tools.namespace in a leiningen project. However, when I use it on a single clj file which doesn't belong to any project, it seems clojure.tools.namespace doesn't work on the file:

=> #<FileNotFoundException java.io.FileNotFoundException: Could not locate com/foo__init.class or com/foo.clj on classpath: >

I have declared clojure.tools.namespace in .lein/profile.clj and require it in the clj file. How should I make clojure.tools.namespace work on a single clj file?

My profile.clj

{:user
 {:repl-options {:timeout 128000}
  :plugins [;; REPL
            [cider/cider-nrepl "0.9.0-SNAPSHOT"]
            [refactor-nrepl "0.2.2"]

            ;; Application server
            [lein-immutant "2.0.0-SNAPSHOT"]

            ;; Automated testing
            [lein-cloverage "1.0.2"]
            [lein-test-out "0.3.1"]

            ;; Package management
            [lein-ancient "0.6.2"]
            [lein-clojars "0.9.1"]

            ;; Documentation
            [codox "0.6.8"]
            [lein-clojuredocs "1.0.2"]

            ;; Static analysis
            [lein-typed "0.3.4"]
            ;; [jonase/eastwood "0.1.2"]
            [lein-bikeshed "0.1.6"]
            [lein-kibit "0.0.8"]]

  :jvm-opts ["-Dapple.awt.UIElement=true"]

  :dependencies [[org.clojars.gjahad/debug-repl "0.3.3"]
                 [difform "1.1.2"]

                 [spyscope "0.1.4"]
                 [org.clojure/tools.trace "0.7.8"]

                 [org.clojure/tools.namespace "0.2.9"]
                 [im.chit/vinyasa "0.2.0"]
                 [slamhound "1.5.5"]

                 [criterium "0.4.3"]]

  :injections [(require 'spyscope.core)
               (require 'alex-and-georges.debug-repl)
               (require 'com.georgejahad.difform)
               (require '[vinyasa.inject :as inj])
               (inj/inject 'clojure.core '>
                           '[[clojure.repl apropos dir doc find-doc pst source]
                             [clojure.tools.trace trace trace-forms trace-ns trace-vars
                              untrace-ns untrace-vars]
                             [clojure.test run-tests run-all-tests]
                             [clojure.pprint pprint pp]
                             [com.georgejahad.difform difform]
                             [alex-and-georges.debug-repl debug-repl]
                             [vinyasa.pull pull]])]}}
Daniel Wu
  • 371
  • 3
  • 9
  • Could you include the profiles.clj file (you do have an s in the lein.progiles.clj right) and the call to `use` or `require` from the REPL – Arthur Ulfeldt Feb 12 '15 at 19:40
  • 1
    `cider-nrepl` already wraps `tools.namespace`. See the `cider-refresh` command. – Bozhidar Batsov Feb 13 '15 at 18:04
  • I don't think the problem is with tools.nhamespace. The problem is cider doesn't know where to find your source file. This is normally set with the :source-paths option in the project.clj file. By not working with the standard approach of having project directories etc, your really fighting the system and making your life a lot harder than it needs to be. What is your use case for doing this? Perhaps we can suggest a better solution – Tim X Feb 15 '15 at 02:28
  • @TimX My case here is not very serious. Sometimes, I just want to test some simple ideas, which can be wrapped within one CLJ unit, and a complete project is not necessary. Also, I want to run the CLJ unit with CIDER's REPL. – Daniel Wu Feb 17 '15 at 16:20
  • Thought it might be something like that. What I do is have a simple base project called 'scratch' where I do this sort of thing. If it turns out to be an idea with more 'legs' I then migrate it to its own project. This also stops lots of ancillary stuff, like compiled classes and other temp files being scattered around my filesystem – Tim X Feb 18 '15 at 06:39

1 Answers1

1

While this doesn't answer your specific question, it might provide some ideas on alternative workflows which may help.

  • lein-exec This is a lein plugin that lets you write clojure scripts or evaluate clojure expressions on the command line in a similar way to what can be done with shells, python, ruby, perl etc. Of course, startup time is a bit high, but this plugin will help you deal with dependencies etc inside your single clj script file. (there are other possible solutions to improve the startup speed). See this blog post for some examples http://charsequence.blogspot.in/2012/04/scripting-clojure-with-leiningen-2.html

  • A scratch project. I have a fairly generic project called scratch. It just allows me to create a new file within the src directory of the project which I can use for experiments or demonstrations. For example, I have a file called stackoverflow.clj within this scratch project which I use when working out the answer to a clojure question on stack overflow. In fact, I have a lot of individual clj files in this directory, each just representing a simple idea, test, experiment etc. It isn't really a project i.e. I couldn't do a lein run at the root of the project and expect anything meaningful. However, I can go to the source directory in emacs, open a file and then run cider.

Tim X
  • 4,158
  • 1
  • 20
  • 26