Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added tramp support #68

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 17 additions & 18 deletions direnv.el
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,11 @@

(defun direnv--detect ()
"Detect the direnv executable."
(executable-find "direnv"))
(executable-find "direnv" t))

(defvar direnv--output-buffer-name "*direnv*"
"Name of the buffer filled with the last direnv output.")

(defvar direnv--executable (direnv--detect)
"Detected path of the direnv executable.")

(defvar direnv--active-directory nil
"Name of the directory for which direnv has most recently ran.")

Expand Down Expand Up @@ -103,21 +100,21 @@ use `default-directory', since there is no file name (or directory)."

(defun direnv--export (directory)
"Call direnv for DIRECTORY and return the parsed result."
(unless direnv--executable
(setq direnv--executable (direnv--detect)))
(unless direnv--executable
(user-error "Could not find the direnv executable. Is ‘exec-path’ correct?"))
(let ((environment process-environment)
(stderr-tempfile (make-temp-file "direnv-stderr"))) ;; call-process needs a file for stderr output
(unwind-protect
(with-current-buffer (get-buffer-create direnv--output-buffer-name)
(erase-buffer)
(let* ((default-directory directory)
(process-environment environment)
(exit-code (call-process
direnv--executable nil
`(t ,stderr-tempfile) nil
"export" "json")))
(process-environment (if (file-remote-p default-directory)
tramp-remote-process-environment
environment))
(direnv--executable (direnv--detect))
(exit-code (if (null direnv--executable)
-1
(process-file direnv--executable nil
`(t ,stderr-tempfile) nil
"export" "json"))))
(prog1
(unless (zerop (buffer-size))
(goto-char (point-max))
Expand Down Expand Up @@ -162,7 +159,6 @@ use `default-directory', since there is no file name (or directory)."
(with-current-buffer (window-buffer)
(let ((directory-name (direnv--directory)))
(when (and directory-name
(not (file-remote-p directory-name))
(not (string-equal direnv--active-directory directory-name))
(file-directory-p directory-name))
(direnv-update-directory-environment directory-name)))))
Expand Down Expand Up @@ -255,8 +251,6 @@ a summary message."
(items)
(summary)
(show-summary (or force-summary (called-interactively-p 'interactive))))
(when (file-remote-p directory)
(user-error "Cannot use direnv for remote files"))
(setq direnv--active-directory directory
items (direnv--export direnv--active-directory)
summary (direnv--summarise-changes items))
Expand All @@ -266,8 +260,13 @@ a summary message."
(direnv--show-summary summary old-directory direnv--active-directory))
(dolist (pair items)
(let ((name (car pair))
(value (cdr pair)))
(value (cdr pair))
(process-environment (if (file-remote-p directory)
tramp-remote-process-environment
process-environment)))
(setenv name value)
(when (file-remote-p directory)
(setq tramp-remote-process-environment process-environment))
(when (string-equal name "PATH")
(setq exec-path (append (parse-colon-path value) (list exec-directory)))
;; Prevent `eshell-path-env` getting out-of-sync with $PATH:
Expand All @@ -278,7 +277,7 @@ a summary message."
(defun direnv-allow ()
"Run ‘direnv allow’ and update the environment afterwards."
(interactive)
(call-process (direnv--detect) nil 0 nil "allow")
(process-file (direnv--detect) nil 0 nil "allow")
(direnv-update-environment))

;;;###autoload
Expand Down