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

`nrepl.middleware.print/print` printer garbles lazy output into the result

Open PEZ opened this issue 1 year ago • 3 comments

Hi!

With nrepl.middleware.print/print, evaluating:

(for [[type amount] {:wood 2 :iron 2}]
  (println "\n--------\n" (str type " " amount)))

Gives a garbled result value, and no output.

With cider.nrepl.pprint/pr, it works fine.

Expected behavior

The output as out messages, and the result as value message.

Actual behavior

The output and result come garbled in a value message.

Steps to reproduce the problem

Calva's nrepl messages log:

1727018467380 -> sent 
{
  id: '124',
  op: 'eval',
  ns: 'mini.playground',
  session: 'bd20ade5-ce08-42c1-91d4-cfd96548d22b',
  code: '(for [[type amount] {:wood 2 :iron 2}]\n' +
    '  (println "\\n--------\\n" (str type " " amount)))',
  'nrepl.middleware.print/options': { 'right-margin': 120, length: 200 },
  'nrepl.middleware.print/print': 'cider.nrepl.pprint/pprint',
  file: '/Users/pez/Projects/teasers/src/mini/playground.clj',
  line: 16,
  column: 1,
  pprint: true
}

1727018467399 <- received 19ms
{
  id: '124',
  ns: 'mini.playground',
  session: 'bd20ade5-ce08-42c1-91d4-cfd96548d22b',
  value: '\n--------\n :wood 2\n(nil\n--------\n :iron 2\n nil)'
}

1727018467399 <- received 0ms
{
  id: '124',
  session: 'bd20ade5-ce08-42c1-91d4-cfd96548d22b',
  status: [ 'done' ]
}

Environment & Version information

cider-nrepl version

I'm using whatever Calva is using, but @alexander-yakushev has reproduced it with the latest snapshot.

Java version

openjdk version "21.0.1" 2023-10-17 LTS OpenJDK Runtime Environment Zulu21.30+15-CA (build 21.0.1+12-LTS) OpenJDK 64-Bit Server VM Zulu21.30+15-CA (build 21.0.1+12-LTS, mixed mode, sharing)

Operating system

MacOS Sonoma 14.5

PEZ avatar Oct 09 '24 07:10 PEZ

@alexander-yakushev Can you take a look at this?

bbatsov avatar Oct 09 '24 07:10 bbatsov

I did and couldn't find the culprit immediately. Will look again later.

alexander-yakushev avatar Oct 09 '24 08:10 alexander-yakushev

This indeed is not a problem with nREPL or cider-nrepl, but with clojure.pprint implementation. Reproducible with plain clojure:

$ clj
Clojure 1.12.0
user=> (require 'clojure.pprint)
nil
user=> (clojure.pprint/pprint (for [[k v] {:a 1 :b 2}] (println k v)))
:a 1
(nil:b 2
 nil)
nil

Laziness and pretty-printing just don't fly well together.

However, it works alright with our recent cider.nrepl.pprint/orchard-pprint (which wraps the embedded pp printer). Let's revisit adding it as one of the pretty-printing options, what do you think @bbatsov?

alexander-yakushev avatar Aug 31 '25 15:08 alexander-yakushev