From 6f49f2151e4362ac5c86208c754cfe7f931972b1 Mon Sep 17 00:00:00 2001 From: Henrik Lissner Date: Sun, 23 Aug 2020 21:37:02 -0400 Subject: [PATCH] Fix #3812: direnv not in scope for babel execution --- modules/tools/direnv/config.el | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/modules/tools/direnv/config.el b/modules/tools/direnv/config.el index e7fba4f3d..f38b7553f 100644 --- a/modules/tools/direnv/config.el +++ b/modules/tools/direnv/config.el @@ -40,4 +40,26 @@ "Don't try to use direnv if the executable isn't present." :before-while #'envrc-mode (or (executable-find "direnv") - (ignore (doom-log "Couldn't find direnv executable"))))) + (ignore (doom-log "Couldn't find direnv executable")))) + + ;; HACK envrc-mode only affects the current buffer's environment, which is + ;; generally what we want, except when we're running babel blocks in + ;; org-mode, because there may be state or envvars those blocks need to + ;; read. In order to perpetuate the org buffer's environment into the + ;; execution of the babel block we need to temporarily change the global + ;; environment. Let's hope it runs quickly enough that its effects aren't + ;; felt in other buffers in the meantime! + (defvar +direnv--old-environment nil) + (defadvice! +direnv-persist-environment-a (orig-fn &rest args) + :around #'org-babel-execute-src-block + (if +direnv--old-environment + (apply orig-fn args) + (setq-default +direnv--old-environment + (cons (default-value 'process-environment) + (default-value 'exec-path)) + exec-path exec-path + process-environment process-environment) + (unwind-protect (apply orig-fn args) + (setq-default process-environment (car +direnv--old-environment) + exec-path (cdr +direnv--old-environment) + +direnv--old-environment nil)))))