Skip to content

Commit

Permalink
Fix the debugger in case of a local shadows a var (clojure-emacs#847)
Browse files Browse the repository at this point in the history
  • Loading branch information
darkleaf authored Feb 21, 2024
1 parent ca4a5fe commit 3824d72
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## master (unreleased)

## Bugs fixed

* [#846](https://github.com/clojure-emacs/cider-nrepl/issues/846): `middleware.debug`: use `resolve` with `&env` to fix debugging in case of a local shadows a var.

## 0.45.0 (2024-01-14)

### Changes
Expand Down
31 changes: 17 additions & 14 deletions src/cider/nrepl/middleware/debug.clj
Original file line number Diff line number Diff line change
Expand Up @@ -447,17 +447,21 @@ this map (identified by a key), and will `dissoc` it afterwards."}

(defn looks-step-innable?
"Decide whether a form looks like a call to a function that we could
instrument and step into."
[form]
(when (and (seq? form) (symbol? (first form)))
(let [v (resolve (first form)) ; Note: special forms resolve to nil
m (meta v)]
;; We do not go so far as to actually try to read the code of the function
;; at this point, which is at macroexpansion time.
(and v
(safe-to-debug? (:ns m))
(not (:macro m))
(not (:inline m))))))
instrument and step into.
You should prefer the second arity with the `&env` argument
to handle a local shadowing correctly."
([form]
(looks-step-innable? nil form))
([&env form]
(when (and (seq? form) (symbol? (first form)))
(let [v (resolve &env (first form)) ; Note: special forms resolve to nil
m (meta v)]
;; We do not go so far as to actually try to read the code of the function
;; at this point, which is at macroexpansion time.
(and v
(safe-to-debug? (:ns m))
(not (:macro m))
(not (:inline m)))))))

;;; ## Breakpoint logic

Expand Down Expand Up @@ -547,7 +551,7 @@ this map (identified by a key), and will `dissoc` it afterwards."}
(defmacro expand-break
"Internal macro to avoid code repetition in `breakpoint-if-interesting`."
[form {:keys [coor]} original-form]
(let [val-form (if (looks-step-innable? form)
(let [val-form (if (looks-step-innable? &env form)
(let [[fn-sym & args] form]
`(apply-instrumented-maybe (var ~fn-sym) [~@args] ~coor ~'STATE__))
form)
Expand All @@ -570,9 +574,8 @@ this map (identified by a key), and will `dissoc` it afterwards."}
`irrelevant-return-value-forms`."
[&env form]
(or (and (symbol? form)
(not (contains? &env form))
(try
(-> (resolve form) meta :ns
(-> (resolve &env form) meta :ns
ns-name #{'clojure.core 'schema.core})
(catch Exception _ nil)))
(and (seq? form)
Expand Down

0 comments on commit 3824d72

Please sign in to comment.