Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Preserve *ns* during analysis and eval #173

Merged
merged 1 commit into from
Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion resources/viewer-js-hash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
MZAFz7cCjz5iXe9gpb2mNybfmhh
2QomkaERveEgZwKtSGc6f95nabbt
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not have changed given the file touched, I believe.

13 changes: 7 additions & 6 deletions src/nextjournal/clerk.clj
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
(try
(let [value (let [cached-value (thaw-from-cas cas-hash)]
(when introduced-var
(intern *ns* (-> introduced-var symbol name symbol) cached-value))
(intern (-> introduced-var symbol namespace find-ns) (-> introduced-var symbol name symbol) cached-value))
cached-value)]
(wrapped-with-metadata (if introduced-var (var-from-def introduced-var) value) visibility hash))
(catch Exception _e
Expand Down Expand Up @@ -189,10 +189,11 @@


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

(defn parse-file [file]
(hashing/parse-file {:doc? true} file))
Expand Down Expand Up @@ -495,7 +496,7 @@
_ (report-fn {:stage :init :state state})
{state :result duration :time-ms} (time-ms (mapv (comp parse-file :file) state))
_ (report-fn {:stage :parsed :state state :duration duration})
{state :result duration :time-ms} (time-ms (mapv (comp (fn [doc] (assoc doc :->hash (hashing/hash doc) :ns *ns*))
{state :result duration :time-ms} (time-ms (mapv (comp (fn [doc] (assoc doc :->hash (hashing/hash doc)))
hashing/build-graph) state))
_ (report-fn {:stage :analyzed :state state :duration duration})
state (mapv (fn [doc]
Expand Down
44 changes: 23 additions & 21 deletions src/nextjournal/clerk/hashing.clj
Original file line number Diff line number Diff line change
Expand Up @@ -328,27 +328,29 @@
([doc]
(analyze-doc {:doc? true :graph (dep/graph)} doc))
([{:as state :keys [doc?]} doc]
(reduce (fn [state i]
(let [{:keys [type text]} (get-in state [:blocks i])]
(if (not= type :code)
state
(let [form (read-string text)
{:as analyzed :keys [vars deps ns-effect?]} (cond-> (analyze form)
(:file doc) (assoc :file (:file doc)))
state (cond-> (reduce (fn [state ana-key]
(assoc-in state [:->analysis-info ana-key] analyzed))
(dissoc state :doc?)
(->ana-keys analyzed))
doc? (update-in [:blocks i] merge (dissoc analyzed :deps :no-cache? :ns-effect?)))]
(when ns-effect?
(eval form))
(if (seq deps)
(-> (reduce (partial analyze-deps analyzed) state deps)
(make-deps-inherit-no-cache analyzed))
state)))))
(cond-> state
doc? (merge doc))
(-> doc :blocks count range))))
(binding [*ns* *ns*]
(reduce (fn [state i]
(let [{:keys [type text]} (get-in state [:blocks i])]
(if (not= type :code)
state
(let [form (read-string text)
{:as analyzed :keys [vars deps ns-effect?]} (cond-> (analyze form)
(:file doc) (assoc :file (:file doc)))
state (cond-> (reduce (fn [state ana-key]
(assoc-in state [:->analysis-info ana-key] analyzed))
(dissoc state :doc?)
(->ana-keys analyzed))
doc? (update-in [:blocks i] merge (dissoc analyzed :deps :no-cache? :ns-effect?))
doc? (assoc :ns *ns*))]
(when ns-effect?
(eval form))
(if (seq deps)
(-> (reduce (partial analyze-deps analyzed) state deps)
(make-deps-inherit-no-cache analyzed))
state)))))
(cond-> state
doc? (merge doc))
(-> doc :blocks count range)))))

#_(let [doc (parse-clojure-string {:doc? true} "(ns foo) (def a 41) (def b (inc a)) (do (def c 4) (def d (inc a)))")]
(analyze-doc doc))
Expand Down
5 changes: 3 additions & 2 deletions src/nextjournal/clerk/webserver.clj
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,9 @@
#_(get-pagination-opts "")
#_(get-pagination-opts "foo=bar&n=42&start=20")

(defn serve-blob [{:as _doc :keys [blob->result ns]} {:keys [blob-id fetch-opts]}]
(assert ns "namespace must be set")
(defn serve-blob [{:as doc :keys [blob->result ns]} {:keys [blob-id fetch-opts]}]
(when-not ns
(throw (ex-info "namespace must be set" {:doc doc})))
(if (contains? blob->result blob-id)
(let [result (v/apply-viewer-unwrapping-var-from-def (blob->result blob-id))
desc (v/present (v/ensure-wrapped-with-viewers
Expand Down
25 changes: 16 additions & 9 deletions test/nextjournal/clerk/hashing_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -78,16 +78,18 @@ par one
par two"))))

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

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

(with-ns-binding 'nextjournal.clerk.hashing
(is (nextjournal.clerk.hashing/no-cache? '(rand-int 10))))))
Expand Down Expand Up @@ -197,7 +199,12 @@ par two"))))
:deps set?}
#{1 3 2} {:form '#{1 3 2}}}}
(analyze-string "^:nextjournal.clerk/no-cache (ns example-notebook)
#{3 1 2}"))))
#{3 1 2}")))

(testing "preserves *ns*"
(with-ns-binding 'nextjournal.clerk.hashing-test
(is (= (find-ns 'nextjournal.clerk.hashing-test)
(do (analyze-string "(ns example-notebook)") *ns*))))))

(deftest no-cache-dep
(is (match? [{:no-cache? true} {:no-cache? true} {:no-cache? true}]
Expand Down
1 change: 1 addition & 0 deletions test/nextjournal/clerk_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -184,3 +184,4 @@
(let [paths (clerk/expand-paths ["notebooks/*clj"])]
(is (> (count paths) 25))
(is (every? #(str/ends-with? % ".clj") paths))))