# ClojureScript Warnings as Errors We recently ran into a problem at Nextjournal when we deployed a bad build to production. It turned out after renaming a var, we overlooked one instance. However, our CI didn't catch that. Let's see how the ClojureScript compiler behaves with a source file using an undefined namespace. ```clojure no-exec id=eeb8f3d6-c97a-4437-b95e-0f5d4f6936ce {:deps {org.clojure/clojure {:mvn/version "1.10.1"} org.clojure/clojurescript {:mvn/version "1.10.597"}}} ``` ```clojurescript no-exec id=191702c8-d707-4f02-9de6-650f7ed10f1f (ns foo.bar) (bam/doesnt-exist) ``` Build it with the default compiler options. ```clojure id=8324fe8a-885d-4c5e-8862-e98ef8bdf201 (require 'cljs.build.api 'cljs.analyzer.api) (cljs.build.api/build "src" {:output-to "out/main.js"}) ``` As we see, it produces warnings, but the build is successful anyway. This is not desirable, we want the build to fail. To make it fail, we have to build it with a custom warning handler to throw an error in addition to logging them, but clean the previous build first. ```bash id=b204815a-bd6e-4049-a606-c0dd1a0572d9 rm -rf out ``` ```clojure id=387f934a-7cb9-4918-8760-176291f4cbc4 (require 'cljs.build.api 'cljs.analyzer.api) (cljs.build.api/build "src" {:output-to "out/main.js" :warning-handlers [cljs.analyzer.api/default-warning-handler (fn [warning-type env extra] (when (warning-type cljs.analyzer/*cljs-warnings*) (throw (cljs.analyzer/error env (cljs.analyzer/error-message warning-type extra)))))]}) ``` 💥 Much better. Also interesting: [shadow-cljs has a dedicated compiler option for this](https://shadow-cljs.github.io/docs/UsersGuide.html#warnigs-as-errors).
This notebook was exported from https://nextjournal.com/a/Ly84fW9GtrqQVGNDa8CaJ?change-id=CfSLGQJAYZ1CAmfvy7PGqR ```edn nextjournal-metadata {:article {:nodes {"191702c8-d707-4f02-9de6-650f7ed10f1f" {:id "191702c8-d707-4f02-9de6-650f7ed10f1f", :kind "code-listing", :name "bar.cljs"}, "387f934a-7cb9-4918-8760-176291f4cbc4" {:compute-ref #uuid "c79b4c9b-c3ae-49a3-a0a4-47e4f0dc03fa", :exec-duration 2210, :id "387f934a-7cb9-4918-8760-176291f4cbc4", :kind "code", :output-log-lines {:stdout 1}, :refs (), :runtime [:runtime "3fc90bb7-ac52-4c3f-b037-d41295658d94"]}, "3fc90bb7-ac52-4c3f-b037-d41295658d94" {:environment [:environment {:article/nextjournal.id #uuid "5b45eb52-bad4-413d-9d7f-b2b573a25322", :change/nextjournal.id #uuid "5de7d569-43c3-4637-b332-217e5498c318", :node/id "0ae15688-6f6a-40e2-a4fa-52d81371f733"}], :id "3fc90bb7-ac52-4c3f-b037-d41295658d94", :kind "runtime", :language "clojure", :type :nextjournal, :runtime/mounts [{:src [:node "eeb8f3d6-c97a-4437-b95e-0f5d4f6936ce"], :dest "/deps.edn"} {:src [:node "191702c8-d707-4f02-9de6-650f7ed10f1f"], :dest "/src/foo/bar.cljs"}]}, "8324fe8a-885d-4c5e-8862-e98ef8bdf201" {:compute-ref #uuid "2dd52131-e547-421d-9e56-eff253cd9b89", :exec-duration 7131, :id "8324fe8a-885d-4c5e-8862-e98ef8bdf201", :kind "code", :output-log-lines {:stdout 2}, :refs (), :runtime [:runtime "3fc90bb7-ac52-4c3f-b037-d41295658d94"]}, "b204815a-bd6e-4049-a606-c0dd1a0572d9" {:compute-ref #uuid "4c9b6a60-ce23-43df-b71b-a0de866ba098", :exec-duration 209, :id "b204815a-bd6e-4049-a606-c0dd1a0572d9", :kind "code", :output-log-lines {}, :refs (), :runtime [:runtime "3fc90bb7-ac52-4c3f-b037-d41295658d94"]}, "eeb8f3d6-c97a-4437-b95e-0f5d4f6936ce" {:id "eeb8f3d6-c97a-4437-b95e-0f5d4f6936ce", :kind "code-listing", :name "deps.edn"}}, :nextjournal/id #uuid "02c9b7c8-a008-477f-8dba-7e57c1e26c7f", :article/change {:nextjournal/id #uuid "5e726667-c678-4ad3-822a-b40faafd7524"}}} ```