Skip to content

Commit efcdf50

Browse files
committed
compile-clj - add stream control args for compilation so out and err can be captured
1 parent 5cfc4e0 commit efcdf50

File tree

6 files changed

+54
-9
lines changed

6 files changed

+54
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ Changelog
22
===========
33

44
* next
5+
* compile-clj - add stream control args for compilation so out and err can be captured
56
* java-command - remove assert that :basis is required (that is no longer true)
67
* v0.10.5 2a21b7a on July 12, 2024
78
* compile-clj - fix ordering of namespaces not included in topo sort

src/main/clojure/clojure/tools/build/api.clj

+7-1
Original file line numberDiff line numberDiff line change
@@ -307,8 +307,14 @@
307307
:auto (default) - use only if os=windows && Java >= 9 && command length >= 8k
308308
:always - always write classpath to temp file and include
309309
:never - never write classpath to temp file (pass on command line)
310+
:out - one of :inherit :capture :write :append :ignore
311+
:err - one of :inherit :capture :write :append :ignore
312+
:out-file - file path to write if :out is :write or :append
313+
:err-file - file path to write if :err is :write or :append
310314
311-
Returns nil."
315+
Returns nil, or if needed a map with keys:
316+
:out captured-out
317+
:err captured-err"
312318
[params]
313319
(assert-required "compile-clj" params [:basis :class-dir])
314320
(assert-specs "compile-clj" params

src/main/clojure/clojure/tools/build/tasks/compile_clj.clj

+14-8
Original file line numberDiff line numberDiff line change
@@ -96,19 +96,25 @@
9696

9797
;; java-command will run in context of *project-dir* - basis, classpaths, etc
9898
;; should all be relative to that (or absolute like working-compile-dir)
99-
process-args (process/java-command (merge
100-
(select-keys params [:java-cmd :java-opts :use-cp-file])
101-
{:cp [(.getPath working-compile-dir) class-dir]
102-
:basis basis
103-
:main 'clojure.main
104-
:main-args [(.getCanonicalPath compile-script)]}))
99+
process-args (merge
100+
(process/java-command
101+
(merge
102+
(select-keys params [:java-cmd :java-opts :use-cp-file])
103+
{:cp [(.getPath working-compile-dir) class-dir]
104+
:basis basis
105+
:main 'clojure.main
106+
:main-args [(.getCanonicalPath compile-script)]}))
107+
(select-keys params [:out :err :out-file :err-file]))
105108
_ (spit (jio/file working-dir "compile.args") (str/join " " (:command-args process-args)))
106-
exit (:exit (process/process process-args))]
109+
{exit :exit, ps-out :out, ps-err :err} (process/process process-args)]
107110
(if (zero? exit)
108111
(do
109112
(if (seq filter-nses)
110113
(file/copy-contents working-compile-dir compile-dir-file (map ns->path filter-nses))
111114
(file/copy-contents working-compile-dir compile-dir-file))
112115
;; only delete on success, otherwise leave the evidence!
113-
(file/delete working-dir))
116+
(file/delete working-dir)
117+
(cond-> nil
118+
ps-out (assoc :out ps-out)
119+
ps-err (assoc :err ps-err)))
114120
(throw (ex-info (str "Clojure compilation failed, working dir preserved: " (.toString working-dir)) {})))))

src/test/clojure/clojure/tools/build/tasks/test_compile_clj.clj

+21
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,27 @@
8383
(api/compile-clj (assoc compile-params :bindings {#'clojure.core/*assert* false})) ;; turn off asserts
8484
(is (= {:exit 0, :out (str "100" (System/lineSeparator))} (invoke))))))
8585

86+
(deftest test-capture-reflection
87+
(with-test-dir "test-data/reflecting"
88+
(api/set-project-root! (.getAbsolutePath *test-dir*))
89+
(let [basis (api/create-basis nil)
90+
compile-params {:class-dir "target/classes"
91+
:src-dirs ["src"]
92+
:basis basis
93+
:ns-compile ['foo.bar]}]
94+
95+
;; by default, reflection does not warn
96+
(is (nil? (api/compile-clj compile-params))) ;; no :bindings set
97+
98+
;; compile with reflection warnings and capture the error output
99+
(api/delete {:path "target/classes"})
100+
(is (str/starts-with?
101+
(:err
102+
(api/compile-clj (merge compile-params
103+
{:bindings {#'clojure.core/*warn-on-reflection* true}
104+
:err :capture})))
105+
"Reflection warning")))))
106+
86107
(deftest test-accidental-basis-delay
87108
(with-test-dir "test-data/p1"
88109
(api/set-project-root! (.getAbsolutePath *test-dir*))

test-data/reflecting/deps.edn

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{:paths ["src"]
2+
:deps
3+
{org.clojure/clojure {:mvn/version "1.12.0"}}
4+
}

test-data/reflecting/src/foo/bar.clj

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
(ns foo.bar
2+
(:gen-class))
3+
4+
(defn foo [s] (.length s))
5+
6+
(defn -main [& args]
7+
(println (foo "abc")))

0 commit comments

Comments
 (0)