Skip to content

Commit

Permalink
compile-clj - add stream control args for compilation so out and err …
Browse files Browse the repository at this point in the history
…can be captured
  • Loading branch information
puredanger committed Nov 22, 2024
1 parent 5cfc4e0 commit efcdf50
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Changelog
===========

* next
* compile-clj - add stream control args for compilation so out and err can be captured
* java-command - remove assert that :basis is required (that is no longer true)
* v0.10.5 2a21b7a on July 12, 2024
* compile-clj - fix ordering of namespaces not included in topo sort
Expand Down
8 changes: 7 additions & 1 deletion src/main/clojure/clojure/tools/build/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -307,8 +307,14 @@
:auto (default) - use only if os=windows && Java >= 9 && command length >= 8k
:always - always write classpath to temp file and include
:never - never write classpath to temp file (pass on command line)
:out - one of :inherit :capture :write :append :ignore
:err - one of :inherit :capture :write :append :ignore
:out-file - file path to write if :out is :write or :append
:err-file - file path to write if :err is :write or :append
Returns nil."
Returns nil, or if needed a map with keys:
:out captured-out
:err captured-err"
[params]
(assert-required "compile-clj" params [:basis :class-dir])
(assert-specs "compile-clj" params
Expand Down
22 changes: 14 additions & 8 deletions src/main/clojure/clojure/tools/build/tasks/compile_clj.clj
Original file line number Diff line number Diff line change
Expand Up @@ -96,19 +96,25 @@

;; java-command will run in context of *project-dir* - basis, classpaths, etc
;; should all be relative to that (or absolute like working-compile-dir)
process-args (process/java-command (merge
(select-keys params [:java-cmd :java-opts :use-cp-file])
{:cp [(.getPath working-compile-dir) class-dir]
:basis basis
:main 'clojure.main
:main-args [(.getCanonicalPath compile-script)]}))
process-args (merge
(process/java-command
(merge
(select-keys params [:java-cmd :java-opts :use-cp-file])
{:cp [(.getPath working-compile-dir) class-dir]
:basis basis
:main 'clojure.main
:main-args [(.getCanonicalPath compile-script)]}))
(select-keys params [:out :err :out-file :err-file]))
_ (spit (jio/file working-dir "compile.args") (str/join " " (:command-args process-args)))
exit (:exit (process/process process-args))]
{exit :exit, ps-out :out, ps-err :err} (process/process process-args)]
(if (zero? exit)
(do
(if (seq filter-nses)
(file/copy-contents working-compile-dir compile-dir-file (map ns->path filter-nses))
(file/copy-contents working-compile-dir compile-dir-file))
;; only delete on success, otherwise leave the evidence!
(file/delete working-dir))
(file/delete working-dir)
(cond-> nil
ps-out (assoc :out ps-out)
ps-err (assoc :err ps-err)))
(throw (ex-info (str "Clojure compilation failed, working dir preserved: " (.toString working-dir)) {})))))
21 changes: 21 additions & 0 deletions src/test/clojure/clojure/tools/build/tasks/test_compile_clj.clj
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,27 @@
(api/compile-clj (assoc compile-params :bindings {#'clojure.core/*assert* false})) ;; turn off asserts
(is (= {:exit 0, :out (str "100" (System/lineSeparator))} (invoke))))))

(deftest test-capture-reflection
(with-test-dir "test-data/reflecting"
(api/set-project-root! (.getAbsolutePath *test-dir*))
(let [basis (api/create-basis nil)
compile-params {:class-dir "target/classes"
:src-dirs ["src"]
:basis basis
:ns-compile ['foo.bar]}]

;; by default, reflection does not warn
(is (nil? (api/compile-clj compile-params))) ;; no :bindings set

;; compile with reflection warnings and capture the error output
(api/delete {:path "target/classes"})
(is (str/starts-with?
(:err
(api/compile-clj (merge compile-params
{:bindings {#'clojure.core/*warn-on-reflection* true}
:err :capture})))
"Reflection warning")))))

(deftest test-accidental-basis-delay
(with-test-dir "test-data/p1"
(api/set-project-root! (.getAbsolutePath *test-dir*))
Expand Down
4 changes: 4 additions & 0 deletions test-data/reflecting/deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:paths ["src"]
:deps
{org.clojure/clojure {:mvn/version "1.12.0"}}
}
7 changes: 7 additions & 0 deletions test-data/reflecting/src/foo/bar.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
(ns foo.bar
(:gen-class))

(defn foo [s] (.length s))

(defn -main [& args]
(println (foo "abc")))

0 comments on commit efcdf50

Please sign in to comment.