Fork me on GitHub
#tools-deps
<
2018-05-31
>
rickmoynihan13:05:07

FYI, we’ve just merged :resolve-aliases support in to lein-tools-deps. Currently available only on the 0.4.x branch: https://github.com/RickMoynihan/lein-tools-deps/blob/0.4.x/example/project.clj#L20

4
kenny16:05:21

How do I load a Clojure file on the classpath from the CLI? I tried doing clj -i my.ns and I get a FileNotFound exception.

kenny16:05:37

I guess I could just call load in -e. Is there an easier way though?

ghadi16:05:16

clj -i foo.clj should work -i expects a file name, not a namespace

4
kenny16:05:54

Say I have a monolith type git repo that looks something like this:

mylib1
mylib2
mylib2 has a deps.edn files that looks like this:
{:deps {mylib1 {:local/root "../mylib1"}}}
Is it possible to use mylib2 as a git dep (`{:git/url "https://github.com/..." :sha "..." :deps/root "mylib2"}`) from the CLI and have mylib1 dependency be resolved using the :local/root path and the same :sha used previously?

kenny16:05:17

Or perhaps even changing mylib1 coordinate to be {:git/url "" :deps/root "mylib1"} and the :sha would be automatically set to the same :sha used for the the git URL ... that is already on the classpath.

Alex Miller (Clojure team)16:05:16

In short, no - git libs are only resolved from git

kenny19:05:19

If I have foo.clj:

(ns foo)

(defn bar
  [& args]
  (prn *command-line-args*))
And run:
clj -i foo.clj  -e '(foo/bar)' asd
I get a FileNotFoundException.

kenny19:05:16

I assume this is because it is expecting the last param to be a path. Why does it require that?

ghadi19:05:22

yes the last param is a path

kenny19:05:21

Right. Why does it require that?

zentrope20:05:15

Are relative paths wonky for deps? For instance, if you local/root a deps.end which in turn local/root’s a dep (something like “../../../whatever”) is the “../../..” going to mess things up if the top level isn’t in a “../../../” relationship to that project?

seancorfield20:05:14

@kenny Use - to introduce the arguments: clj -i foo.clj -e '(foo/bar)' - asd

zentrope20:05:16

Hm. Overriding it in the “current” deps.edn solves it.

kenny20:05:09

@seancorfield The program doesn't exit then.

ghadi20:05:08

@kenny what are you trying to achieve?

seancorfield20:05:19

Ah, yes, you'd have to send it empty input 🙂

kenny20:05:25

To evaluate the function in -e.

ghadi20:05:26

there may be a more straighforward path.

kenny20:05:35

(foo/bar)

seancorfield20:05:02

You can only get *command-line-args* if you have main-opts

ghadi20:05:25

you can -e "(require 'foo)" -e "(foo/bar)"

kenny20:05:18

I also want *command-line-args* 🙂

ghadi20:05:23

oh -- yeah you can't.

ghadi20:05:10

Or, this (cue eye roll):

clj - <<EOM
(require 'foo)
(foo/bar)
EOM

ghadi20:05:23

(In bash or zsh)

seancorfield20:05:24

Rename bar to -main and then clj -i foo.clj -m foo asd

seancorfield20:05:50

(! 633)-> cat foo.clj 
(ns foo)

(defn -main
  [& args]
  (prn *command-line-args*))

Thu May 31 13:08:39
(sean)-(jobs:0)-(~/clojure/kenny)
(! 634)-> clj -i foo.clj -m foo asd
("asd")

kenny20:05:53

Bleh. And I can't rename because -main is used for something else.

kenny20:05:08

I also may want to call different functions besides -main.

seancorfield20:05:40

Add another namespace for this entry point?

kenny20:05:21

Perhaps. But I may have 50 different functions that I'd like to call. I don't want 50 namespaces to simply call a function.

seancorfield20:05:21

OK, how about this:

(! 638)-> cat foo.clj 
(ns foo)

(defn -main [& args] (println "I am foo's -main"))

(ns foo.main)

(defn -main
  [& args]
  (prn *command-line-args*))

Thu May 31 13:10:56
(sean)-(jobs:0)-(~/clojure/kenny)
(! 639)-> clj -i foo.clj -m foo
I am foo's -main

Thu May 31 13:11:04
(sean)-(jobs:0)-(~/clojure/kenny)
(! 640)-> clj -i foo.clj -m foo.main asd
("asd")

seancorfield20:05:31

(multiple namespaces in one file 🙂 )

kenny20:05:03

Ah, interesting.

kenny20:05:44

Still feels like a bit of a hack but that could work. It seems like it'd be useful to have a CLI option to execute a function besides -main.

seancorfield20:05:51

We have a job script at work whose first argument is a namespace-qualified symbol and it calls that from -main. So you'd say clj -i job.clj -m job job/foo asd

seancorfield20:05:07

Relying on dynamic require and resolve

kenny20:05:42

Yeah, I think I like that option more.

kenny21:05:17

Should :override-deps work to change an artifact's coordinate from Git to local?

kenny21:05:29

It doesn't seem to work. Example a/deps.edn:

{:deps {org.clojure/tools.cli {:mvn/version "0.3.7"}}}
b/deps.edn:
{:deps    {a {:git/url ""
              :sha     "7b8e5c33588a94c1318e225dc9fe655599216370"}}
 :aliases {:dev {:override-deps {a {:local/root "../a"}}}}}
Result of clj -A:dev -Sforce -Spath:
src:/home/kenny/.m2/repository/org/clojure/clojure/1.9.0/clojure-1.9.0.jar:/home/kenny/.gitlibs/libs/a/a/7b8e5c33588a94c1318e225dc9fe655599216370/src/main/clojure:/home/kenny/.m2/repository/org/clojure/spec.alpha/0.1.143/spec.alpha-0.1.143.jar:/home/kenny/.m2/repository/org/clojure/core.specs.alpha/0.1.24/core.specs.alpha-0.1.24.jar
That classpath does not contain tools.cli as required by the overridden a dependency.

Alex Miller (Clojure team)22:05:06

No, there is a ticket for this right now

kenny22:05:59

https://dev.clojure.org/jira/browse/TDEPS-51. Is the plan to allow this behavior or throw?