Skip to content

Commit 6e3a671

Browse files
committedJan 28, 2025·
Don't throw on warnings; don't crash watch-and-recompile on errors
Addresses #40.
1 parent b8fc536 commit 6e3a671

File tree

6 files changed

+48
-7
lines changed

6 files changed

+48
-7
lines changed
 

‎src/virgil.clj

+6-2
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22
(:require
33
[clojure.java.io :as io]
44
[virgil.watch :refer (watch-directory make-idle-callback)]
5-
[virgil.compile :refer (compile-all-java java-file?)]))
5+
[virgil.compile :refer (compile-all-java java-file?)]
6+
virgil.util))
67

78
(def watches (atom #{}))
89

910
(defn compile-java [directories & {:keys [options verbose]}]
10-
(compile-all-java directories options verbose))
11+
(let [diags (compile-all-java directories options verbose)]
12+
(when (virgil.util/compilation-errored? diags)
13+
(throw (ex-info (format "Compilation failed: %d error(s)." (count diags))
14+
{:diagnostics diags})))))
1115

1216
(defn watch-and-recompile [directories & {:keys [options verbose post-hook]}]
1317
(let [recompile (fn []

‎src/virgil/compile.clj

+2-3
Original file line numberDiff line numberDiff line change
@@ -151,10 +151,9 @@
151151
(let [collector (DiagnosticCollector.)
152152
options (ArrayList. (vec options))
153153
name->source (generate-classname->source directories)]
154-
(println "Compiling" (count name->source)"Java source files in" directories "...")
154+
(println "\nCompiling" (count name->source)"Java source files in" directories "...")
155155
(binding [*print-compiled-classes* verbose?]
156156
(compile-java options collector name->source))
157157
(when-let [diags (seq (.getDiagnostics collector))]
158158
(print-diagnostics diags)
159-
(throw (ex-info (format "Compilation failed: %d error(s)." (count diags))
160-
{:diagnostics diags}))))))
159+
diags))))

‎src/virgil/util.clj

+5-2
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
(let [k (.getKind d)
2121
log (infer-print-function k)]
2222
(if (nil? (.getSource d))
23-
(println-err (format "%s: %s\n"
23+
(println-err (format "%s: %s"
2424
(.toString k)
2525
(.getMessage d nil)))
26-
(println-err (format "%s: %s, line %d: %s\n"
26+
(println-err (format "%s: %s, line %d: %s"
2727
(.toString k)
2828
(.. d getSource getName)
2929
(.getLineNumber d)
3030
(.getMessage d nil)))))))
31+
32+
(defn compilation-errored? [diagnostics]
33+
(some #(= (.getKind ^Diagnostic %) Diagnostic$Kind/ERROR) diagnostics))

‎test/ClassWithError.java

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package virgil;
2+
3+
import java.util.*;
4+
5+
public class ClassWithWarning {
6+
7+
public static void badReturn() {
8+
return 1 + 2;
9+
}
10+
}

‎test/ClassWithWarning.java

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package virgil;
2+
3+
import java.util.*;
4+
5+
public class ClassWithWarning {
6+
7+
public static void rawType() {
8+
List list = new ArrayList();
9+
list.add("Hello");
10+
}
11+
}

‎test/virgil_test.clj

+14
Original file line numberDiff line numberDiff line change
@@ -81,3 +81,17 @@
8181
(cp "B" 'B)
8282
(wait-until-true #(= 42 (magic-number)))
8383
(is (= 42 (magic-number)))))
84+
85+
(deftest warnings-shouldnt-throw-test
86+
(binding [*dir* (mk-tmp)]
87+
(cp "ClassWithWarning" 'ClassWithWarning)
88+
(is (nil? (recompile))))
89+
90+
(binding [*dir* (mk-tmp)]
91+
(cp "ClassWithError" 'ClassWithError)
92+
(is (thrown? clojure.lang.ExceptionInfo (recompile)))))
93+
94+
(deftest errors-shouldnt-break-watch-and-recompile-test
95+
(binding [*dir* (mk-tmp)]
96+
(cp "ClassWithError" 'ClassWithError)
97+
(is (nil? (virgil/watch-and-recompile [(str *dir*)])))))

0 commit comments

Comments
 (0)