cider icon indicating copy to clipboard operation
cider copied to clipboard

Custom middleware broke last update

Open archaic opened this issue 6 months ago • 4 comments

The lateset update to 0.56.0 broke some custom middleware I have been using (shown below)


(:require
   [cider.nrepl.middleware]
   [nrepl.middleware]
   [nrepl.server]
   [refactor-nrepl.middleware]
   [taoensso.telemere :as t])

(defn wrap-print-limit
  [handler]
  (fn [{:as msg
        :nrepl.middleware.print/keys [options]
        :keys [op session]}]

    (when (and (#{"eval"} op)
               session)

      (if (instance? clojure.lang.IAtom session)
        (swap! session
               (fn [state]
                 (-> state
                    (assoc #'clojure.core/*print-length*
                           (or (get options :length)
                               (get options :max-length)
                               52))

                    (assoc #'clojure.core/*print-level*
                           (or (get options :level)
                               (get options :max-depth)
                               10)))))

        (t/error! {:data session
                   :msg "session is not an atom"})))

    (handler msg)))

(nrepl.middleware/set-descriptor! #'wrap-print-limit
                                  {:description "overwrites *print-length* and *print-level* in :session"
                                   :expects #{"eval"}
                                   :requires #{"session"}})

(defn nrepl-handler
  []
  (apply nrepl.server/default-handler
         (into cider.nrepl.middleware/cider-middleware
               [#'refactor-nrepl.middleware/wrap-refactor
                #'wrap-print-limit])))

Using Zprint, I have been using this setup for a while, it broke in the last update (55.7 to 56.0 I think) (the error condition is being hit, and session is not an atom)

I use this setup so that I can dynamically set print options in emacs and have it immedately work in the repl, also I have some custom eval code that I bind that uses print-length and print-level so I want that updated along with the zprint variables.

Is there something fundamentally I am doing wrong, or did something change in regards to :expects and :requires?

Everything is absolute latest (emacs git-master + doom, java is 24.0.1, OS arch-linux current)

Thanks, Chris Rosengren

archaic avatar May 29 '25 21:05 archaic

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your contribution and understanding!

github-actions[bot] avatar Jun 30 '25 03:06 github-actions[bot]

How are you using Zprint exactly? I'm guessing that's something to do with us removing it from the bundled dependencies in cider-nrepl, but I'll have to take a closer look to be sure. //cc @alexander-yakushev

bbatsov avatar Jul 10 '25 04:07 bbatsov

@archaic The reason it doesn't work now is because now the session middleware establishes all dynamic bindings early. You modifying the session object afterwards has no effect.

Instead, you can directly set! the variables in your middleware:

(defn wrap-print-limit
  [handler]
  (fn [{:as msg
        :nrepl.middleware.print/keys [options]
        :keys [op]}]

    (when (#{"eval"} op)
      (set! *print-length* (or (get options :length)
                               (get options :max-length)
                               52))
      (set! *print-level* (or (get options :level)
                              (get options :max-depth)
                              10)))

    (handler msg)))

alexander-yakushev avatar Sep 02 '25 09:09 alexander-yakushev

This issue has been automatically marked as stale because it has not had any recent activity. It will be closed soon if no further activity occurs. Thank you for your contribution and understanding!

github-actions[bot] avatar Dec 02 '25 02:12 github-actions[bot]