tools-build

seancorfield 2022-11-29T17:37:23.692509Z

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 ๐Ÿงต

seancorfield 2022-11-29T17:37:33.777809Z

(~/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=>

seancorfield 2022-11-29T17:40:16.903619Z

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) 2022-11-29T17:43:17.954639Z

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

Alex Miller (Clojure team) 2022-11-29T17:44:09.758149Z

https://clojure.atlassian.net/browse/TBUILD-34 ?

seancorfield 2022-11-29T17:44:29.646719Z

That's just for compile-clj

Alex Miller (Clojure team) 2022-11-29T17:44:51.565249Z

well feel free to expand its scope in the jira

seancorfield 2022-11-29T17:45:15.758749Z

OK, wanted to check you were amenable to that.

Alex Miller (Clojure team) 2022-11-29T17:47:27.477419Z

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

seancorfield 2022-11-29T17:50:51.069279Z

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

seancorfield 2022-11-29T17:52:56.582359Z

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).

seancorfield 2022-11-29T17:53:24.415219Z

(I responded with some questions about approach)

seancorfield 2022-11-29T19:39:59.310819Z

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) 2022-11-29T19:54:44.209849Z

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

๐Ÿ‘๐Ÿป 1
seancorfield 2022-11-29T19:37:27.733499Z

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.).