Skip to content

Commit a15016e

Browse files
authored
Preserve *ns* during analysis and eval (#173)
1 parent 0bcc6a0 commit a15016e

File tree

6 files changed

+51
-39
lines changed

6 files changed

+51
-39
lines changed

resources/viewer-js-hash

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
MZAFz7cCjz5iXe9gpb2mNybfmhh
1+
2QomkaERveEgZwKtSGc6f95nabbt

src/nextjournal/clerk.clj

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@
8181
(try
8282
(let [value (let [cached-value (thaw-from-cas cas-hash)]
8383
(when introduced-var
84-
(intern *ns* (-> introduced-var symbol name symbol) cached-value))
84+
(intern (-> introduced-var symbol namespace find-ns) (-> introduced-var symbol name symbol) cached-value))
8585
cached-value)]
8686
(wrapped-with-metadata (if introduced-var (var-from-def introduced-var) value) visibility hash))
8787
(catch Exception _e
@@ -189,10 +189,11 @@
189189

190190

191191
(defn +eval-results [results-last-run parsed-doc]
192-
(let [analyzed-doc (hashing/build-graph parsed-doc)]
193-
(-> analyzed-doc
194-
(assoc :blob->result results-last-run :->hash (hashing/hash analyzed-doc) :ns *ns*)
195-
eval-analyzed-doc)))
192+
(let [{:as analyzed-doc :keys [ns]} (hashing/build-graph parsed-doc)]
193+
(binding [*ns* ns]
194+
(-> analyzed-doc
195+
(assoc :blob->result results-last-run :->hash (hashing/hash analyzed-doc))
196+
eval-analyzed-doc))))
196197

197198
(defn parse-file [file]
198199
(hashing/parse-file {:doc? true} file))
@@ -495,7 +496,7 @@
495496
_ (report-fn {:stage :init :state state})
496497
{state :result duration :time-ms} (time-ms (mapv (comp parse-file :file) state))
497498
_ (report-fn {:stage :parsed :state state :duration duration})
498-
{state :result duration :time-ms} (time-ms (mapv (comp (fn [doc] (assoc doc :->hash (hashing/hash doc) :ns *ns*))
499+
{state :result duration :time-ms} (time-ms (mapv (comp (fn [doc] (assoc doc :->hash (hashing/hash doc)))
499500
hashing/build-graph) state))
500501
_ (report-fn {:stage :analyzed :state state :duration duration})
501502
state (mapv (fn [doc]

src/nextjournal/clerk/hashing.clj

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -328,27 +328,29 @@
328328
([doc]
329329
(analyze-doc {:doc? true :graph (dep/graph)} doc))
330330
([{:as state :keys [doc?]} doc]
331-
(reduce (fn [state i]
332-
(let [{:keys [type text]} (get-in state [:blocks i])]
333-
(if (not= type :code)
334-
state
335-
(let [form (read-string text)
336-
{:as analyzed :keys [vars deps ns-effect?]} (cond-> (analyze form)
337-
(:file doc) (assoc :file (:file doc)))
338-
state (cond-> (reduce (fn [state ana-key]
339-
(assoc-in state [:->analysis-info ana-key] analyzed))
340-
(dissoc state :doc?)
341-
(->ana-keys analyzed))
342-
doc? (update-in [:blocks i] merge (dissoc analyzed :deps :no-cache? :ns-effect?)))]
343-
(when ns-effect?
344-
(eval form))
345-
(if (seq deps)
346-
(-> (reduce (partial analyze-deps analyzed) state deps)
347-
(make-deps-inherit-no-cache analyzed))
348-
state)))))
349-
(cond-> state
350-
doc? (merge doc))
351-
(-> doc :blocks count range))))
331+
(binding [*ns* *ns*]
332+
(reduce (fn [state i]
333+
(let [{:keys [type text]} (get-in state [:blocks i])]
334+
(if (not= type :code)
335+
state
336+
(let [form (read-string text)
337+
{:as analyzed :keys [vars deps ns-effect?]} (cond-> (analyze form)
338+
(:file doc) (assoc :file (:file doc)))
339+
state (cond-> (reduce (fn [state ana-key]
340+
(assoc-in state [:->analysis-info ana-key] analyzed))
341+
(dissoc state :doc?)
342+
(->ana-keys analyzed))
343+
doc? (update-in [:blocks i] merge (dissoc analyzed :deps :no-cache? :ns-effect?))
344+
doc? (assoc :ns *ns*))]
345+
(when ns-effect?
346+
(eval form))
347+
(if (seq deps)
348+
(-> (reduce (partial analyze-deps analyzed) state deps)
349+
(make-deps-inherit-no-cache analyzed))
350+
state)))))
351+
(cond-> state
352+
doc? (merge doc))
353+
(-> doc :blocks count range)))))
352354

353355
#_(let [doc (parse-clojure-string {:doc? true} "(ns foo) (def a 41) (def b (inc a)) (do (def c 4) (def d (inc a)))")]
354356
(analyze-doc doc))

src/nextjournal/clerk/webserver.clj

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,9 @@
4141
#_(get-pagination-opts "")
4242
#_(get-pagination-opts "foo=bar&n=42&start=20")
4343

44-
(defn serve-blob [{:as _doc :keys [blob->result ns]} {:keys [blob-id fetch-opts]}]
45-
(assert ns "namespace must be set")
44+
(defn serve-blob [{:as doc :keys [blob->result ns]} {:keys [blob-id fetch-opts]}]
45+
(when-not ns
46+
(throw (ex-info "namespace must be set" {:doc doc})))
4647
(if (contains? blob->result blob-id)
4748
(let [result (v/apply-viewer-unwrapping-var-from-def (blob->result blob-id))
4849
desc (v/present (v/ensure-wrapped-with-viewers

test/nextjournal/clerk/hashing_test.clj

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -78,16 +78,18 @@ par one
7878
par two"))))
7979

8080
(deftest no-cache?
81-
(testing "are variables set to no-cache?"
82-
(is (not (h/no-cache? (h/analyze+emit '(rand-int 10)))))
83-
(is (not (h/no-cache? (h/analyze+emit '(def random-thing (rand-int 1000))))))
84-
(is (not (h/no-cache? (h/analyze+emit '(defn random-thing [] (rand-int 1000))))))
85-
(is (h/no-cache? (h/analyze+emit '(def ^:nextjournal.clerk/no-cache random-thing (rand-int 1000)))))
86-
(is (h/no-cache? (h/analyze+emit '(defn ^:nextjournal.clerk/no-cache random-thing [] (rand-int 1000)))))
87-
(is (h/no-cache? (h/analyze+emit '(defn ^{:nextjournal.clerk/no-cache true} random-thing [] (rand-int 1000))))))
81+
(with-ns-binding 'nextjournal.clerk.hashing-test
82+
(testing "are variables set to no-cache?"
83+
(is (not (h/no-cache? (h/analyze+emit '(rand-int 10)))))
84+
(is (not (h/no-cache? (h/analyze+emit '(def random-thing (rand-int 1000))))))
85+
(is (not (h/no-cache? (h/analyze+emit '(defn random-thing [] (rand-int 1000))))))
86+
(is (h/no-cache? (h/analyze+emit '(def ^:nextjournal.clerk/no-cache random-thing (rand-int 1000)))))
87+
(is (h/no-cache? (h/analyze+emit '(defn ^:nextjournal.clerk/no-cache random-thing [] (rand-int 1000)))))
88+
(is (h/no-cache? (h/analyze+emit '(defn ^{:nextjournal.clerk/no-cache true} random-thing [] (rand-int 1000)))))))
8889

8990
(testing "is evaluating namespace set to no-cache?"
90-
(is (not (h/no-cache? '(rand-int 10))))
91+
(with-ns-binding 'nextjournal.clerk.hashing-test
92+
(is (not (h/no-cache? '(rand-int 10)))))
9193

9294
(with-ns-binding 'nextjournal.clerk.hashing
9395
(is (nextjournal.clerk.hashing/no-cache? '(rand-int 10))))))
@@ -197,7 +199,12 @@ par two"))))
197199
:deps set?}
198200
#{1 3 2} {:form '#{1 3 2}}}}
199201
(analyze-string "^:nextjournal.clerk/no-cache (ns example-notebook)
200-
#{3 1 2}"))))
202+
#{3 1 2}")))
203+
204+
(testing "preserves *ns*"
205+
(with-ns-binding 'nextjournal.clerk.hashing-test
206+
(is (= (find-ns 'nextjournal.clerk.hashing-test)
207+
(do (analyze-string "(ns example-notebook)") *ns*))))))
201208

202209
(deftest no-cache-dep
203210
(is (match? [{:no-cache? true} {:no-cache? true} {:no-cache? true}]

test/nextjournal/clerk_test.clj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,3 +184,4 @@
184184
(let [paths (clerk/expand-paths ["notebooks/*clj"])]
185185
(is (> (count paths) 25))
186186
(is (every? #(str/ends-with? % ".clj") paths))))
187+

0 commit comments

Comments
 (0)