emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
@ 2024-03-09  7:42 Damien Cassou
  2024-03-12 12:18 ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Cassou @ 2024-03-09  7:42 UTC (permalink / raw)
  To: emacs-orgmode

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



Remember to cover the basics, that is, what you expected to happen and
what in fact did happen.  You don't know how to make a good report?  See

     https://orgmode.org/manual/Feedback.html#Feedback

Your bug report will be posted to the Org mailing list.
------------------------------------------------------------------------

The command `org-babel-execute-buffer' tries to execute all source code
blocks of the current buffer. When it encounters a block that is not
executable the command crashes. The attached patch makes the command ignore
non-executable blocks and move on.

Emacs  : GNU Emacs 29.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.41, cairo version 1.18.0)
Package: Org mode version 9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)
-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-babel-execute-buffer-Prevent-executing-non-code-.patch --]
[-- Type: text/x-patch, Size: 1097 bytes --]

From 0879a7f4b855465a9cb1532293726a9a3d7d7c75 Mon Sep 17 00:00:00 2001
From: Damien Cassou <damien@cassou.me>
Date: Sat, 9 Mar 2024 08:35:44 +0100
Subject: [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks

* lisp/ob-core.el (org-babel-execute-buffer): Guard against evaluating
non-code blocks.
---
 lisp/ob-core.el | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index bfeac257b..ca7afcdc5 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -1417,7 +1417,11 @@ (defun org-babel-execute-buffer (&optional arg)
       (if (org-element-type-p
            (org-element-context) '(babel-call inline-babel-call))
           (org-babel-lob-execute-maybe)
-        (org-babel-execute-src-block arg)))))
+        (let* ((info (org-babel-get-src-block-info))
+               (lang (nth 0 info))
+               (cmd (intern (concat "org-babel-execute:" lang))))
+          (when (fboundp cmd)
+            (org-babel-execute-src-block arg)))))))
 
 ;;;###autoload
 (defun org-babel-execute-subtree (&optional arg)
-- 
2.43.1


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

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-09  7:42 [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)] Damien Cassou
@ 2024-03-12 12:18 ` Ihor Radchenko
  2024-03-12 21:07   ` Damien Cassou
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2024-03-12 12:18 UTC (permalink / raw)
  To: Damien Cassou; +Cc: emacs-orgmode

Damien Cassou <damien@cassou.me> writes:

> The command `org-babel-execute-buffer' tries to execute all source code
> blocks of the current buffer. When it encounters a block that is not
> executable the command crashes. The attached patch makes the command ignore
> non-executable blocks and move on.
> ...
> -        (org-babel-execute-src-block arg)))))
> +        (let* ((info (org-babel-get-src-block-info))
> +               (lang (nth 0 info))
> +               (cmd (intern (concat "org-babel-execute:" lang))))
> +          (when (fboundp cmd)
> +            (org-babel-execute-src-block arg)))))))

Thanks for the patch, but it is not obvious that skipping src blocks
that cannot be executed is always a good idea.
Consider, for example, that some blocks are used as input for other
blocks. Then, failing to execute them means that other blocks may have
unpredictable side effects.

May you please provide a concrete use-case when skipping some src code
blocks is desired?

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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] 9+ messages in thread

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-12 12:18 ` Ihor Radchenko
@ 2024-03-12 21:07   ` Damien Cassou
  2024-03-12 21:23     ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Cassou @ 2024-03-12 21:07 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Hi Ihor,

Ihor Radchenko <yantar92@posteo.net> writes:
> Thanks for the patch, but it is not obvious that skipping src blocks
> that cannot be executed is always a good idea.  Consider, for example,
> that some blocks are used as input for other blocks. Then, failing to
> execute them means that other blocks may have unpredictable side
> effects.


I don't understand your position. Without this patch, executing
`org-babel-execute-buffer' will just crash if a block can't be
executed. What problem can skipping those could cause?


> May you please provide a concrete use-case when skipping some src code
> blocks is desired?

I use text-mode blocks to represent output of bash scripts as in:

    #+name: repo-test-check
    #+begin_src sh :exports both :results output raw :wrap SRC text
      git log --oneline
    #+end_src
    
    #+RESULTS: repo-test-check
    #+begin_SRC text
    cfd2b (HEAD -> main) Empty
    #+end_SRC

The second block can't be executed because no
`org-babel-execute:text` function exists.

Best

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill


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

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-12 21:07   ` Damien Cassou
@ 2024-03-12 21:23     ` Ihor Radchenko
  2024-03-16 10:39       ` Damien Cassou
  0 siblings, 1 reply; 9+ messages in thread
From: Ihor Radchenko @ 2024-03-12 21:23 UTC (permalink / raw)
  To: Damien Cassou; +Cc: emacs-orgmode

Damien Cassou <damien@cassou.me> writes:

> Ihor Radchenko <yantar92@posteo.net> writes:
>> Thanks for the patch, but it is not obvious that skipping src blocks
>> that cannot be executed is always a good idea.  Consider, for example,
>> that some blocks are used as input for other blocks. Then, failing to
>> execute them means that other blocks may have unpredictable side
>> effects.
>
>
> I don't understand your position. Without this patch, executing
> `org-babel-execute-buffer' will just crash if a block can't be
> executed. What problem can skipping those could cause?

For example, consider

#+begin_src bash
mkdir foo
#+end_src

#+begin_src emacs-lisp
(write-file "foo/result")
#+end_src

The second block relies upon side effects of the first block.
However, if ob-shell is not loaded, with your patch, the side effect
will not happen.

One can construct more nasty examples when not executing prior block
leads to unexpected or even damaging results.

>> May you please provide a concrete use-case when skipping some src code
>> blocks is desired?
>
> I use text-mode blocks to represent output of bash scripts as in:
>
>     #+name: repo-test-check
>     #+begin_src sh :exports both :results output raw :wrap SRC text
>       git log --oneline
>     #+end_src
>     
>     #+RESULTS: repo-test-check
>     #+begin_SRC text
>     cfd2b (HEAD -> main) Empty
>     #+end_SRC
>
> The second block can't be executed because no
> `org-babel-execute:text` function exists.

Why not simply adding :eval no header argument?

The problem with testing that `org-babel-execute:<lang>' function exists
is that we cannot distinguish between languages that cannot and should
not be executed and babel backends that are not loaded for some reason.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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] 9+ messages in thread

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-12 21:23     ` Ihor Radchenko
@ 2024-03-16 10:39       ` Damien Cassou
  2024-03-16 11:48         ` Ihor Radchenko
  0 siblings, 1 reply; 9+ messages in thread
From: Damien Cassou @ 2024-03-16 10:39 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Hi Ihor,

Ihor Radchenko <yantar92@posteo.net> writes:
> For example, consider
>
> #+begin_src bash
> mkdir foo
> #+end_src
>
> #+begin_src emacs-lisp
> (write-file "foo/result")
> #+end_src
>
> The second block relies upon side effects of the first block.
> However, if ob-shell is not loaded, with your patch, the side effect
> will not happen.  One can construct more nasty examples when not
> executing prior block leads to unexpected or even damaging results.


If I understand correctly, you are saying that it is better to let the
command crash as soon as possible instead of letting it execute
potentially problematic code because some previous block has not been
executed. I think it makes perfect sense. I'm now convinced that my
patch is not desirable. Feel free to close the issue.


> Why not simply adding :eval no header argument?


Just because I didn't know it exists. Thank you for telling me. I have
now added it to all my code blocks generating non-executable results
using the :wrap keyword like this:

    #+name: show-tree
    #+begin_src sh :exports both :results output drawer :tangle yes :wrap "SRC text :eval no"
    echo foo
    #+end_src
    
    #+RESULTS: show-tree
    #+begin_SRC text :eval no
    foo
    #+end_SRC

It works very well, thanks! Would you say this is how you would have
done it as well or is there a better way?

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill


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

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-16 10:39       ` Damien Cassou
@ 2024-03-16 11:48         ` Ihor Radchenko
  2024-03-16 18:30           ` Damien Cassou
  2024-03-17 12:28           ` Gerard Vermeulen
  0 siblings, 2 replies; 9+ messages in thread
From: Ihor Radchenko @ 2024-03-16 11:48 UTC (permalink / raw)
  To: Damien Cassou; +Cc: emacs-orgmode

Damien Cassou <damien@cassou.me> writes:

> If I understand correctly, you are saying that it is better to let the
> command crash as soon as possible instead of letting it execute
> potentially problematic code because some previous block has not been
> executed. I think it makes perfect sense. I'm now convinced that my
> patch is not desirable. Feel free to close the issue.

Yup, you understand correctly.
Although, we previously discussed a similar issue with bibtex
blocks in https://list.orgmode.org/87ilvnx29d.fsf@kyleam.com/, but the
proposed approach to solve the issue was different.

> Just because I didn't know it exists. Thank you for telling me. I have
> now added it to all my code blocks generating non-executable results
> using the :wrap keyword like this:
>
>     #+name: show-tree
>     #+begin_src sh :exports both :results output drawer :tangle yes :wrap "SRC text :eval no"
>     echo foo
>     #+end_src
>     
>     #+RESULTS: show-tree
>     #+begin_SRC text :eval no
>     foo
>     #+end_SRC
>
> It works very well, thanks! Would you say this is how you would have
> done it as well or is there a better way?

Yes. You can add

#+property: header-args:text :eval no

on top of your Org file or add

(setq org-babel-default-header-args:text '((:eval . "no")))

to your config.

See https://orgmode.org/manual/Using-Header-Arguments.html

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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] 9+ messages in thread

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-16 11:48         ` Ihor Radchenko
@ 2024-03-16 18:30           ` Damien Cassou
  2024-03-17 12:28           ` Gerard Vermeulen
  1 sibling, 0 replies; 9+ messages in thread
From: Damien Cassou @ 2024-03-16 18:30 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

Ihor Radchenko <yantar92@posteo.net> writes:
> Yes. You can add
> #+property: header-args:text :eval no
> on top of your Org file or add
> (setq org-babel-default-header-args:text '((:eval . "no")))
> to your config.

org is amazing!

Thank you very much for all your work.

-- 
Damien Cassou

"Success is the ability to go from one failure to another without
losing enthusiasm." --Winston Churchill


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

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-16 11:48         ` Ihor Radchenko
  2024-03-16 18:30           ` Damien Cassou
@ 2024-03-17 12:28           ` Gerard Vermeulen
  2024-03-17 14:30             ` Ihor Radchenko
  1 sibling, 1 reply; 9+ messages in thread
From: Gerard Vermeulen @ 2024-03-17 12:28 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: Damien Cassou, emacs-orgmode



On 16.03.2024 12:48, Ihor Radchenko wrote:

> Yes. You can add
> 
> #+property: header-args:text :eval no
> 
> on top of your Org file or add
> 
> (setq org-babel-default-header-args:text '((:eval . "no")))
> 
> to your config.

Is it possible to make org-lint recognize those settings?

I have the kludge

(defun org-babel-execute:text (_body _params)
   "NO-OP to silence warnings." nil)

in my config to silence "Unknown source block language" warnings.

Regards -- Gerard



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

* Re: [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)]
  2024-03-17 12:28           ` Gerard Vermeulen
@ 2024-03-17 14:30             ` Ihor Radchenko
  0 siblings, 0 replies; 9+ messages in thread
From: Ihor Radchenko @ 2024-03-17 14:30 UTC (permalink / raw)
  To: Gerard Vermeulen; +Cc: Damien Cassou, emacs-orgmode

Gerard Vermeulen <gerard.vermeulen@posteo.net> writes:

>> (setq org-babel-default-header-args:text '((:eval . "no")))
>> 
>> to your config.
>
> Is it possible to make org-lint recognize those settings?
>
> I have the kludge
>
> (defun org-babel-execute:text (_body _params)
>    "NO-OP to silence warnings." nil)
>
> in my config to silence "Unknown source block language" warnings.

You can hide all the warnings from a certain checker by pressing "i" in
the org-lint report.

If necessary, we may add some customizations to org-lint that will allow
customizing which checkers to use.

-- 
Ihor Radchenko // yantar92,
Org mode contributor,
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] 9+ messages in thread

end of thread, other threads:[~2024-03-17 14:31 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-03-09  7:42 [BUG] [PATCH] org-babel-execute-buffer: Prevent executing non-code blocks [9.6.20 ( @ /home/cassou/.emacs.d/lib/org/lisp/)] Damien Cassou
2024-03-12 12:18 ` Ihor Radchenko
2024-03-12 21:07   ` Damien Cassou
2024-03-12 21:23     ` Ihor Radchenko
2024-03-16 10:39       ` Damien Cassou
2024-03-16 11:48         ` Ihor Radchenko
2024-03-16 18:30           ` Damien Cassou
2024-03-17 12:28           ` Gerard Vermeulen
2024-03-17 14:30             ` Ihor Radchenko

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).