dundalek / closh

Bash-like shell based on Clojure
Eclipse Public License 1.0
1.62k stars 66 forks source link

Referring to def'd var blows up program #75

Closed johanatan closed 5 years ago

johanatan commented 6 years ago

repro.cljs

(when (clojure.string/blank? (getenv "JAR")) (setenv "JAR" "lambda.jar"))
(def target-name (str "target/uberjar/" (getenv "JAR")))
(defn jar-not-exists? [] (not (sh-ok test -f (str target-name))))

(when (jar-not-exists?) (source-shell "mkdir -p target/uberjar/; touch target/uberjar/lambda.jar"))

rm -f (str target-name)

(when (jar-not-exists?) (sh echo (str (str target-name) " was not created.")))

repro.sh

#!/bin/bash

closh < `dirname $0`/repro.cljs

Running repro.sh blows up on the rm -f (str target-name) line. Any reference to target-name or to (getenv "JAR") there will cause the same blowup.

Also a simple ls after a reference to either target-name or (getenv "JAR") will cause the same.

The program exits with a success code of 0 (and the lines after blow up do not execute).

johanatan commented 6 years ago

Related: https://github.com/dundalek/closh/issues/2

johanatan commented 6 years ago

Here is a workaround:

jclosh.sh

#!/bin/bash

tmppipe=$(mktemp -u)
mkfifo -m 600 "$tmppipe"
closh < "$tmppipe" & > 1
closh_pid=$!

while IFS='' read -r line || [[ -n "$line" ]]; do
    echo "$line" > "$tmppipe";
done < "$1"

wait $closh_pid

Use it like so:

$ jclosh.sh repro.cljs
dundalek commented 6 years ago

Thanks for the repro. Could you explain what is the principle behind the workaround? It seems it feeds the input via fifo pipe. What is the difference from the plain file redirection?

johanatan commented 6 years ago

Line-by-line rather than all at once. I have no idea why this would matter but it does.

dundalek commented 5 years ago

Initial iteration of script mode has now landed in the latest JVM version release v0.4, you can read how to use it in the scripting docs.