emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Async Python src block behavior with :dir header property


From: Ihor Radchenko
Subject: Re: Async Python src block behavior with :dir header property
Date: Sat, 03 Feb 2024 15:51:32 +0000

Jack Kamm <jackkamm@gmail.com> writes:

> Ihor Radchenko <yantar92@posteo.net> writes:
>
>> The patch generally looks reasonable, although I am slightly concerned
>> about interaction between :dir and session we describe in the manual:
>>
>>     When ‘dir’ is used with ‘session’, Org sets the starting directory
>>     for a new session.  But Org does not alter the directory of an already
>>     existing session.
>
> I agree it's a problem -- if there are multiple blocks with the same
> session but different ":dir" arguments, then a ":file" result of the
> second block will be relative to the wrong :dir.
>
> This seems like a longstanding problem, and affects both async and
> non-async session blocks.

Maybe something like the attached?


> Ideally, ":dir" should be set at session level rather than block
> level. This could be done via PROPERTY if there is only one session in
> the file or subtree, but there isn't a mechanism for this more
> generally.

Or we can ask babel backends to respect :dir and issue the necessary
"cd" command in the session for the duration of the code block evaluation.

> Perhaps the simplest solution is to add a check to org-lint, requiring
> all blocks with the same session to have the same :dir.

Or this.

>From 1e87de13d44a1bb31985622bfc9c9a6df6b7548c Mon Sep 17 00:00:00 2001
Message-ID: 
<1e87de13d44a1bb31985622bfc9c9a6df6b7548c.1706975370.git.yantar92@posteo.net>
From: Ihor Radchenko <yantar92@posteo.net>
Date: Sat, 3 Feb 2024 16:47:57 +0100
Subject: [PATCH] org-babel-execute-src-block: Force :dir according to live
 session

* lisp/ob-core.el (org-babel-execute-src-block): Force eval directory
to follow live session buffer, if any.  This is consistent with what
we promise in the manual.

Link: 87mssi8ht2.fsf@gmail.com">https://orgmode.org/list/87mssi8ht2.fsf@gmail.com
---
 lisp/ob-core.el | 19 +++++++++++--------
 1 file changed, 11 insertions(+), 8 deletions(-)

diff --git a/lisp/ob-core.el b/lisp/ob-core.el
index 1de3af6ad..2cd542012 100644
--- a/lisp/ob-core.el
+++ b/lisp/ob-core.el
@@ -840,14 +840,17 @@ (defun org-babel-execute-src-block (&optional arg info 
params executor-type)
                 (dir (cdr (assq :dir params)))
                 (mkdirp (cdr (assq :mkdirp params)))
                 (default-directory
-                  (cond
-                   ((not dir) default-directory)
-                   ((member mkdirp '("no" "nil" nil))
-                    (file-name-as-directory (expand-file-name dir)))
-                   (t
-                    (let ((d (file-name-as-directory (expand-file-name dir))))
-                      (make-directory d 'parents)
-                      d))))
+                 (cond
+                  ((not dir) default-directory)
+                   ((when-let ((session (assq :session params)))
+                      (when (org-babel-comint-buffer-livep session)
+                        (buffer-local-value 'default-directory (get-buffer 
session)))))
+                  ((member mkdirp '("no" "nil" nil))
+                   (file-name-as-directory (expand-file-name dir)))
+                  (t
+                   (let ((d (file-name-as-directory (expand-file-name dir))))
+                     (make-directory d 'parents)
+                     d))))
                 (cmd (intern (concat "org-babel-execute:" lang)))
                 result exec-start-time)
            (unless (fboundp cmd)
-- 
2.43.0


-- 
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>

reply via email to

[Prev in Thread] Current Thread [Next in Thread]