questionable result of Clojure code execution in org babel

1,032 views
Skip to first unread message

Johannes

unread,
Aug 5, 2019, 12:12:41 PM8/5/19
to Clojure
Hi

executing the the following code section in an org-mode file with C-c C-c

#+BEGIN_SRC clojure :results value
(* 3 5)
#+END_SRC

I get 

 #+RESULTS:
 : nil15

instead of

 #+RESULTS:
 : 15

Trying the same with emacs-lisp instead of clojure the correct result appears.

What is going wrong?

Johannes 

Peter Hull

unread,
Aug 6, 2019, 11:07:34 AM8/6/19
to Clojure
On Monday, 5 August 2019 17:12:41 UTC+1, Johannes wrote:
What is going wrong?
I tried Emacs 'built-in' org mode (9.1.9) and it gave the correct result, I then installed 9.2.5 and that did give the incorrect result that you reported.
So, I assume something changed in org-mode between 9.1.9 and 9.2.5.
Not an answer I know, but may be helpful to pin it down
Pete

;; Emacs 26.2 (MacOS)
;; CIDER 0.22.0snapshot (package: 20190805.1619), nREPL 0.6.0
;; Clojure 1.10.0, Java 11

Johannes

unread,
Aug 6, 2019, 12:45:08 PM8/6/19
to Clojure
My versions:

;; org-mode 9.2.5
;; Aquamacs 3.5  GNU Emacs 25.3.50.1
;; CIDER 0.21.0 (New York), nREPL 0.6.0
;; Clojure 1.9.0, Java 11

Is there a better place to post the problem?

Johannes

Peter Hull

unread,
Aug 7, 2019, 7:42:07 AM8/7/19
to Clojure
I poked around at this a bit.
If you enable nrepl's logging (use nrepl-toggle-message-logging) you will see something like this:
(-->
  id        
"18"
  op        
"eval"
  session    
"86281560-e467-47c4-869d-043b03f5c546"
  time
-stamp "2019-08-07 12:23:33.769213028"
  code      
"(ns org-babel-clojure.core)
(* 1 2 3 4)"

)
(<--
  id        
"18"
  session    
"86281560-e467-47c4-869d-043b03f5c546"
  time
-stamp "2019-08-07 12:23:33.775780419"
  ns        
"org-babel-clojure.core"
  value      
"nil"
)
(<--
  id        
"18"
  session    
"86281560-e467-47c4-869d-043b03f5c546"
  time
-stamp "2019-08-07 12:23:33.776704332"
  ns        
"org-babel-clojure.core"
  value      
"24"
)
(<--
  id        
"18"
  session    
"86281560-e467-47c4-869d-043b03f5c546"
  time
-stamp "2019-08-07 12:23:33.777133786"
  status    
("done")
)
(<--
  id                
"18"
  session            
"86281560-e467-47c4-869d-043b03f5c546"
  time
-stamp         "2019-08-07 12:23:33.780311930"
  changed
-namespaces (dict)
  repl
-type          "clj"
  status            
("state")
)

The connection returns two values,  first 'nil' then '24', and function nrepl--merge concats these into the incorrect response that org-mode pastes into your doc.

So I am not sure where to ask - it doesn't look like org-mode's ob-clojure code is doing the wrong thing, but it does seem like something changed between 9.1.x and 9.2.x.

If you're on clojurians slack there is a cider channel where I think they might be able to clear up what's happening with that double return.

Peter Hull

unread,
Aug 7, 2019, 8:37:06 AM8/7/19
to Clojure
On Wednesday, 7 August 2019 12:42:07 UTC+1, Peter Hull wrote:


The connection returns two values, first 'nil' then '24', and function nrepl--merge concats these into the incorrect response that org-mode pastes into your doc.

Wait, might be on to something here. In org-mode 9.1.9 the sent message looks like:
(-->
 id
"8"
 op
"eval"
 session
"a34917da-541a-4d4a-b790-af8e11020c96"
 time
-stamp "2019-08-07 13:18:00.307045105"
 code
"(* 1 2 3 4)"
 ns
"org-babel-clojure.core"
)


in 9.2.5 it's

(-->
 id
"18"
 op
"eval"
 session
"86281560-e467-47c4-869d-043b03f5c546"
 time
-stamp "2019-08-07 12:23:33.769213028"
 code
"(ns org-babel-clojure.core)
(* 1 2 3 4)"

)



In the latter we're sending two forms (ns ...) and (* ...) so we get two responses. Previously it sent only one and used the ns key in the message to set the message.


So, I think you should bug the ob-clojure people at org-mode about this!

Hope that helps,
Pete



Johannes

unread,
Aug 8, 2019, 12:00:43 PM8/8/19
to Clojure
Thank you, Pete, for your investigation. 

Peter Hull

unread,
Aug 9, 2019, 2:18:01 AM8/9/19
to Clojure
On Thursday, 8 August 2019 17:00:43 UTC+1, Johannes wrote:
Thank you, Pete, for your investigation. 
 
OK, I would be interested to hear if you get this sorted out!

numb...@gmail.com

unread,
Aug 9, 2019, 2:36:32 AM8/9/19
to clo...@googlegroups.com
Actually I commited that patch, I intened to fix a problem in ob-clojure which also is a feature for ob-clojure allow user can specify `:ns` header argument. I noticed this result problem. I tried to dig in. But have no lucky. Maybe someone in Org Mode mailing list can help.


[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/


--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to
clojure+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/ac4eeb01-f29b-4f6c-bf0b-86029202dae9%40googlegroups.com.

Johannes

unread,
Aug 9, 2019, 6:52:32 AM8/9/19
to Clojure


Am Freitag, 9. August 2019 08:36:32 UTC+2 schrieb numb...@gmail.com:
Actually I commited that patch, I intened to fix a problem in ob-clojure which also is a feature for ob-clojure allow user can specify `:ns` header argument. I noticed this result problem. I tried to dig in. But have no lucky. Maybe someone in Org Mode mailing list can help.
I posted the problem there. 

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clo...@googlegroups.com.

Peter Hull

unread,
Aug 12, 2019, 7:44:44 AM8/12/19
to Clojure
On Friday, 9 August 2019 11:52:32 UTC+1, Johannes wrote:
I posted the problem there.

Not a great response so far, I see!
I had another little play and it may be possible to solve by a small change to ob-clojure.el. Basically we just wrap the forms and the ns into a single (do ...) form.
$ diff --unified  ~/.emacs.d/elpa/org-9.2.5/ob-clojure.el ~/ob-clojure.el
--- /
home/peter/.emacs.d/elpa/org-9.2.5/ob-clojure.el   2019-08-12 12:22:52.323068180 +0100
+++ /home/peter/ob-clojure.el   2019-08-12 12:21:00.647317946 +0100
@@ -101,7 +101,7 @@
         
(print-length nil)
         
(body
         
(org-trim
-          (format "(ns %s)\n%s"
+          (format "(do (ns %s)\n%s)"
                   
;; Source block specified namespace :ns.
                   ns
                   
;; Variables binding.
@@ -115,7 +115,7 @@
                             body
))))))
     
(if (or (member "code" result-params)
           
(member "pp" result-params))
-       (format "(clojure.pprint/pprint (do %s))" body)
+       (format "(clojure.pprint/pprint %s)" body)
       body
)))
 
 
(defun org-babel-execute:clojure (body params)



However I have not really used this feature before so may be missing something.

Maybe you could give it a try and see if it helps?

Pete


Alex Miller

unread,
Aug 12, 2019, 1:47:48 PM8/12/19
to Clojure
You can’t wrap ns and forms into a single do like that. If you google around you can find this issue referred to as the Gilardi scenario.

Peter Hull

unread,
Aug 12, 2019, 3:38:42 PM8/12/19
to Clojure
On Monday, 12 August 2019 18:47:48 UTC+1, Alex Miller wrote:
You can’t wrap ns and forms into a single do like that. If you google around you can find this issue referred to as the Gilardi scenario.
OK, thanks, Alex.
Is this not covered in https://technomancy.us/143 by the para that starts "In Clojure 1.1, the compiler introduced special handling..."?

Anyway I assumed it was OK because the current code already wraps an ns and some forms in a do, here: https://github.com/bzg/org-mode/blob/d7e12d1df7091563c5f0fe0bd8b2db634d3e87ba/lisp/ob-clojure.el#L118

stardiviner, please could you explain a bit more about your patch? Before it (if I understand it) we had the namespace separately and passed it to `nrepl-sync-request:eval` as the last param.

Thanks,
Pete


numb...@gmail.com

unread,
Aug 13, 2019, 5:57:41 AM8/13/19
to clo...@googlegroups.com
Sorry about this, I forgot most of I did in this patch. Actually I modified it some times in different version. I can find out the discussion mail thread. Here it is:

If someone can fix this issue will be great!
I need to review those discussion to remember the code details. I will. If someone have more question to ask, welcome to ask me :)

[stardiviner]           <Hack this world!>      GPG key ID: 47C32433
IRC(freeenode): stardiviner                     Twitter:  @numbchild
Key fingerprint = 9BAA 92BC CDDD B9EF 3B36  CB99 B8C4 B8E5 47C3 2433
Blog: http://stardiviner.github.io/

--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@googlegroups.com
Note that posts from new members are moderated - please be patient with your first post.
To unsubscribe from this group, send email to

For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/3fabc0cb-69b9-4525-b561-0812870937e7%40googlegroups.com.

Johannes

unread,
Sep 4, 2019, 7:19:30 AM9/4/19
to Clojure
Until today there is no response to my post on org mode list.

In the meantime I've upgraded Aquamacs to the lastest stable version (3.5) and upgraded all my packages (esp. Org mode version 9.2.6).
If I now try to execute the the following code section in an org-mode file with C-c C-c

#+BEGIN_SRC clojure :results value
(* 3 5)
#+END_SRC

I get the error message:
executing Clojure code block...
nrepl-send-sync-request: Wrong type argument: stringp, nil

There is a running cider-real.

The former behavior wasn't nice but better than this.

Any ideas what to do?
Johannes

Johannes

unread,
Sep 8, 2019, 3:46:24 PM9/8/19
to Clojure


Am Mittwoch, 4. September 2019 13:19:30 UTC+2 schrieb Johannes:
Until today there is no response to my post on org mode list.

In the meantime I've upgraded Aquamacs to the lastest stable version (3.5) and upgraded all my packages (esp. Org mode version 9.2.6).
If I now try to execute the the following code section in an org-mode file with C-c C-c

#+BEGIN_SRC clojure :results value
(* 3 5)
#+END_SRC

I get the error message:
executing Clojure code block...
nrepl-send-sync-request: Wrong type argument: stringp, nil

There is a running cider-real.
I fear I was wrong. The connection to the cider-repl wasn't okay.

Now I get the same (wrong) answer as before:
 
#+BEGIN_SRC clojure :results value
(* 3 5)
#+END_SRC

#+RESULTS:
: nil15

Reply all
Reply to author
Forward
0 new messages