Fork me on GitHub
#tools-build
<
2022-11-29
>
seancorfield17:11:23

The Clojure CLI respects JAVA_CMD and JAVA_HOME environment variables. With clojure.tools.build/java-command, if you don't specify :java-cmd, it defaults to java and picks up whatever your system default is (even tho' those env vars are passed into the process when you run it). This means that you can't just set JAVA_CMD and have your Clojure build consistently use it, you potentially need to replicate some sort of "find java" logic in every build.clj file that runs Java/Clojure subprocesses. Would it make sense for the default behavior of java-command -- when :java-cmd is not specified -- to attempt to mimic the "find java" logic of the Clojure CLI for consistency? More in 🧵

seancorfield17:11:33

(~/clojure)-(!2009)-> java -version
openjdk version "17.0.4" 2022-07-19
OpenJDK Runtime Environment (build 17.0.4+8-Ubuntu-120.04)
OpenJDK 64-Bit Server VM (build 17.0.4+8-Ubuntu-120.04, mixed mode, sharing)

Tue Nov 29 09:30:29
(~/clojure)-(!2010)-> echo $JAVA_CMD
/Developer/jdk-19+36/bin/java

Tue Nov 29 09:30:46
(~/clojure)-(!2011)-> clj -Sdeps '{:deps {io.github.clojure/tools.build {:git/tag "v0.8.5" :git/sha "9c738da"}}}'
Clojure 1.11.1
user=> (require '[clojure.tools.build.api :refer :all])
nil
user=> (System/getenv "JAVA_CMD")
"/Developer/jdk-19+36/bin/java"
user=> (System/getProperty "java.version")
"19"
user=> (java-command {:main 'clojure.main :basis (create-basis) :main-args ["-e" "[(System/getenv \"JAVA_CMD\") (System/getProperty \"java.version\")]"]})
{:command-args ["java" "-cp" "src:/home/sean/.m2/repository/org/clojure/clojure/1.11.1/clojure-1.11.1.jar:/home/sean/.m2/repository/org/clojure/core.specs.alpha/0.2.62/core.specs.alpha-0.2.62.jar:/home/sean/.m2/repository/org/clojure/spec.alpha/0.3.218/spec.alpha-0.3.218.jar" "clojure.main" "-e" "[(System/getenv \"JAVA_CMD\") (System/getProperty \"java.version\")]"]}
user=> (process *1)
["/Developer/jdk-19+36/bin/java" "17.0.4"]
{:exit 0}
user=>

seancorfield17:11:16

I tripped over this writing an external test runner for Polylith (which runs in a subprocess instead of using classloader isolation) and realized it was picking up the system default java so I added "find java" logic based on those env vars to fix it. Then I noticed various subprocesses in our build.clj were also picking up the system default java even tho' they were using java-command and so they were running with JDK 17 instead of JDK 19 (which the top-level build stuff was using since JAVA_CMD was set).

Alex Miller (Clojure team)17:11:17

I think there's a jira for this, haven't gotten to it yet

seancorfield17:11:29

That's just for compile-clj

Alex Miller (Clojure team)17:11:51

well feel free to expand its scope in the jira

seancorfield17:11:15

OK, wanted to check you were amenable to that.

Alex Miller (Clojure team)17:11:27

yes, compile-clj relies on java-command so the more generic fix here would cover both

seancorfield17:11:51

I left the title of TBUILD-34 as-is but added a comment suggesting a expansion of scope.

seancorfield17:11:56

I had missed that my patch for TDEPS-184 needed additional work, sorry. I don't think I got an email about your comment (or it went into spam).

seancorfield17:11:24

(I responded with some questions about approach)

seancorfield19:11:59

I updated my build-clj wrapper to do this defaulting since we needed it at work. Once there's an official solution in tools.build, I can remove it from build-clj (and also update my Polylith external test runner and part of our build.clj file at work -- our exec-command -- to match). Thanks!

Alex Miller (Clojure team)19:11:44

yeah, happy to take it on the next time I pass through tbuild

1
seancorfield19:11:27

io.github.seancorfield/build-clj {:git/tag "v0.8.5" :git/sha "de693d0"} -- Common tools.build tasks abstracted into a library, building on the examples in the https://clojure.org/guides/tools_build`tools.build`https://clojure.org/guides/tools_build: • Modify calls to compile-clj and java-command to default :java-cmd based on the JAVA_CMD and/or JAVA_HOME environment variables, if set. This won't be necessary when https://clojure.atlassian.net/browse/TBUILD-34 is addressed. • Update tools.build to v0.8.5 (for the snapshot/maven policy stuff in t.d.a.).