fix(cli): envvar injection into bin/doom post-script

This commit is contained in:
Henrik Lissner 2024-09-07 02:27:56 -04:00
parent 61dc1da645
commit 4c7aec35ba
No known key found for this signature in database
GPG key ID: B60957CA074D39A3

View file

@ -1128,13 +1128,20 @@ Emacs' batch library lacks an implementation of the exec system call."
(persistent-files (persistent-files
(combine-and-quote-strings (delq nil (list script-file context-file)))) (combine-and-quote-strings (delq nil (list script-file context-file))))
(persisted-env (persisted-env
(cl-remove-if-not
#'cdr (append
`(("DOOMPROFILE" . ,(ignore-errors (doom-profile->id doom-profile)))
("EMACSDIR" . ,doom-emacs-dir)
("DOOMDIR" . ,doom-user-dir)
("DEBUG" . ,(if init-file-debug "1"))
("__DOOMPID" . ,(number-to-string (doom-cli-context-pid context)))
("__DOOMSTEP" . ,(number-to-string (doom-cli-context-step context)))
("__DOOMCONTEXT" . ,context-file))
(save-match-data (save-match-data
(cl-loop with initial-env = (get 'process-environment 'initial-value) (cl-loop with initial-env = (get 'process-environment 'initial-value)
for env in (seq-difference process-environment initial-env) for env in (seq-difference process-environment initial-env)
if (string-match "^\\([a-zA-Z0-9_][^=]+\\)=\\(.+\\)$" env) if (string-match "^\\([a-zA-Z0-9_][^=]+\\)=\\(.+\\)$" env)
collect (format "%s=%s" collect (cons (match-string 1 env) (match-string 2 env))))))))
(match-string 1 env)
(shell-quote-argument (match-string 2 env)))))))
(cl-incf (doom-cli-context-step context)) (cl-incf (doom-cli-context-step context))
(with-file-modes #o600 (with-file-modes #o600
(doom-log "restart: writing context to %s" context-file) (doom-log "restart: writing context to %s" context-file)
@ -1156,14 +1163,6 @@ Emacs' batch library lacks an implementation of the exec system call."
(doom-log "restart: writing post-script to %s" script-file) (doom-log "restart: writing post-script to %s" script-file)
(doom-file-write (doom-file-write
script-file script-file
(let ((envvars `(("DOOMPROFILE" . ,(ignore-errors (doom-profile->id doom-profile)))
("EMACSDIR" . ,doom-emacs-dir)
("DOOMDIR" . ,doom-user-dir)
("DEBUG" . ,(if init-file-debug "1"))
("__DOOMPID" . ,(number-to-string (doom-cli-context-pid context)))
("__DOOMSTEP" . ,(number-to-string (doom-cli-context-step context)))
("__DOOMGEOM" . ,(number-to-string (doom-cli-context-step context)))
("__DOOMCONTEXT" . ,context-file))))
(pcase-exhaustive shtype (pcase-exhaustive shtype
("sh" `(,(if (featurep :system 'android) ("sh" `(,(if (featurep :system 'android)
"#!/bin/sh\n" "#!/bin/sh\n"
@ -1171,27 +1170,27 @@ Emacs' batch library lacks an implementation of the exec system call."
"trap _doomcleanup EXIT\n" "trap _doomcleanup EXIT\n"
"_doomcleanup() {\n rm -f " ,persistent-files "\n}\n" "_doomcleanup() {\n rm -f " ,persistent-files "\n}\n"
"_doomrun() {\n " ,command "\n}\n" "_doomrun() {\n " ,command "\n}\n"
,(string-join persisted-env " \\\n") ,(cl-loop for (var . val) in persisted-env
,(cl-loop for (envvar . val) in envvars concat (format "%s=%s \\\n" var (shell-quote-argument val)))
if val
concat (format "%s=%s \\\n" envvar (shell-quote-argument val)))
,(format "PATH=\"%s%s$PATH\" \\\n" ,(format "PATH=\"%s%s$PATH\" \\\n"
(doom-path doom-emacs-dir "bin") (doom-path doom-emacs-dir "bin")
path-separator) path-separator)
"_doomrun \"$@\"\n")) "_doomrun \"$@\"\n"))
("ps1" `("try {\n" ("ps1" `("try {\n"
,(cl-loop for (envvar . val) in envvars ,(cl-loop for (var . val) in persisted-env
if val concat (format " $__%s = $env:%s; $env:%s = %S\n "
concat (format " $__%s = $env:%s; $env:%s = %s\\\n " envvar envvar envvar (shell-quote-argument val))) var var var val))
,command ,command
"\n} finally {\n" "\n} finally {\n"
,(cl-loop for file in persistent-files ,(cl-loop for file in persistent-files
concat (format " Remote-Item -Path %S\n " file)) concat (format " Remove-Item -Path %S\n " file))
,(cl-loop for (envvar . val) in envvars ,(cl-loop for (var . val) in envvars
if val concat (format " $env:%s = $__%s\n " var var))
concat (format " $env:%s = $__%s\\\n " envvar envvar)) "\n}")))))
"\n}")))))) (doom-log "_doomrun: %s %s"
(doom-log "_doomrun: %s %s" (string-join persisted-env " ") command) (cl-loop for (var . val) in persisted-env
concat (format "%s=%s \\\n" var (shell-quote-argument val)))
command)
(doom-log "_doomcleanup: %s" persistent-files) (doom-log "_doomcleanup: %s" persistent-files)
;; Error code 254 is special: it indicates to the caller that the ;; Error code 254 is special: it indicates to the caller that the
;; post-script should be executed after this session ends. It's up to ;; post-script should be executed after this session ends. It's up to