emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* [BUG][PATCH] org-babel: Shared list structure causes spurious changes in user options [9.8-pre (release_9.7.10-127-g07dd3b @ /usr/local/share/emacs/site-lisp/org/)]
@ 2024-09-28 20:33 Nick Dokos
  2024-10-10 17:43 ` Ihor Radchenko
  0 siblings, 1 reply; 2+ messages in thread
From: Nick Dokos @ 2024-09-28 20:33 UTC (permalink / raw)
  To: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 2434 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.
------------------------------------------------------------------------
This is a simplified reproducer of the problem, as discovered by user
@lyndhurst on SE Emacs. See
https://emacs.stackexchange.com/questions/82261/set-org-babel-source-block-parameters-dynamically
for more details.

Starting with `emacs -Q', open the following file:

--8<---------------cut here---------------start------------->8---
* Bug report example

#+begin_src shell
  echo "foo"
#+end_src

* Code

#+begin_src elisp :results raw
  (setq org-babel-default-header-args:shell '((:results . "file") (:file . "results.txt") (:output-dir . "a")))
#+end_src
--8<---------------cut here---------------end--------------->8---

Evaluate the last code block to set
`org-babel-default-header-args:shell' to the indicated value, then
evaluate the shell code block repeatedly: the first time it works to
produce an output file `a/results.txt'; subsequent evaluations produce
errors:

org-ctrl-c-ctrl-c: Opening output file: No such file or directory, .../a/a/results.txt
org-ctrl-c-ctrl-c: Opening output file: No such file or directory, .../a/a/a/results.txt
org-ctrl-c-ctrl-c: Opening output file: No such file or directory, .../a/a/a/a/results.txt

Note the increasing number of copies of the `output-dir' setting.  
The value of `org-babel-default-header-args:shell' is modified every
time:

    ((:results . "file") (:file . "a/a/a/a/results.txt") (:output-dir . "a"))

The problem is the way that `org-babel-get-src-block-info' constructs
the `info' variable, which is used to pass around information during the
evaluation: the value depends on the `org-babel-default-header-args*'
variables, but is modified in place, and because of shared list
structure, the modification gets propagated to the user variables.

Emacs  : GNU Emacs 31.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.43, cairo version 1.18.0) of 2024-08-09
Package: Org mode version 9.8-pre (release_9.7.10-127-g07dd3b @/usr/local/share/emacs/site-lisp/org/)

------------------------------------------------------------------
The following patch wraps calls to `copy-tree' around the relevant
places to eliminate the sharing.




[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Patch for "Shared list structure caused spurious changes in user options" --]
[-- Type: text/x-patch, Size: 2117 bytes --]

From dfe13482a8b3997aaca35063e81c7535faf02c0b Mon Sep 17 00:00:00 2001
From: Nick Dokos <ndokos@gmail.com>
Date: Sat, 28 Sep 2024 15:39:25 -0400
Subject: [PATCH] org-babel: Avoid changing user options by not sharing list
 structure

* lisp/ob-core.el (org-babel-get-src-block-info): Use `copy-tree'
when using `org-babel-default-header-args*' variables to prepare the
`info' variable used in src block evaluation. The `info' variable gets
modified, and the modifications were affecting the values of the user
variables. In particular, the `:file' setting was modified in the presence
of a `:output-dir' setting, concatenating more and more copies of the directory
every time the block was evaluated.

Reported-by: @lyndhurst on SE Emacs
Link: https://emacs.stackexchange.com/questions/82261/
---
 lisp/ob-core.el | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 90c52f8b7836..e6db422c9d20 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -731,9 +731,17 @@ Otherwise, return a list with the following pattern:
 	       lang
 	       (org-babel--normalize-body datum)
 	       (apply #'org-babel-merge-params
-		      (if inline org-babel-default-inline-header-args
-			org-babel-default-header-args)
-		      (and (boundp lang-headers) (eval lang-headers t))
+                      ;; Use `copy-tree' to avoid creating shared structure
+                      ;; with the `org-babel-default-header-args-*' variables:
+                      ;; modifications by `org-babel-generate-file-param'
+                      ;; below would modify the shared structure, thereby
+                      ;; modifying the variables.
+  		    (copy-tree
+                       (if inline org-babel-default-inline-header-args
+                         org-babel-default-header-args)
+                       t)
+  		    (and (boundp lang-headers)
+                           (copy-tree (eval lang-headers t) t))
 		      (append
 		       ;; If DATUM is provided, make sure we get node
 		       ;; properties applicable to its location within
-- 
2.46.0


[-- Attachment #3: Type: text/plain, Size: 11 bytes --]



-- 
Nick

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

* Re: [BUG][PATCH] org-babel: Shared list structure causes spurious changes in user options [9.8-pre (release_9.7.10-127-g07dd3b @ /usr/local/share/emacs/site-lisp/org/)]
  2024-09-28 20:33 [BUG][PATCH] org-babel: Shared list structure causes spurious changes in user options [9.8-pre (release_9.7.10-127-g07dd3b @ /usr/local/share/emacs/site-lisp/org/)] Nick Dokos
@ 2024-10-10 17:43 ` Ihor Radchenko
  0 siblings, 0 replies; 2+ messages in thread
From: Ihor Radchenko @ 2024-10-10 17:43 UTC (permalink / raw)
  To: Nick Dokos; +Cc: emacs-orgmode

Nick Dokos <ndokos@gmail.com> writes:

> Subject: [PATCH] org-babel: Avoid changing user options by not sharing list
>  structure
>
> * lisp/ob-core.el (org-babel-get-src-block-info): Use `copy-tree'
> when using `org-babel-default-header-args*' variables to prepare the
> `info' variable used in src block evaluation. The `info' variable gets
> modified, and the modifications were affecting the values of the user
> variables. In particular, the `:file' setting was modified in the presence
> of a `:output-dir' setting, concatenating more and more copies of the directory
> every time the block was evaluated.

Thanks!
Applied, onto bugfix.
https://git.savannah.gnu.org/cgit/emacs/org-mode.git/commit/?id=b457dbfe5a
Fixed.

-- 
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] 2+ messages in thread

end of thread, other threads:[~2024-10-10 17:42 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-28 20:33 [BUG][PATCH] org-babel: Shared list structure causes spurious changes in user options [9.8-pre (release_9.7.10-127-g07dd3b @ /usr/local/share/emacs/site-lisp/org/)] Nick Dokos
2024-10-10 17:43 ` 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).