Fork me on GitHub
#babashka
<
2023-02-10
>
benfleis11:02:23

From what I can read in https://github.com/babashka/process it seems that (require '[babashka.process.pprint]) should always “just work”… what did I miss? (Note: requiring other things eg process/shell, works as expected.)

borkdude11:02:03

In bb this isn't necessary and this namespace isn't exposed in bb

benfleis11:02:59

ah so the Clojure.pprint bit only applies when invoking clj. That makes sense in hindsight 🙂

borkdude11:02:45

yeah, and I think nowadays it isn't mostly necessary at all to call it yourself anymore, since it's automatically done when clojure.pprint is already loaded

benfleis11:02:22

Not exactly a follow up, but debugging the same thing (and my clojure is 5+ years rusty, so I’m probably misreading)… I want to pass both opts, and a command sequence to process. The third/fourth rows here suggests that I can’t have both:

(parse-args ["git" "ls-files"]) ; {:prev nil, :cmd ["git" "ls-files"], :opts nil}
(parse-args [["git" "ls-files"]]) ; {:prev nil, :cmd ["git" "ls-files"], :opts nil}
(parse-args [{} ["git" "ls-files"]]) ; {:prev nil, :cmd ["[git" "ls-files]"], :opts {}}
(parse-args [{:cmd ["git" "ls-files"]}]) ; {:prev nil, :cmd [], :opts {:cmd ["git" "ls-files"]}}
Note that cmd gets stringified to become the unwelcome command [git when it’s second arg, nor is there an explicit way to set cmd to a sequence. It seems better to support sequences as such so that my pre-constructed command + args can be passed directly… (instead of me quoting and using bash -c). [a] do I understand correctly? [b] PR welcome with such a tweak?

borkdude12:02:59

(parse-args [{} "git" "ls-files"])

borkdude12:02:35

so you can do:

(apply process opts "git" remaining-args)

benfleis12:02:49

(parse-args [{} "git" "ls-files"]) ; {:prev nil, :cmd ["git" "ls-files"], :opts {}} does work, thanks! It feels a bit incongruous that it flattens in the “first position” sequence, but not the second. I’m curious, is there a reason to avoid that case? (Not seeking controversy, but curious since the behavior is counter to my intuitions after my initial sequence experiment worked.)

borkdude12:02:51

The process library moved from the syntax (process [args] ?opts)) to (process ?opts ...args) since this was easier in combination with apply. parse-args bridges that migration

borkdude12:02:47

That this is the case shouldn't matter to you as a new user, the README reflects how you should currently use the library

borkdude12:02:19

I have considered allowing this:

(process ?opts ...preceding-args [remaining-arguments])

borkdude12:02:33

but this would create another migration which could lead to confusion

borkdude12:02:06

right now you have to do it like this:

(apply process ?opts ...preceding-args [remaining-arguments])

borkdude12:02:58

I hope all is clear now?

benfleis12:02:01

Close, but not quite — the “considered allowing” form seems that would imply full something like:

;; both args and extra-args arguments are flattened into command
(process ?opts args & extra-args)
where both args and extra-args may be string-or-seq-of-string, but “another migration” suggests that it’s no longer compatible.

benfleis12:02:06

Perhaps I misunderstand some of the nuance in the type checks due to the argument reordering. My mental model assumes 3 basic types: map (opts), seq (array of args), or string (arg).

borkdude12:02:37

The supported syntax is:

(process ?opts ...strings)

borkdude12:02:51

(process ?opts [...strings]) was never supported - if it works, it's purely accidental

borkdude12:02:59

Just go by the docs and not by what accidentally works

benfleis13:02:51

lol, I thought the repl was the doc 🙂

borkdude13:02:23

there are many things in clojure that work, but not according to prescribed usage. E.g. (inc (merge 1))

benfleis13:02:08

thanks for the guidance 🙂

borkdude12:02:52

deps-try, a bb CLI to try out JVM Clojure libs: https://twitter.com/borkdude/status/1624027178307387393

🙌 2
Daniel Gerson14:02:34

Cool! But see in 🧵

Daniel Gerson14:02:39

deps-try                                                                                               127 ✘  14:03:38
----- Error --------------------------------------------------------------------
Type:     clojure.lang.ArityException
Message:  Wrong number of args (3) passed to: babashka.deps/clojure
Location: /Users/dmg46664/.gitlibs/libs/io.github.eval/deps-try/dca3e52fa7e723021ae4f8c62d3c111771d27bde/src/eval/deps_try.clj:26:40

----- Context ------------------------------------------------------------------
22:   (str/trim (with-out-str (deps/clojure {:dir (str tmp)} "-Spath" "-Sdeps" (str {:deps deps})))))
23:
24: (defn -main [& args]
25:   (fs/with-temp-dir [tmp {}]
26:     (let [verbose-output (with-out-str (deps/clojure {:dir (str tmp)} "-Sverbose" "-Spath"))
                                           ^--- Wrong number of args (3) passed to: babashka.deps/clojure
27:           cp-file (parse-cp-file verbose-output)
28:           basis-file (str/replace cp-file #".cp$" ".basis")
29:           requested-deps (try-deps/parse-dep-args args)
30:           default-cp (deps->cp tmp '{org.clojure/clojure {:mvn/version "RELEASE"}})
31:           requested-cp (deps->cp tmp requested-deps)

----- Stack trace --------------------------------------------------------------
eval.deps-try      - /Users/dmg46664/.gitlibs/libs/io.github.eval/deps-try/dca3e52fa7e723021ae4f8c62d3c111771d27bde/src/eval/deps_try.clj:26:40
clojure.core/apply - <built-in>
user               - <expr>:1:38

Daniel Gerson14:02:56

Works! Thanks.

J15:02:37

Hi! Is there an other way to do this:

{:task (shell "MY_ENV_VAR=1 clojure -M:foo:baz")}

borkdude15:02:46

@UHZPYLPU1 Yes:

(shell {:extra-env {"MY_ENV_VAR" "1"} "clojure -M:foo:baz")

borkdude15:02:08

or even better:

(clojure {:extra-env {"MY_ENV_VAR" "1"} "-M:foo:baz")
🤯

J15:02:39

@U04V15CAJ you rocks! Thanks!

dvingo16:02:15

Hi, I'm attempting to use java.time.format.DateTimeFormatter in bb and I'm seeing the java.util.Locale is having no effect:

$ bb
Babashka v1.1.173 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (import '[java.time LocalDate])
java.time.LocalDate
user=> (import '[java.time.format DateTimeFormatter])
java.time.format.DateTimeFormatter
user=> (def df (.withLocale (DateTimeFormatter/ofPattern "YYYY-MMM-dd") java.util.Locale/JAPAN))
#'user/df
user=> (.format (LocalDate/now) df)
"2023-Feb-10"
user=> 
vs clojure jvm:
$ clj
Clojure 1.11.1
user=> (import '[java.time LocalDate])
java.time.LocalDate
user=> (import '[java.time.format DateTimeFormatter])
java.time.format.DateTimeFormatter
user=>  (def df (.withLocale (DateTimeFormatter/ofPattern "YYYY-MMM-dd") java.util.Locale/JAPAN))
#'user/df
user=> (.format (LocalDate/now) df)
"2023-2月-10"
user=> 

borkdude16:02:29

@U051V5LLP locales aren't automatically included in a native image since they take up a lot of space. I think only English is included

dvingo16:02:11

aha - good to know, thanks for clarifying!

dvingo16:02:27

and because these are defined in java you'd need a custom image to add more, is that correct?

borkdude17:02:17

yes. we can enable some of those but it's hard to say which ones we should enable, other than english

dvingo17:02:35

cool, no worries, just making sure there wasn't a way to load them dynamically. Thanks!