Skip to content

Commit 68524a8

Browse files
author
dnolen
committed
CLJS-1578: Corrupted Analysis Files Break Compilation
Don't assume reading the analysis cache is going to work. For example it may have only been partially written out. If a error is thrown during reading the cache, retry and skip the analysis cache stuff.
1 parent 1e486d2 commit 68524a8

File tree

1 file changed

+46
-39
lines changed

1 file changed

+46
-39
lines changed

src/main/clojure/cljs/analyzer.cljc

Lines changed: 46 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
[cljs.reader :as edn]))
3535
#?(:clj (:import [java.io File Reader PushbackReader]
3636
[java.net URL]
37+
[java.lang Throwable]
3738
[clojure.lang Namespace Var LazySeq ArityException]
3839
[cljs.tagged_literals JSValue])))
3940

@@ -2840,57 +2841,63 @@
28402841
compiler options, if :cache-analysis true will cache analysis to
28412842
\":output-dir/some/ns/foo.cljs.cache.edn\". This function does not return a
28422843
meaningful value."
2843-
([f] (analyze-file f nil))
2844+
([f]
2845+
(analyze-file f nil))
28442846
([f opts]
2847+
(analyze-file f false opts))
2848+
([f skip-cache opts]
28452849
(binding [*file-defs* (atom #{})]
28462850
(let [output-dir (util/output-directory opts)
2847-
res (cond
2848-
(instance? File f) f
2849-
(instance? URL f) f
2850-
(re-find #"^file://" f) (URL. f)
2851-
:else (io/resource f))]
2851+
res (cond
2852+
(instance? File f) f
2853+
(instance? URL f) f
2854+
(re-find #"^file://" f) (URL. f)
2855+
:else (io/resource f))]
28522856
(assert res (str "Can't find " f " in classpath"))
28532857
(ensure
28542858
(let [ns-info (parse-ns res)
2855-
path (if (instance? File res)
2856-
(.getPath ^File res)
2857-
(.getPath ^URL res))
2858-
cache (when (:cache-analysis opts)
2859-
(cache-file res ns-info output-dir))]
2859+
path (if (instance? File res)
2860+
(.getPath ^File res)
2861+
(.getPath ^URL res))
2862+
cache (when (:cache-analysis opts)
2863+
(cache-file res ns-info output-dir))]
28602864
(when-not (get-in @env/*compiler* [::namespaces (:ns ns-info) :defs])
2861-
(if (or (not cache) (requires-analysis? res output-dir))
2865+
(if (or skip-cache (not cache) (requires-analysis? res output-dir))
28622866
(binding [*cljs-ns* 'cljs.user
28632867
*cljs-file* path
28642868
reader/*alias-map* (or reader/*alias-map* {})]
28652869
(when (or *verbose* (:verbose opts))
28662870
(util/debug-prn "Analyzing" (str res)))
28672871
(let [env (assoc (empty-env) :build-options opts)
2868-
ns (with-open [rdr (io/reader res)]
2869-
(loop [ns nil forms (seq (forms-seq* rdr (util/path res)))]
2870-
(if forms
2871-
(let [form (first forms)
2872-
env (assoc env :ns (get-namespace *cljs-ns*))
2873-
ast (analyze env form nil opts)]
2874-
(if (= (:op ast) :ns)
2875-
(recur (:name ast) (next forms))
2876-
(recur ns (next forms))))
2877-
ns)))]
2872+
ns (with-open [rdr (io/reader res)]
2873+
(loop [ns nil forms (seq (forms-seq* rdr (util/path res)))]
2874+
(if forms
2875+
(let [form (first forms)
2876+
env (assoc env :ns (get-namespace *cljs-ns*))
2877+
ast (analyze env form nil opts)]
2878+
(if (= (:op ast) :ns)
2879+
(recur (:name ast) (next forms))
2880+
(recur ns (next forms))))
2881+
ns)))]
28782882
(when (and cache (true? (:cache-analysis opts)))
28792883
(write-analysis-cache ns cache res))))
2880-
;; we want want to keep dependency analysis information
2881-
;; don't revert the environment - David
2882-
(let [{:keys [ns]}
2883-
(parse-ns res
2884-
(merge opts
2885-
{:restore false
2886-
:analyze-deps true
2887-
:load-macros true}))]
2888-
(when (or *verbose* (:verbose opts))
2889-
(util/debug-prn "Reading analysis cache for" (str res)))
2890-
(swap! env/*compiler*
2891-
(fn [cenv]
2892-
(let [cached-ns (edn/read-string (slurp cache))]
2893-
(doseq [x (get-in cached-ns [::constants :order])]
2894-
(register-constant! x))
2895-
(-> cenv
2896-
(assoc-in [::namespaces ns] cached-ns)))))))))))))))
2884+
(try
2885+
;; we want want to keep dependency analysis information
2886+
;; don't revert the environment - David
2887+
(let [{:keys [ns]} (parse-ns res
2888+
(merge opts
2889+
{:restore false
2890+
:analyze-deps true
2891+
:load-macros true}))
2892+
cached-ns (edn/read-string (slurp cache))]
2893+
(when (or *verbose* (:verbose opts))
2894+
(util/debug-prn "Reading analysis cache for" (str res)))
2895+
(swap! env/*compiler*
2896+
(fn [cenv]
2897+
(let []
2898+
(doseq [x (get-in cached-ns [::constants :order])]
2899+
(register-constant! x))
2900+
(-> cenv
2901+
(assoc-in [::namespaces ns] cached-ns))))))
2902+
(catch Throwable e
2903+
(analyze-file f true opts))))))))))))

0 commit comments

Comments
 (0)