cider-nrepl icon indicating copy to clipboard operation
cider-nrepl copied to clipboard

Debugger incorrectly steps through conditional forms

Open vspinu opened this issue 6 years ago • 6 comments

Once #435 is in, try

#dbg
(defn tt [a]
  (let [b (+ a 1)]
    #?(:clj (do
              (loop [b b]
                (if (pos? b)
                  (recur (- b 1))
                  (println "clj"))))
       :cljs (println "cljs"))))

(tt 2)

The debugger within :clj form breaks almost immediately.

vspinu avatar Aug 20 '17 10:08 vspinu

@FiV0 You might consider taking a stab at this one as well.

bbatsov avatar Feb 07 '20 07:02 bbatsov

@bbatsov Had seen it. You can assign it to me if you want.

FiV0 avatar Feb 07 '20 11:02 FiV0

@bbatsov @vspinu For this one I need some pointers/help. Consider the following code

#dbg
#?(:cljs (+ 1 2) :clj (inc (inc 1)))

In the maybe-debug function https://github.com/clojure-emacs/cider-nrepl/blob/f71b851a248ac95217b17f409ff7ab7669acd402/src/cider/nrepl/middleware/debug.clj#L612-L617 the code is still in string form, so something like "#dbg\n#?(....)". Once the evaluation gets to instrument-and-eval https://github.com/clojure-emacs/cider-nrepl/blob/f71b851a248ac95217b17f409ff7ab7669acd402/src/cider/nrepl/middleware/debug.clj#L594-L597 the code has been "read" as clojure data and the form is just (inc (inc 1)), so somewhere in between some reading seems to happen. I am assuming this happens in some lower handler in the nrepl. Is that correct? Essentially when reading such a form we need to do something like (read-string {:read-cond :preserve} "#dbg\n#?(....)") as otherwise the bookkeeping of where stuff is located fails. Maybe it's this line? https://github.com/nrepl/nrepl/blob/1cc9baae631703c184894559a2232275dc50dff6/src/clojure/nrepl/middleware/interruptible_eval.clj#L110-L111

FiV0 avatar Feb 08 '20 12:02 FiV0

Yeah, I think that's the line. nREPL basically just delegates to Clojure's own REPL, so it wouldn't have to reimplement its functionality.

bbatsov avatar Feb 09 '20 15:02 bbatsov

@bbatsov You think it would be ok to add an option to nrepl so that code is read with :preserve. We could then do a first read via nrepl, locate stuff corretly in cider-nrepl, do a second read also in cider-nrepl and then do stuff as usual. I don't see how to solve this one otherwise.

FiV0 avatar Feb 09 '20 16:02 FiV0

Probably that'd be fine. What exactly does :preserve do? If it doesn't affect the evaluation then we can safely enable it.

bbatsov avatar Feb 09 '20 16:02 bbatsov