Skip to content

Commit e8c4dfb

Browse files
authored
Fix build-graph crash on non-Clojure source files (#531) (#810)
Skip `parse-file` for sources that aren't `.clj`/`.cljc`/`.cljs`/`.bb` (e.g. `.py` files from libpython-clj vars); hash them opaquely like jars. Fixes #531.
1 parent 6e37fe6 commit e8c4dfb

3 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/nextjournal/clerk/analyzer.clj

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -550,8 +550,11 @@
550550
(let [jar? (or (nil? source)
551551
(str/ends-with? source ".jar"))
552552
gitlib-hash (and (not jar?)
553-
(second (re-find #".gitlibs/libs/.*/(\b[0-9a-f]{5,40}\b)/" (utils/unixify source))))]
554-
(if (or jar? gitlib-hash)
553+
(second (re-find #".gitlibs/libs/.*/(\b[0-9a-f]{5,40}\b)/" (utils/unixify source))))
554+
clojure-source? (and source
555+
(some #(str/ends-with? source %)
556+
[".clj" ".cljc" ".cljs" ".bb"]))]
557+
(if (or jar? gitlib-hash (not clojure-source?))
555558
(update g :->analysis-info merge (into {} (map (juxt identity
556559
(constantly (if source
557560
(or (when gitlib-hash {:hash gitlib-hash})

test/nextjournal/clerk/analyzer_test.clj

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,15 @@ my-uuid")]
390390
(is (match? {:jar string?} (->analysis-info 'weavejester.dependency/graph)))))
391391
(testing "should establish dependencies across files"
392392
(let [{:keys [graph]} (analyze-string (slurp "src/nextjournal/clerk.clj"))]
393-
(is (dep/depends? graph 'nextjournal.clerk/show! 'nextjournal.clerk.analyzer/hash)))))
393+
(is (dep/depends? graph 'nextjournal.clerk/show! 'nextjournal.clerk.analyzer/hash))))
394+
(testing "does not try to parse non-Clojure source files (issue #531)"
395+
;; Mimic libpython-clj: a var whose :file meta points to a Python file.
396+
(def issue-531-py-var 1)
397+
;; Absolute path, matching how libpython-clj writes :file meta — also forces
398+
;; `var->location` down its on-disk branch so the bad code path is exercised.
399+
(alter-meta! #'issue-531-py-var assoc :file
400+
(str (fs/absolutize (fs/file "test" "nextjournal" "clerk" "fixtures" "issue_531_not_clojure.py"))))
401+
(is (analyze-string "(ns consumer) (def use-py (nextjournal.clerk.analyzer-test/issue-531-py-var))"))))
394402

395403
(deftest graph-nodes-with-anonymous-ids
396404
(testing "nodes with \"anonymous ids\" from dependencies in foreign files respect graph dependencies"
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import numpy as np
2+
3+
4+
class BagObj:
5+
def __init__(self, obj):
6+
self._obj = obj

0 commit comments

Comments
 (0)