Fork me on GitHub
#babashka
<
2022-12-21
>
pesterhazy11:12:10

I wrote > Common operations like spit or slurp and functions in https://github.com/babashka/fs/blob/master/API.md accept a string filename or, alternatively, an instance of java.io.File as returned by https://github.com/babashka/fs/blob/master/API.md#file-page_facing_up. So (spit "out" "xxx") and (spit (babashka.fs/file "out") "xxx") are interchangeable. Is this accurate and safe?

borkdude11:12:34

Good work @pesterhazy I think it would be nice to enable syntax highlight by adding clojure to the code fence

pesterhazy11:12:36

Right, forgot it again - done

pesterhazy12:12:08

@arturdumchev would reading this have answered the question you had?

yes 1
1
borkdude12:12:20

@pesterhazy Were you going to announce this somewhere? I'd want to share it on social media, but if you'd rather wait, then I'll wait as well

pesterhazy13:12:43

Ok, managed to find my AWS credentials and uploaded the post to my blog https://presumably.de/how-to-do-things-with-babashka.html

pesterhazy13:12:02

I think it would be great to share it on social media! I think there's enough stuff there to be useful now

borkdude15:12:33

@pesterhazy I shared it on twitter and this will also go to my mastodon: https://twitter.com/borkdude/status/1605591648875864064

🎉 1
borkdude15:12:20

Also shared to #C8NUSGWG6

borkdude20:12:19

@pesterhazy There's a small error here:

["cat" "/etc/hosts"]
This shouldn't be wrapped in a vector

borkdude20:12:27

And the destroy-tree isn't necessary here

pesterhazy21:12:12

Do you mean this line? It doesn't work without the [] (it's using process, not shell) https://github.com/pesterhazy/presumably/blob/master/posts/howto-babashka.md?plain=1#L102

pesterhazy21:12:06

About the destroy-tree call, I guess I was cargo-culting by copying it from the docs without thinking too much about it https://github.com/babashka/process#processing-streaming-output Basically you're saying it's needed only when you don't consume the entire process input - like when the process never stops?

pesterhazy21:12:48

To be clear, this doesn't work for me

(require '[babashka.process :refer [process]]
         '[ :as io])

(let [stream (process {:err :inherit} "cat" "/etc/hosts")]
  (with-open [rdr (io/reader (:out stream))]
    (doseq [line (line-seq rdr)]
      (println (str "#" line))))
  nil)

pesterhazy21:12:08

----- Error --------------------------------------------------------------------
Type:     java.lang.ClassCastException
Message:  java.lang.Character cannot be cast to java.util.Map$Entry
Location: /Users/user/arena/bin/scrapbb:6:14

borkdude21:12:19

This is because you're using a too old bb ;)

borkdude21:12:37

The first map is just ignored probably even in this case

borkdude21:12:49

What's bb --version?

pesterhazy21:12:15

% bb --version
babashka v1.0.165

borkdude21:12:40

This doesn't have the updated process API

borkdude21:12:43

From the process docs (with the newly updated API):

pesterhazy21:12:13

Hm, I updated to 1.0.168 Now "cat" "/etc/hosts" works. But ["cat" "/etc/hosts"] doesn't work anymore - is this a breaking change?

pesterhazy21:12:24

I'm pretty sure I got this way of using it from the docs (at the time)

borkdude21:12:45

No, this is not a breaking change, (process {} ["foo"]) was never supposed to work, the first map should have been a process that you piped into the next process. The documented API was: (process ["foo"] {?opts})

borkdude21:12:51

and this still works btw

borkdude21:12:09

You can check the docs of the previous version if you don't believe me ;) https://github.com/babashka/process/tree/ff4085c4dc571966a2bfa239b778de5e08b76aaf

pesterhazy21:12:07

I believe you! What I didn't get was that I misplaced the options map

borkdude21:12:34

Makes sense

pesterhazy22:12:21

OK, I fixed all usage of babashka.process/process and babashka.process/sh to use the new style (`opts? arg1 arg2 ....`) https://presumably.de/how-to-do-things-with-babashka.html?cb=1#read-command-output-line-by-line

pesterhazy22:12:37

Also added this note > Note: this example requires babashka v1.0.168 or higher.

👍 1
borkdude22:12:52

Cool, thanks!

pesterhazy22:12:32

The new style of calling the fns in babashka.process is much nicer

👍 1
borkdude10:12:53

I posted a link to HN with the title "How to Babashka (Clojure vs. Bash)" (I think mentioning bash and Clojure in the title will get more views and "things" is a word which could be left out imo)

👍 3
borkdude15:12:48

Check out "How to do things with babashka" by @pesterhazy! A good intro with examples of how to do bash things in babashka babashka! https://presumably.de/how-to-do-things-with-babashka.html

nice 6
💯 2
pesterhazy21:12:06

About the destroy-tree call, I guess I was cargo-culting by copying it from the docs without thinking too much about it https://github.com/babashka/process#processing-streaming-output Basically you're saying it's needed only when you don't consume the entire process input - like when the process never stops?

pesterhazy21:12:08

Just for managing expectations on my side: in emacs+eglot+clojure-lsp, should I be able to see docs and jump-to-definition for babashka-bundled vars like babashka.process/process?

pesterhazy21:12:43

LSP does give me jump-to-definition for vars in my own namespace, so it's working that far

borkdude21:12:22

Not jump to definition since there isn't any source to jump to, but there will be analysis available for built-ins, so you will see arity warnings, docstrings, etc probably

borkdude21:12:10

or maybe it does jump to definition for the deps it used for analysis, not sure.

ericdallo21:12:25

It does if a bb.edn is available

ericdallo21:12:50

Then bb deps would be on classpath provided by clojure-lsp

pesterhazy21:12:52

Lemme try that again

pesterhazy22:12:53

OK, so my mini-project looks like this

pesterhazy22:12:04

% cat bb.edn
{:paths ["bb"]}

pesterhazy22:12:20

% cat src/scrap.clj
#!/usr/bin/env bb

(require '[babashka.process :refer [sh]])
(def myname (:out (sh "whoami")))

(print myname)

pesterhazy22:12:15

Now if I move the point to sh and jump to definition, I get "No definitions found"

ericdallo22:12:49

Shouldn't the :paths contain src and not bb?

pesterhazy22:12:33

Ah, yes 🙂 But it doesn't change anything

ericdallo22:12:25

Could you check what is the classpath clojure-lsp is finding? In emacs there is the lsp-clojure-server-info command

pesterhazy22:12:12

Any idea on how to do that with eglot, which is what I'm using?

pesterhazy22:12:30

Or from the command line perhaps

ericdallo22:12:36

Not possible with eglot AFAIK as this is a custom request. you can check server logs though

pesterhazy22:12:17

Ok, I'm looking at the server log now. Is this what you mean?

2022-12-21T22:42:32.502Z  INFO [clojure-lsp.source-paths:85] - [Startup] Using source-paths from classpath: ["/Users/user/arena/scrap/src"]

ericdallo22:12:15

Almost, we need the classpath string, but it's probably using the cached classpath so it won't show on these logs, but if you delete .lsp/.cache it should show

pesterhazy22:12:57

2022-12-21T22:47:02.910Z  INFO [clojure-lsp.classpath:103] - Finding classpath via `/Users/user/arena/bin/bb print-deps --format classpath`
2022-12-21T22:47:03.364Z  DEBUG [clojure-lsp.classpath:115] - Classpath found, paths:  ["src" "/Users/user/.gitlibs/libs/babashka/babashka.core/52a6037bd4b632bffffb04394fb4efd0cdab6b1e/src" "/Users/user/.m2/repository/babashka/babashka.curl/0.1.1/babashka.curl-0.1.1.jar" "/Users/user/.m2/repository/babashka/fs/0.1.2/fs-0.1.2.jar" "/Users/user/.m2/repository/cheshire/cheshire/5.11.0/cheshire-5.11.0.jar" "/Users/user/.m2/repository/clj-commons/clj-yaml/0.7.169/clj-yaml-0.7.169.jar" "/Users/user/.m2/repository/com/cognitect/transit-clj/1.0.329/transit-clj-1.0.329.jar" "/Users/user/.m2/repository/com/taoensso/timbre/6.0.1/timbre-6.0.1.jar" "/Users/user/.m2/repository/hiccup/hiccup/2.0.0-alpha2/hiccup-2.0.0-alpha2.jar" "/Users/user/.m2/repository/http-kit/http-kit/2.6.0-RC1/http-kit-2.6.0-RC1.jar" "/Users/user/.m2/repository/insn/insn/0.5.2/insn-0.5.2.jar" "/Users/user/.m2/repository/nrepl/bencode/1.1.0/bencode-1.1.0.jar" "/Users/user/.m2/repository/org/babashka/babashka.impl.reify/0.1.3/babashka.impl.reify-0.1.3.jar" "/Users/user/.m2/repository/org/babashka/cli/0.5.40/cli-0.5.40.jar" "/Users/user/.m2/repository/org/babashka/sci.impl.types/0.0.2/sci.impl.types-0.0.2.jar" "/Users/user/.m2/repository/org/clojure/clojure/1.11.1/clojure-1.11.1.jar" "/Users/user/.m2/repository/org/clojure/core.async/1.6.673/core.async-1.6.673.jar" "/Users/user/.m2/repository/org/clojure/core.match/1.0.0/core.match-1.0.0.jar" "/Users/user/.m2/repository/org/clojure/core.rrb-vector/0.1.2/core.rrb-vector-0.1.2.jar" "/Users/user/.m2/repository/org/clojure/data.csv/1.0.0/data.csv-1.0.0.jar" "/Users/user/.m2/repository/org/clojure/data.priority-map/1.1.0/data.priority-map-1.1.0.jar" "/Users/user/.m2/repository/org/clojure/data.xml/0.2.0-alpha8/data.xml-0.2.0-alpha8.jar" "/Users/user/.m2/repository/org/clojure/test.check/1.1.1/test.check-1.1.1.jar" "/Users/user/.m2/repository/org/clojure/tools.cli/1.0.214/tools.cli-1.0.214.jar" "/Users/user/.m2/repository/org/clojure/tools.logging/1.1.0/tools.logging-1.1.0.jar" "/Users/user/.m2/repository/rewrite-clj/rewrite-clj/1.1.45/rewrite-clj-1.1.45.jar" "/Users/user/.m2/repository/selmer/selmer/1.12.50/selmer-1.12.50.jar" "/Users/user/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.13.3/jackson-core-2.13.3.jar" "/Users/user/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-cbor/2.13.3/jackson-dataformat-cbor-2.13.3.jar" "/Users/user/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-smile/2.13.3/jackson-dataformat-smile-2.13.3.jar" "/Users/user/.m2/repository/tigris/tigris/0.1.2/tigris-0.1.2.jar" "/Users/user/.m2/repository/org/flatland/ordered/1.5.9/ordered-1.5.9.jar" "/Users/user/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar" "/Users/user/.m2/repository/com/cognitect/transit-java/1.0.362/transit-java-1.0.362.jar" "/Users/user/.m2/repository/com/taoensso/encore/3.31.0/encore-3.31.0.jar" "/Users/user/.m2/repository/io/aviso/pretty/1.3/pretty-1.3.jar" "/Users/user/.m2/repository/org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar" "/Users/user/.m2/repository/org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.jar" "/Users/user/.m2/repository/org/clojure/tools.analyzer.jvm/1.2.2/tools.analyzer.jvm-1.2.2.jar" "/Users/user/.m2/repository/org/clojure/tools.reader/1.3.6/tools.reader-1.3.6.jar" "/Users/user/.m2/repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar" "/Users/user/.m2/repository/org/msgpack/msgpack/0.6.12/msgpack-0.6.12.jar" "/Users/user/.m2/repository/com/taoensso/truss/1.6.0/truss-1.6.0.jar" "/Users/user/.m2/repository/org/clojure/core.memoize/1.0.253/core.memoize-1.0.253.jar" "/Users/user/.m2/repository/org/clojure/tools.analyzer/1.1.0/tools.analyzer-1.1.0.jar" "/Users/user/.m2/repository/org/ow2/asm/asm/9.2/asm-9.2.jar" "/Users/user/.m2/repository/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar" "/Users/user/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar" "/Users/user/.m2/repository/org/clojure/core.cache/1.0.225/core.cache-1.0.225.jar"]
2022-12-21T22:47:03.365Z  INFO [clojure-lsp.source-paths:85] - [Startup] Using source-paths from classpath: ["/Users/user/arena/scrap/src"]

ericdallo22:12:13

Is babashka.process there?

pesterhazy22:12:02

No, for some reason it's missing

pesterhazy22:12:28

/Users/user/arena/bin/bb print-deps --format classpath
src:/Users/user/.gitlibs/libs/babashka/babashka.core/52a6037bd4b632bffffb04394fb4efd0cdab6b1e/src:/Users/user/.m2/repository/babashka/babashka.curl/0.1.1/babashka.curl-0.1.1.jar:/Users/user/.m2/repository/babashka/fs/0.1.2/fs-0.1.2.jar:/Users/user/.m2/repository/cheshire/cheshire/5.11.0/cheshire-5.11.0.jar:/Users/user/.m2/repository/clj-commons/clj-yaml/0.7.169/clj-yaml-0.7.169.jar:/Users/user/.m2/repository/com/cognitect/transit-clj/1.0.329/transit-clj-1.0.329.jar:/Users/user/.m2/repository/com/taoensso/timbre/6.0.1/timbre-6.0.1.jar:/Users/user/.m2/repository/hiccup/hiccup/2.0.0-alpha2/hiccup-2.0.0-alpha2.jar:/Users/user/.m2/repository/http-kit/http-kit/2.6.0-RC1/http-kit-2.6.0-RC1.jar:/Users/user/.m2/repository/insn/insn/0.5.2/insn-0.5.2.jar:/Users/user/.m2/repository/nrepl/bencode/1.1.0/bencode-1.1.0.jar:/Users/user/.m2/repository/org/babashka/babashka.impl.reify/0.1.3/babashka.impl.reify-0.1.3.jar:/Users/user/.m2/repository/org/babashka/cli/0.5.40/cli-0.5.40.jar:/Users/user/.m2/repository/org/babashka/sci.impl.types/0.0.2/sci.impl.types-0.0.2.jar:/Users/user/.m2/repository/org/clojure/clojure/1.11.1/clojure-1.11.1.jar:/Users/user/.m2/repository/org/clojure/core.async/1.6.673/core.async-1.6.673.jar:/Users/user/.m2/repository/org/clojure/core.match/1.0.0/core.match-1.0.0.jar:/Users/user/.m2/repository/org/clojure/core.rrb-vector/0.1.2/core.rrb-vector-0.1.2.jar:/Users/user/.m2/repository/org/clojure/data.csv/1.0.0/data.csv-1.0.0.jar:/Users/user/.m2/repository/org/clojure/data.priority-map/1.1.0/data.priority-map-1.1.0.jar:/Users/user/.m2/repository/org/clojure/data.xml/0.2.0-alpha8/data.xml-0.2.0-alpha8.jar:/Users/user/.m2/repository/org/clojure/test.check/1.1.1/test.check-1.1.1.jar:/Users/user/.m2/repository/org/clojure/tools.cli/1.0.214/tools.cli-1.0.214.jar:/Users/user/.m2/repository/org/clojure/tools.logging/1.1.0/tools.logging-1.1.0.jar:/Users/user/.m2/repository/rewrite-clj/rewrite-clj/1.1.45/rewrite-clj-1.1.45.jar:/Users/user/.m2/repository/selmer/selmer/1.12.50/selmer-1.12.50.jar:/Users/user/.m2/repository/com/fasterxml/jackson/core/jackson-core/2.13.3/jackson-core-2.13.3.jar:/Users/user/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-cbor/2.13.3/jackson-dataformat-cbor-2.13.3.jar:/Users/user/.m2/repository/com/fasterxml/jackson/dataformat/jackson-dataformat-smile/2.13.3/jackson-dataformat-smile-2.13.3.jar:/Users/user/.m2/repository/tigris/tigris/0.1.2/tigris-0.1.2.jar:/Users/user/.m2/repository/org/flatland/ordered/1.5.9/ordered-1.5.9.jar:/Users/user/.m2/repository/org/yaml/snakeyaml/1.33/snakeyaml-1.33.jar:/Users/user/.m2/repository/com/cognitect/transit-java/1.0.362/transit-java-1.0.362.jar:/Users/user/.m2/repository/com/taoensso/encore/3.31.0/encore-3.31.0.jar:/Users/user/.m2/repository/io/aviso/pretty/1.3/pretty-1.3.jar:/Users/user/.m2/repository/org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar:/Users/user/.m2/repository/org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.jar:/Users/user/.m2/repository/org/clojure/tools.analyzer.jvm/1.2.2/tools.analyzer.jvm-1.2.2.jar:/Users/user/.m2/repository/org/clojure/tools.reader/1.3.6/tools.reader-1.3.6.jar:/Users/user/.m2/repository/javax/xml/bind/jaxb-api/2.3.0/jaxb-api-2.3.0.jar:/Users/user/.m2/repository/org/msgpack/msgpack/0.6.12/msgpack-0.6.12.jar:/Users/user/.m2/repository/com/taoensso/truss/1.6.0/truss-1.6.0.jar:/Users/user/.m2/repository/org/clojure/core.memoize/1.0.253/core.memoize-1.0.253.jar:/Users/user/.m2/repository/org/clojure/tools.analyzer/1.1.0/tools.analyzer-1.1.0.jar:/Users/user/.m2/repository/org/ow2/asm/asm/9.2/asm-9.2.jar:/Users/user/.m2/repository/com/googlecode/json-simple/json-simple/1.1.1/json-simple-1.1.1.jar:/Users/user/.m2/repository/org/javassist/javassist/3.18.1-GA/javassist-3.18.1-GA.jar:/Users/user/.m2/repository/org/clojure/core.cache/1.0.225/core.cache-1.0.225.jar

borkdude22:12:01

It uses this deps.edn for this feature and process is missing because it's a git submodule of babashka. Please make an issue about it and I'll fix it. https://github.com/babashka/babashka/blob/master/resources/META-INF/babashka/deps.edn

👍 1
pesterhazy22:12:01

It still doesn't work for babashka.fs/glob (or even clojure.core/print) but that's probably a problem with my emacs/lsp, not bb

borkdude22:12:44

can you try to make an arity error to see if it detects that?

pesterhazy23:12:01

Yeah that works fine - it's only the jump-to-definition that doesn't work

pesterhazy23:12:22

I also do get docs for it

borkdude23:12:09

maybe this is an eglot problem with jumping to definition into jars? I've heard about this before

ericdallo23:12:28

Oh yeah, forgot about that eglot issue

ericdallo23:12:53

Installing jarchive should fix it

borkdude23:12:04

can you try it with a regular jvm project to see if you can navigate into clojure.jar?

pesterhazy23:12:50

Will try tomorrow

pesterhazy23:12:57

A little tired now :) thanks yall for all the help

👍 1