Unable to resolve symbol when running code using Leiningen - clojure

I'm using Leiningen (for the first time) to manage an app my writing. So far I've defined the project dependencies, installed the deps in the project lib directory, and I've defined a function. When I run lein repl from the project root and then call the function I've defined, I get the error unable to resolve symbol. Anyone know what I'm doing wrong and how to correctly run my app via Leiningen? Thanks.

from the leiningen repl you will have to switch to the namespace your function was defined in with the in-ns macro.
(in-ns 'myproject.core)
then the function should be available
you could also use that namespace from the repl to include it in the default (user) namespace.
(use 'myproject.core)
after that you may want to consider looking into the lein run, lein uberjar, and lein jar leiningen tasks.

In my projects, for a core.clj file which contains a namespace defined thus:
(ns my-project.core)
... I set the :main key in Leiningen's defproject map in project.clj:
(defproject my-project "1.0.0-SNAPSHOT"
:description "My project description"
:dependencies [[org.clojure/clojure "1.2.1"]]
:main my-project.core)
So when I run lein repl, my core namespace is automatically loaded, and I see this:
mac:my-project scott$ lein repl
REPL started; server listening on localhost:31515.
my-project.core=>

Related

Leiningen wont exclude namespaces from uberjar

I have a project where I want certain parts of the code to be able to run on a local environment, and other parts which depend on libraries that only run on a remote environment. E.g.
src/app/core.clj <- can run anywhere
src/app/sandbox/remote_only.clj <- depnds on libs that only function in remote environ
where src/app/core.clj is
(ns app.core
(:gen-class))
(defn -main [] (println "Hello, World!"))
and src/app/sandbox/remote_only.clj is
(ns app.sandbox.remote-only
(:require
[uncomplicate.commons.core :refer [with-release]]
[uncomplicate.neanderthal
[native :refer [dv dge]]
[core :refer [mv mv!]]]))
I want to be able to uberjar and run the code located in core.clj on my local machine without pulling in remote_only.clj which will cause the program to fail locally. According to the docs, Leiningen should be able to accomplish this using profiles and uberjar exclusions e.g.:
(defproject app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]]
:profiles {:uberjar {:main app.core
:init-ns app.core
:aot :all}
:remote {:init-ns app.sandbox.remote_only
:dependencies [[uncomplicate/neanderthal "0.43.1"]]}} ; these deps will fail locally
:uberjar-exclusions [#".*sandbox.*"]
:repl-options {:init-ns app.core})
compiling the uberjar here will result in:
❯ lein uberjar
Compiling app.core
Compiling app.sandbox.remote-only
Syntax error macroexpanding at (remote_only.clj:1:1).
Execution error (FileNotFoundException) at app.sandbox.remote-only/loading (remote_only.clj:1).
Could not locate uncomplicate/commons/core__init.class, uncomplicate/commons/core.clj or uncomplicate/commons/core.cljc on classpath.
So, explicitly its trying to compile the class that was specifically excluded.
I thought that this kind of problem could be avoided by removing the :all tag from :aot compilation. E.g.:
(defproject app "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.10.1"]]
:profiles {:uberjar {:main app.core
:init-ns app.core
:aot [app.core]}
:remote {:init-ns app.sandbox.remote_only
:dependencies [[uncomplicate/neanderthal "0.43.1"]]}} ; these deps will fail locally
:uberjar-exclusions [#".*sandbox.*"]
:repl-options {:init-ns app.core})
which causes other problems:
❯ lein uberjar
Compiling app.core
Created /Users/warrenronsiek/Projects/app/target/app-0.1.0-SNAPSHOT.jar
Created /Users/warrenronsiek/Projects/app/target/app-0.1.0-SNAPSHOT-standalone.jar
~/Projects/app 5s 15:47:37
❯ java -jar ./target/app-0.1.0-SNAPSHOT.jar
Exception in thread "main" java.lang.NoClassDefFoundError: clojure/lang/Var
I've tried playing around with all kinds of :exclusions, :jar-exclusions and regexes. Variations of this question have been asked multiple times (1, 2) Nothing works.
How do I get Leiningen to compile uberjars and ignore the specified file(s)?
You are on the right track with changing :aot :all to :aot [app.core] but when you tried to run the JAR, you are running the library version instead of the whole application version:
java -jar ./target/app-0.1.0-SNAPSHOT.jar
That doesn't include Clojure or any dependencies. You want:
java -jar ./target/app-0.1.0-SNAPSHOT-standalone.jar
I haven't tried this particular setup with the "exclusions" features.
One workaround would be to create sub-projects for the "local" & "remote" parts of your app, which could then be used by a higher-level "total" project.
app
app-local
app-remote
where each of the 3 is a separate Lein project. app-local and app-remote could be developed in isolation. The top-level app project then uses has the 2 sub-projects as dependencies (and therefore only works in the remote/full environment).
The Lein feature checkouts allows you to develop in all 3 repos simultaneously, which is a huge help. It is much faster than the alternative of using lein install, which is slow, manual, repetitive, and error-prone.
P.S. Did you experiment with using lein jar instead of lein uberjar?

Why is clojure deps adding `/main/clojure` to classpath when using `:local/root`?

I am trying to link a local project to another local project that is under development using tools.deps :local/root option in deps.edn. It isn't working. I can't require the library's namespaces, and the path is correct.
The deps.edn entry looks like:
{:paths ["src" "resources"]
:deps {...
...
...
mylib {:local/root "../../../mylib"}
}}
The classpath that is generated, however, is incorrect:
../../../mylib/src/main/clojure
For some reason clojure/main is added on to the classpath for this library, and I don't know why. Then when I run clj to start the repl I am unable to load the library, and I get the FileNotFoundException.
To test that the addition of main/clojure to the lib path is the problem, I manually removed that part of the path in the cache file in .cpcache directory and was able to require the library namespaces once I'd removed clojure/main.
Does anyone know where the main/clojure is coming from and how I can stop it being added?
UPDATE
I did a new test that leads me to think this has something to do with the use of project.clj as opposed to deps.edn in the target project. In the test I had a project b with a deps.edn like this:
{:deps {a-proj {:local/root "../a-proj"}}}
While a-proj had a project.clj like this:
(defproject a-proj "0.1.0-SNAPSHOT"
:description "blah"
:url "http://example.com/FIXME"
:license {:name "The MIT Licence"
:url "https://opensource.org/licenses/MIT"}
:source-paths ["src"]
:dependencies [])
I then ran clj -Sforece -Spath and got:
~/Projects/b > clj -Sforce -Spath ethan at rembrandt.local
src:/Users/ethan/.m2/repository/org/clojure/clojure/1.10.0/clojure-1.10.0.jar:/Users/ethan/Projects/b/src:/Users/ethan/Projects/a-proj/src/main/clojure:/Users/ethan/.m2/repository/org/clojure/spec.alpha/0.2.176/spec.alpha-0.2.176.jar:/Users/ethan/.m2/repository/org/clojure/core.specs.alpha/0.2.44/core.specs.alpha-0.2.44.jar
You can see there that the /main/clojure path has been aded on. When I instead use an essentially deps.edn in a-proj:
{:deps {}}
I get the following path, which seems correct:
~/Projects/b > clj -Sforce -Spath
src:/Users/ethan/.m2/repository/org/clojure/clojure/1.10.0/clojure-1.10.0.jar:/Users/ethan/Projects/b/../a-proj/src:/Users/ethan/.m2/repository/org/clojure/spec.alpha/0.2.176/spec.alpha-0.2.176.jar:/Users/ethan/.m2/repository/org/clojure/core.specs.alpha/0.2.44/core.specs.alpha-0.2.44.jar
My deps.edn in ~/.clojure has only this:
{
:aliases {
:new {:extra-deps {seancorfield/clj-new {:mvn/version "0.9.0"}}
:main-opts ["-m" "clj-new.create"]}
:deps {:extra-deps {org.clojure/tools.deps.alpha {:mvn/version "0.5.435"}}}
:test {:extra-paths ["test"]}
}
}
project.clj is not supported as a project type by the Clojure CLI via deps.edn.

How do I create a Clojure application with lein uberjar if I have a dependency in checkouts?

I have a Clojure application that is using a library which is symlinked from inside the "checkouts" directory.
This lets me work on both the app and the library at the same time. And lein knows how to compile and run the program without any problems.
But I want to make a standalone with lein uberjar, and it's complaining
Caused by: java.io.FileNotFoundException: Could not locate mylib/core__init.class, mylib/core.clj or mylib/core.cljc on classpath.
I assume that that is because mylib isn't mentioned in my project.clj file. It isn't, precisely because I want to use the version of mylib symlinked inside "checkouts".
But the uberjar command doesn't seem to be able to see it.
How can I solve this?
You can accomplish this by installing mylib in your local repository (~/.m2/repository).
Run lein install in the dependent project to install it in your local repository.
Add the project to :dependencies: in project.clj:
[mylib "version"]
Run lein uberjar in the main project.
The project will find the jar in your local repository.
/Edit
If you want to develop two libraries at the same time you can use a checkouts folder where checkouts contains a symlink to the dependent library.
mkdir checkouts
ln -nfs full-path-other-lib-dir full-path-checkouts-dir
Now changes in other-lib are immediately available in the main project.
See [https://github.com/technomancy/leiningen/blob/master/doc/TUTORIAL.md#checkout-dependencies](the Leiningen checkouts documentation).
OK, it appears that the "checkouts" feature of lein works with lein test and lein run but not with lein uberjar.
I added the following to the local source code of a lib tupelo.core:
(def dummy-sample-data "Bogus!")
In the consuming project demo.core we can access the new Var:
(ns demo.core
(:use tupelo.core tupelo.test))
(defn -main [& args]
(println :foo-enter)
(spyx dummy-sample-data)
(println :foo-leave))
lein run produces:
:foo-enter
dummy-sample-data => "Bogus!"
:foo-leave
project.clj remains unchanged:
(defproject demo "0.1.0-SNAPSHOT"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [
[criterium "0.4.5"]
[org.clojure/math.combinatorics "0.1.6"]
[org.clojure/clojure "1.10.1"]
[prismatic/schema "1.1.12"]
[tupelo "0.9.201"]
<snip>
where "0.9.201" is the latest version of tupelo on Clojars. Checkouts looks like:
~/expr/demo > ls -ldF checkouts/*
lrwxrwxrwx 1 alan alan 17 May 12 13:57 checkouts/tupelo -> /home/alan/tupelo/
but uberjar fails:
~/expr/demo > lein clean ; lein uberjar
Compiling demo.core
Syntax error compiling at (demo/core.clj:6:3).
Syntax error compiling at (demo/core.clj:6:3).
Unable to resolve symbol: dummy-sample-data in this context
Full report at:
/tmp/clojure-10416346559924917196.edn
Compilation failed: Subprocess failed
Options
If you are wanting to deploy an application, you have 2 options:
If the lib exists on Clojars or Maven, you should probably deploy a release there before making a uberjar. If the lib is not ready for Clojars or Maven, maybe you aren't ready to use it in a uberjar...?
Simply copy the source tree of mylib (or use a symlink!) under the ./src dir in your project. You'll also have to copy in the dependencies into myproj/project.clj. You are effectively merging myproj and mylib, at least temporarily with this approach. If the are coupled tightly enough to require co-development, perhaps it should be a permanent merge?

Using Medley.core lib in clojure repl

Is there a way to use the medley library directly in clojure's repl without adding it to the :dependencies in the project.clj file? Something like (use 'medley.core)?
You could add pomegranate to your $HOME/.lein/profiles.clj file
{:user {
:dependencies [[com.cemerick/pomegranate "0.3.0"]]
}}
Then, from the repl, you can require pomegranate functions with:
(use '[cemerick.pomegranate :only (add-dependencies)])
Then add your dependency like this:
(add-dependencies
:coordinates '[[incanter "1.2.3"]]
:repositories {"clojars" "http://clojars.org/repo"}))
Most of the time, Clojure libraries do use a maven compatible repository named clojars, hence the extra repository, but if the library is on the maven central, no need for the extra repository definition.
For example, bootstrap-clj is on central, so in that case, the below is enough:
(add-dependencies
:coordinates '[[com.github.sebhoss/bootstrap-clj "2.0.0"]])
Voila.

How to launch a clojure repl with access to a jar

I'm new to clojure. I have a jar file I want to play with in a clojure repl, but I've failed to do so with leiningen 2.
I have tried placing the jar file in src/myjar.jar and also in src/org/mydomain/myjar.jar
When I run lein repl I get errors stating that leiningen can not find my artifact, and a reference to a page about repeatability which I do not understand.
Here is my project.clj (with the real name of myjar)
(defproject cljliveordead "0.1.0-SNAPSHOT"
:description "FIXME: write description"
:url "http://example.com/FIXME"
:license {:name "Eclipse Public License"
:url "http://www.eclipse.org/legal/epl-v10.html"}
:dependencies [[org.clojure/clojure "1.3.0"]
[org.allen.temporalintervalrelationships/time "0.2" :extension "jar"]])
You can use local jars using the lein-localrepo plugin. Add this line to your project.clj
:plugins [[lein-localrepo "0.4.0"]]
Then install the jar to the local repository using
lein localrepo install <path-to-jar> org.allen.temporalintervalrelationships/time 0.2
You can check that the file is installed by running lein localrepo list and check that lein can resolve the project dependencies using lein deps. If all is well then you can start playing with the jar using lein repl.
Leiningen doesn't like local jars due to its goal of repeatable builds. If this was a real project using a third party closed source jar then the best thing to do is install it in a local Nexus repository and add a reference to that repository to your project.
However, this all seems a bit heavyweight for what you are trying to achieve. If all you want to do is play with the jar in the REPL then create a simple project like this
(defproject clojure-time "0.1.0-SNAPSHOT"
:dependencies [[org.clojure/clojure "1.4.0"]
[com.cemerick/pomegranate "0.0.13"]])
and use the pomegranate library to add the jar to your classpath manually
(require '[cemerick.pomegranate :as p])
(p/add-classpath "jsr-310-ri-0.6.3.jar")
(javax.time.Instant/now)
and play away.
the hackish way is to just put it into /proiject/path/lib/ and the 'proper' way is to:
add a dependency for it to your project
run lein deps which will print the command for installing the jar to your local maven repo
run the command leiningen gives you with the path to your jar
run lein deps again
I use clojure-csv in a lot of my applications, so make sure the modules referencing clojure-csv were able to build with it, this is what I did:
0) Ran lein new bene-csv
1) Added this to project.clj (after enter lein new bene-csv). The pertinent line is [clojure-csv/clojure-csv "1.3.2"], but it makes sense to show you the whole project.clj for good example's sake.
(defproject bene-csv "1.0.4-SNAPSHOT"
:description "A csv parsing library"
:dependencies [[org.clojure/clojure "1.3.0"]
[clojure-csv/clojure-csv "1.3.2"]
[util "1.0.2-SNAPSHOT"]]
:aot [bene-csv.core]
:omit-source true)
2) Made sure my bene-csv/src/bene_csv/core.clj references clojure-csv.
(ns bene-csv.core
^{:author "Charles M. Norton",
:doc "bene-csv is a small library to parse a .csv file.
Created on March 8, 2012"}
(:require [clojure.string :as cstr])
(:require [util.core :as utl])
(:use clojure-csv.core))
Finally, I ran these commands, so that my main project could reference bene-csv's functions and defs.
lein deps
lein install