Fix #1322: replace load-env-vars w/ custom loader

I've replaced load-env-var with our own custom parser. load-env-var
expects a well-formatted env file, which neither env nor set produces,
which is what doom env uses to dump the shell environment.

This should fix issues that arise when envvars (like PATH) contain
arbitrary whitespace.
This commit is contained in:
Henrik Lissner 2019-05-17 20:19:35 -04:00
parent 31ed6fdf2b
commit 3ed54e191b
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
3 changed files with 25 additions and 13 deletions

View file

@ -67,7 +67,7 @@ It is rare that you'll need to change this.")
'("-c") '("-c")
;; Execute twice, once in a non-interactive login shell and once in an ;; Execute twice, once in a non-interactive login shell and once in an
;; interactive shell in order to capture all the init files possible. ;; interactive shell in order to capture all the init files possible.
'("-lc" "-ic")) '("-ic"))
"The `shell-command-switch'es to use on `doom-env-executable'. "The `shell-command-switch'es to use on `doom-env-executable'.
This is a list of strings. Each entry is run separately and in sequence with This is a list of strings. Each entry is run separately and in sequence with
`doom-env-executable' to scrape envvars from your shell environment.") `doom-env-executable' to scrape envvars from your shell environment.")
@ -110,16 +110,12 @@ order of `doom-env-switches' determines priority."
"# or set DOOMENV=1 in your shell environment/config.\n" "# or set DOOMENV=1 in your shell environment/config.\n"
"# ---------------------------------------------------------------------------\n\n")) "# ---------------------------------------------------------------------------\n\n"))
(let ((env-point (point))) (let ((env-point (point)))
;; temporarily unset ignored environment variables
(dolist (var doom-env-ignored-vars)
(setenv var nil))
(dolist (shell-command-switch doom-env-switches) (dolist (shell-command-switch doom-env-switches)
(message "Scraping env from '%s %s %s'" (message "Scraping env from '%s %s %s'"
shell-file-name shell-file-name
shell-command-switch shell-command-switch
doom-env-executable) doom-env-executable)
(insert (shell-command-to-string doom-env-executable))) (insert (shell-command-to-string doom-env-executable))))))))
;; sort the environment variables
(sort-lines nil env-point (point-max))
;; remove adjacent duplicated lines
(delete-duplicate-lines env-point (point-max) nil t)
;; remove ignored environment variables
(dolist (var doom-env-ignored-vars)
(flush-lines (concat "^" var "=") env-point (point-max))))))))

View file

@ -38,7 +38,7 @@ package's name as a symbol, and whose CDR is the plist supplied to its
`package!' declaration. Set by `doom-initialize-packages'.") `package!' declaration. Set by `doom-initialize-packages'.")
(defvar doom-core-packages (defvar doom-core-packages
'(persistent-soft use-package quelpa async load-env-vars) '(persistent-soft use-package quelpa async)
"A list of packages that must be installed (and will be auto-installed if "A list of packages that must be installed (and will be auto-installed if
missing) and shouldn't be deleted.") missing) and shouldn't be deleted.")

View file

@ -427,6 +427,23 @@ in interactive sessions, nil otherwise (but logs a warning)."
(message "Autoload file warning: %s -> %s" (car e) (error-message-string e)) (message "Autoload file warning: %s -> %s" (car e) (error-message-string e))
(signal 'doom-autoload-error (list (file-name-nondirectory file) e)))))) (signal 'doom-autoload-error (list (file-name-nondirectory file) e))))))
(defun doom-load-env-vars (file)
"Read and set envvars in FILE."
(let (vars)
(with-temp-buffer
(insert-file-contents file)
(re-search-forward "\n\n" nil t)
(while (re-search-forward "\n\\([^= \n]+\\)=" nil t)
(save-excursion
(let ((var (match-string 1))
(value (buffer-substring-no-properties
(point)
(1- (or (when (re-search-forward "^\\([^= ]+\\)=" nil t)
(line-beginning-position))
(point-max))))))
(setenv var value)))))
vars))
(defun doom-initialize (&optional force-p) (defun doom-initialize (&optional force-p)
"Bootstrap Doom, if it hasn't already (or if FORCE-P is non-nil). "Bootstrap Doom, if it hasn't already (or if FORCE-P is non-nil).
@ -491,9 +508,8 @@ to least)."
;; Load shell environment ;; Load shell environment
(when (and (not noninteractive) (when (and (not noninteractive)
(file-readable-p doom-env-file) (file-readable-p doom-env-file))
(require 'load-env-vars nil t)) (doom-load-env-vars doom-env-file)
(load-env-vars doom-env-file)
(setq exec-path (append (split-string (getenv "PATH") ":") (setq exec-path (append (split-string (getenv "PATH") ":")
(list exec-directory)) (list exec-directory))
shell-file-name (or (getenv "SHELL") shell-file-name (or (getenv "SHELL")