This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-09-22
Channels
- # announcements (2)
- # asami (123)
- # aws (17)
- # babashka (77)
- # babashka-sci-dev (23)
- # beginners (48)
- # biff (6)
- # calva (35)
- # cider (16)
- # clj-on-windows (1)
- # clj-yaml (19)
- # clojure (36)
- # clojure-europe (78)
- # clojure-nl (5)
- # clojure-norway (8)
- # clojure-poland (3)
- # clojure-uk (16)
- # clojurescript (17)
- # cursive (6)
- # datahike (3)
- # datalevin (26)
- # duct (7)
- # emacs (41)
- # events (2)
- # fulcro (7)
- # graphql (5)
- # honeysql (13)
- # juxt (3)
- # kaocha (7)
- # lsp (5)
- # malli (12)
- # off-topic (14)
- # pathom (3)
- # portal (1)
- # rdf (9)
- # reitit (3)
- # remote-jobs (2)
- # shadow-cljs (37)
- # spacemacs (5)
- # tools-build (1)
- # tools-deps (20)
- # xtdb (2)
how is the value of :main-opts used? At a high level i know your supposed to pass it a string that matches the namespace that has a main function that you want to invoke. However, the arguments are a vector and nearly every example i see is a tuple where the first arg is '-m' . i take is this '-m' and not the :main-opts is really want makes it at a function that invokes the main function in that namespace. So a simple question is, why doesn't :main-opts just add the '-m' or is there something else that could be passed intead of '-m' for :main-opts?
link for reference https://clojure.org/reference/deps_and_cli#_execution
i guess i'm not sure what options clojure.main
takes, here is clojure main https://clojure.github.io/clojure/clojure.main-api.html#clojure.main/main
ahh the doc string says what else can be passed
main-opt:
-m, --main ns-name Call the -main function from namespace w/args
-r, --repl Run a repl
path Run a script from a file or resource
- Run a script from standard input
-h, -?, --help Print this help message and exit
-M tells the Clojure CLI to use clojure.main to run the code (as opposed to clojure.exec)
-m
defines the namespace that clojure.main should look in for the -main function
To me t makes sense to include the -m
in the main-opts as the next thing in the list is the namespace
I see the main-opts as a Clojure version of the command line arguments that are passed after -M (and any aliases following -M). Missing out the -m
and just having the namespace would make a difference between the command line and the :main-opts configuration (which I would find confusing)
thanks, yeah. I just think i have done anything but use -m. Here is another thing that can be done:
cat foo
(println (+ 1 1))
clj foo
WARNING: Implicit use of clojure.main with options is deprecated, use -M
2
Though i'm not sure how to avoid getting that warning, i don't really mind as i'm not sure when i would ever need this...The practicalli/clojure-deps-edn aliases have some additive arguments, but I don't recall using something instead of -m
`:repl/cider
{:extra-deps {nrepl/nrepl {:mvn/version "1.0.0"}
cider/cider-nrepl {:mvn/version "0.28.5"}}
:main-opts ["-m" "nrepl.cmdline"
"--middleware" "[cider.nrepl/cider-middleware]"
"--interactive"]}`
The depreciation message I believe was in part to help move people away from the -A flag and use the more appropriate -M flag, so f -M isn't used then I'd expect to see a depreciation message (unless using -X or -T instead, in which case main-opts sent used anyway)
I use clj -M:build -i build.clj -r
to start a REPL in "build" mode. -i
runs build.clj
as an "init script" and then -r
signals that a REPL should be started. At that point I'm in the user
ns but build.clj
has been loaded so I can do stuff like (build/database-setup {})
or (build/run-tests {:projects '{api auth login]})
Oh interesting, thanks sean.
Thats an interesting alternative to having a user
namespace do everything like that. Thanks Sean.
Given the various caveats around user.clj
I avoid it completely.
>
clj foo
> WARNING: Implicit use of clojure.main with options is deprecated, use -M
> 2
> Though i'm not sure how to avoid getting that warning,
If you use the -M
flag, like it asks, then it won't warn you anymore. 😉
$ clj -M foo
The -M
flag is separate from -m
. Currently if you don't use a conflicting flag (`-X` or -T
) then it starts just like -M
, but gives you this warning.
You'll notice in @U04V70XH6’s example above, he attaches his aliases to the -M
flag. But if you did not want to use an alias, you could still use -m
in the command line.
$ cli -M foo -m "foo.cmdline"
Thanks. I didn't even notice i left the -M off!
@U04V70XH6 what are the caveats of using a user.clj
? (I searched but couldn't find anything)
I always define the custom user namespace as dev/user.clj
so the dev
path has to be explicitly added via alias, controlling when it lads.
Would be interested to know what else to be aware of. Thanks.
@U05254DQM I'm surprised you can't find problems talked about in this Slack, since it's come up quite a few times. I'd have to search Slack to give you examples... Maybe later on today if I have time...
@U05254DQM so I had a quick search for user.clj
here this morning and found lots of threads with people running into problems with it. It's loaded outside the context of normal code running or a repl: it can swallow errors, many dynamic vars are not bound, it can load code in ways that break regular code in several ways (because of the difference in context). If it accidentally ends up in a library, it can cause all these problems for downstream users (Leiningen briefly had a bug that caused this to happen). Apparently, even the way different editor plugins interact with starting repls can cause user.clj
to be problematic (I did not dig into that particular thread).
The startup order seems something to investigate for constraints, as some contexts are not created before a (custom) user
namespace is loaded.
I am assuming a namespace loaded via an -i
flag loads later than the user namespace (hopefully at the end), possibly removing those context constraints (something to investigate at some point).
Other concerns seem due to convoluting the user namespace with the application code, which isn't a problem if kept separate.
FYI: I'd searched the web and didn't find anything, seemed to be a lot f noise in slack, but found a couple of threads. Thanks.