all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* [BUG] ob-clojure doesn't handle namespaces properly
@ 2024-12-03 11:07 Rens Oliemans
  2024-12-22 11:52 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Rens Oliemans @ 2024-12-03 11:07 UTC (permalink / raw)
  To: emacs-orgmode@gnu.org; +Cc: Daniel Kraus

[-- Attachment #1: Type: text/plain, Size: 582 bytes --]

Hi,

There are two bugs introduced in commit 6efb073463481ee572eb3bb1155cc3b0d6987df6;
I Cced Daniel, the commit author. It persists on main.

Since the bugs are intimately related to ob-clojure and the org mode source
blocks, I've just attached an org file that highlights the two bugs, I think
that's easiest.

I'm fairly sure that they have the same root cause: wrapping each block inside
`(binding [*out (java.io.StringWriter.)])'. I'm not too familiar with
(ob-)clojure, so I'm not sure what the best way to solve this would be, but I'm
happy to try if necessary.

Best,
Rens


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: bugs.org --]
[-- Type: text/org, Size: 2964 bytes --]

#+title: Clojure bugs
I bisected this to [[https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=6efb073463481ee572eb3bb1155cc3b0d6987df6][6efb073463481ee572eb3bb1155cc3b0d6987df6]]. I've only tested this on =cider=, I'm not sure if that makes a difference.

* Bug 1: Different namespaces in the same document don't work well
Say I have the following structure:

** Util
:PROPERTIES:
:header-args:clojure: :exports both :session *clojure-util*
:END:

It seems ~(ns example.util)~ isn't run properly: =sum= gets added to the default namespace the first time it's run. The second time it's run, it does work. In general, a block with a ~(ns)~ call needs to be run twice, it seems. To show this behaviour, I've added =:results value append= to the block below.

#+begin_src clojure :results value append
  (ns example.util)
  (defn sum [vec]
    (reduce + vec))
#+end_src

#+RESULTS:
: Please reevaluate when nREPL is connected
: #'user/sum
: #'example.util/sum

This has the result that if I execute the above block only once, other functions cannot use =util/sum=; see the code blocks below. If the REPL is restarted, it needs to be run twice again.

To show that you need to run the above block twice, see the output of the block below. I executed it once after the /first/ time the previous was run (and =sum= was defined in the =user= namespace), and once after the /second/ time.
#+begin_src clojure :results value append
  (sum (range 10))
#+end_src

#+RESULTS:
: class clojure.lang.Compiler$CompilerException
: 45

** Program
:PROPERTIES:
:header-args:clojure: :exports both :session *clojure-01*
:END:

This code block always errors:
#+begin_src clojure
  (ns example.program
    (:require [example.util :as util]))

  (util/sum (range 10))
#+end_src

#+RESULTS:
: class clojure.lang.Compiler$CompilerException

* Bug 2: When tangling, code blocks can't see previously defined vars
:PROPERTIES:
:header-args:clojure: :tangle tangled.clj :session *clojure-tangle* :results none
:END:

This does not occur with =:results output=, since in that case =ob-clojure= keeps the body unchanged. If we use the default, however, each code block is individually wrapped in a =prn= call, and all variables defined are limited to the scope of the org source block..

If we first define some function =sum=,
#+begin_src clojure
  (ns example.tangle)

  (defn sum [xs] (reduce + xs))
#+end_src

We cannot use it in later code blocks. Well, when executing this from Org mode or when exporting, things go well. However, when this file gets tangled (here to =tangled.clj=), the following line fails (/Unable to resolve symbol: sum in this context/).
#+begin_src clojure
  (sum (range 10))
#+end_src

#+RESULTS:
: 45

#+begin_src sh :results output
  clj -M tangled.clj 2>&1
#+end_src

#+RESULTS:
: #'user/sum
: Syntax error compiling at (tangled.clj:5:46).
: Unable to resolve symbol: sum in this context
: 
: Full report at:
: /tmp/clojure-14548717163049100556.edn


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [BUG] ob-clojure doesn't handle namespaces properly
  2024-12-03 11:07 [BUG] ob-clojure doesn't handle namespaces properly Rens Oliemans
@ 2024-12-22 11:52 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2024-12-22 11:52 UTC (permalink / raw)
  To: Rens Oliemans; +Cc: emacs-orgmode@gnu.org, Daniel Kraus

Rens Oliemans <hallo@rensoliemans.nl> writes:

> There are two bugs introduced in commit 6efb073463481ee572eb3bb1155cc3b0d6987df6;
> I Cced Daniel, the commit author. It persists on main.
>
> Since the bugs are intimately related to ob-clojure and the org mode source
> blocks, I've just attached an org file that highlights the two bugs, I think
> that's easiest.

I cannot reproduce the problems you are showing. Although I am able to
reproduce *different* problems.

> I'm fairly sure that they have the same root cause: wrapping each block inside
> `(binding [*out (java.io.StringWriter.)])'. I'm not too familiar with
> (ob-)clojure, so I'm not sure what the best way to solve this would be, but I'm
> happy to try if necessary.

May you try reproducing from clean emacs -Q?

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-12-22 11:52 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-12-03 11:07 [BUG] ob-clojure doesn't handle namespaces properly Rens Oliemans
2024-12-22 11:52 ` Ihor Radchenko

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.