Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't throw on warnings; don't crash watch-and-recompile on errors
Browse files Browse the repository at this point in the history
Addresses #40.
alexander-yakushev committed Jan 28, 2025
1 parent b8fc536 commit c2ac862
Showing 4 changed files with 27 additions and 7 deletions.
8 changes: 6 additions & 2 deletions src/virgil.clj
Original file line number Diff line number Diff line change
@@ -2,12 +2,16 @@
(:require
[clojure.java.io :as io]
[virgil.watch :refer (watch-directory make-idle-callback)]
[virgil.compile :refer (compile-all-java java-file?)]))
[virgil.compile :refer (compile-all-java java-file?)]
virgil.util))

(def watches (atom #{}))

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

(defn watch-and-recompile [directories & {:keys [options verbose post-hook]}]
(let [recompile (fn []
5 changes: 2 additions & 3 deletions src/virgil/compile.clj
Original file line number Diff line number Diff line change
@@ -151,10 +151,9 @@
(let [collector (DiagnosticCollector.)
options (ArrayList. (vec options))
name->source (generate-classname->source directories)]
(println "Compiling" (count name->source)"Java source files in" directories "...")
(println "\nCompiling" (count name->source)"Java source files in" directories "...")
(binding [*print-compiled-classes* verbose?]
(compile-java options collector name->source))
(when-let [diags (seq (.getDiagnostics collector))]
(print-diagnostics diags)
(throw (ex-info (format "Compilation failed: %d error(s)." (count diags))
{:diagnostics diags}))))))
diags))))
7 changes: 5 additions & 2 deletions src/virgil/util.clj
Original file line number Diff line number Diff line change
@@ -20,11 +20,14 @@
(let [k (.getKind d)
log (infer-print-function k)]
(if (nil? (.getSource d))
(println-err (format "%s: %s\n"
(println-err (format "%s: %s"
(.toString k)
(.getMessage d nil)))
(println-err (format "%s: %s, line %d: %s\n"
(println-err (format "%s: %s, line %d: %s"
(.toString k)
(.. d getSource getName)
(.getLineNumber d)
(.getMessage d nil)))))))

(defn compilation-errored? [diagnostics]
(some #(= (.getKind ^Diagnostic %) Diagnostic$Kind/ERROR) diagnostics))
14 changes: 14 additions & 0 deletions test/virgil_test.clj
Original file line number Diff line number Diff line change
@@ -81,3 +81,17 @@
(cp "B" 'B)
(wait-until-true #(= 42 (magic-number)))
(is (= 42 (magic-number)))))

(deftest warnings-shouldnt-throw-test
(binding [*dir* (mk-tmp)]
(cp "ClassWithWarning" 'ClassWithWarning)
(is (nil? (recompile))))

(binding [*dir* (mk-tmp)]
(cp "ClassWithError" 'ClassWithError)
(is (thrown? clojure.lang.ExceptionInfo (recompile)))))

(deftest errors-shouldnt-break-watch-and-recompile-test
(binding [*dir* (mk-tmp)]
(cp "ClassWithError" 'ClassWithError)
(is (nil? (virgil/watch-and-recompile [(str *dir*)])))))

0 comments on commit c2ac862

Please sign in to comment.