Fork me on GitHub
#babashka
<
2023-09-04
>
pesterhazy09:09:15

How to make a Babashka program running external shell commands work on Windows? ๐Ÿงต

pesterhazy09:09:29

Suppose I have curl installed on Windows and curl.exe is on the PATH. (Note that curl is just an example here - I know better options for making HTTP requests exist in bb, but the q is not about that). Some observations (please correct if wrong): 1. babashka.process/shell seems to work differently on Windows than on Unix. For one thing, if you specify a bare command like "curl", the command finds it in PATH, whereas on Windows it doesn't seem to look at PATH at all. 2. Binaries on Windows have the .exe extension. bash on Windows or cmd.exe allow you to run curl and will find a binary called curl.exe for you if necessary, but babashka.process/shell doesn't. 3. These differences are not due to babashka.process but originate in ProcessBuilder, which takes an agnostic approach, forwarding the command to the OS. From the https://docs.oracle.com/en/java/javase/18/docs/api/java.base/java/lang/ProcessBuilder.html: "a command, a list of strings which signifies the external program file to be invoked and its arguments, if any. Which string lists represent a valid operating system command is system-dependent."

pesterhazy09:09:01

So what I'm looking for is guidance on how to write a cross-platform bb program. What are the best practices here?

borkdude09:09:21

should work the same

borkdude09:09:37

.exe should not matter, at least not with newer versions of bb

pesterhazy09:09:20

Do you mean that if I run

(shell "curl")
, it'll look for an exe file in PATH?

pesterhazy09:09:46

OK, then my whole mental model may be off. I'll try this and get back to you

pesterhazy09:09:04

(Part of the problem is that it's a PITA to get a working Windows shell... without a Windows box)

borkdude09:09:45

I'd test this on my Windows PC but it doesn't seem to be work anymore ๐Ÿ˜ง

pesterhazy09:09:22

What I do is to spin up an SSH on CircleCI, but it takes a while and the ssh terminal is pretty janky

borkdude09:09:28

yeah, this isn't funny

borkdude09:09:32

it's a 3k+ PC

borkdude11:09:06

ok, the darn thing started again, no idea why!

borkdude11:09:07

C:\Users\borkdude>bb
Babashka v1.3.177 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (require '[babashka.process :refer [shell]])
nil
user=> (shell "curl")
curl: try 'curl --help' or 'curl --manual' for more information
clojure.lang.ExceptionInfo [at <repl>:2:1]

borkdude11:09:26

so to confirm, it "just works" on Windows, by design. This is babashka.process doing extra work on top of processbuilder

borkdude11:09:28

but you will need a recent version of bb, it's been there for a while though

pesterhazy12:09:44

You're right โ€“ it seems to be working just fine

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

user=> (babashka.process/shell "java" "-version")
openjdk version "20.0.2" 2023-07-18
OpenJDK Runtime Environment (build 20.0.2+9-78)
OpenJDK 64-Bit Server VM (build 20.0.2+9-78, mixed mode, sharing)
{:proc #object[java.lang.ProcessImpl 0x379b0638 "Process[pid=8328, exitValue=0]"], :exit 0, :in #object[java.lang.ProcessBuilder$NullOutputStream 0x5adbebf2 "java.lang.ProcessBuilder$NullOutputStream@5adbebf2"], :out #object[java.lang.ProcessBuilder$NullInputStream 0x3743635c "java.lang.ProcessBuilder$NullInputStream@3743635c"], :err #object[java.lang.ProcessBuilder$NullInputStream 0x3743635c "java.lang.ProcessBuilder$NullInputStream@3743635c"], :prev nil, :cmd ["C:\\Program Files\\OpenJDK\\jdk-20.0.2\\bin\\java.exe" "-version"]}

pesterhazy12:09:10

1. "java" find java.exe 2. find that exe in PATH

pesterhazy12:09:49

Thanks for the great work smoothing over the platform differences

๐Ÿ‘ 2
timo17:09:22

I am getting an error running

(b/write-pom {:src-pom pom-template
              :src-dirs src-dirs
              :class-dir class-dir
              :lib lib
              :version (version/string repo-config)
              :basis (basis project-config)
              :scm (assoc scm :tag (version/sha))})

timo17:09:17

----- Error --------------------------------------------------------------------
Type:     java.lang.Exception
Message:  Unable to resolve classname: java.util.concurrent.ConcurrentMap
Location: clojure/tools/deps/alpha/util/session.clj:12:3

----- Context ------------------------------------------------------------------
 8:
 9: (ns ^{:skip-wiki true}
10:   clojure.tools.deps.alpha.util.session
11:   "Maintains session resources during or across runs of the resolver"
12:   (:import
      ^--- Unable to resolve classname: java.util.concurrent.ConcurrentMap
13:     [java.util.concurrent ConcurrentMap ConcurrentHashMap]
14:     [java.util.function Function]))
15:
16: (def session (ConcurrentHashMap.)) ;; should never be nil
17:

----- Stack trace --------------------------------------------------------------
clojure.tools.deps.alpha.util.session     - clojure/tools/deps/alpha/util/session.clj:12:3
clojure.tools.deps.alpha                  - clojure/tools/deps/alpha.clj:10:3
clojure.tools.build.tasks.process         - /home/timo/.gitlibs/libs/io.github.babashka/tools.bbuild/0a4959d2b1147f79e9fb37e0243727390b8ba2b3/src/main/clojure/clojure/tools/build/tasks/process.clj:10:3
clojure.tools.build.api/process           - /home/timo/.gitlibs/libs/io.github.babashka/tools.bbuild/0a4959d2b1147f79e9fb37e0243727390b8ba2b3/src/main/clojure/clojure/tools/build/api.clj:205:3
clojure.tools.build.api/process           - /home/timo/.gitlibs/libs/io.github.babashka/tools.bbuild/0a4959d2b1147f79e9fb37e0243727390b8ba2b3/src/main/clojure/clojure/tools/build/api.clj:180:1
... (run with --debug to see elided elements)
tools.build                               - /home/timo/projects/datahike/bb/src/tools/build.clj:50:26
clojure.tools.build.api/write-pom         - /home/timo/.gitlibs/libs/io.github.babashka/tools.bbuild/0a4959d2b1147f79e9fb37e0243727390b8ba2b3/src/main/clojure/clojure/tools/build/api.clj:344:1
tools.build/pom                           - /home/timo/projects/datahike/bb/src/tools/build.clj:46:3
tools.build/pom                           - /home/timo/projects/datahike/bb/src/tools/build.clj:43:1
user-8e1ba67e-6b46-4b05-8393-8d0f8269567a - <expr>:26:1
Is this expected when running with latest tools.deps.native?

timo17:09:40

yeah, I thought so but didn't know where to look for it. Thanks @U03B2SRNYTY

๐Ÿ‘ 2
borkdude17:09:16

why is tools.deps.alpha (alpha!) on the classpath?

timo17:09:04

it's included as a pod

borkdude17:09:26

this is not the latest version of tools.bbuild

timo17:09:43

you're best, I thought it's the pod and didn't think about anything else ๐Ÿ‘

borkdude18:09:24

not sure if upgrading will solve your problem, but worth a shot

borkdude18:09:27

let me know

timo18:09:31

it does work ๐Ÿ™‚

๐Ÿ‘ 4