Skip to content

Conversation

@dschrempf
Copy link

Fixes #55. The hook is probably called more often than necessary. Is there a better position in the source code of envrc for it to be called?

This allows setting (buffer-local) variables from local environment variables. For example,

(defun my-set-jdtls-path ()
  (setq-local lsp-java-server-install-dir (getenv "JDTLS_PATH")))

(add-hook 'envrc-post-update-hook 'my-set-jdtls-path)

This allows setting (buffer-local) variables from local environment
variables. For example,

    (defun my-set-jdtls-path ()
      (setq-local lsp-java-server-install-dir (getenv "JDTLS_PATH")))

    (add-hook 'envrc-post-update-hook 'my-set-jdtls-path)
@purcell
Copy link
Owner

purcell commented Mar 17, 2023

Thanks! I'll articulate here the reason I haven't immediately merged this.

Namely, I expect that some of the things one might do in such a hook would be specific to a major mode, e.g. because the variable being set from an env var is specific to a major mode. By modifying envrc-post-update-hook globally, one would have to use guards like (when (derived-mode-p 'cc-mode) (setq-local blah ... (getenv "FOO"))) in the function added to the hook, which is a bit clunky.

So one might then naturally try to modify the hook buffer-locally, e.g. with

(defun enable-sync-jdtls-path ()
  (add-hook 'envrc-post-update-hook 'my-set-jdtls-path nil t))
(add-hook 'java-mode-hook 'enable-sync-jdtls-path nil t)

but then the envc-post-update-hook might end up getting invoked during major mode initialisation before enable-sync-jdtls-path, leading to confusing results.

So I'm pondering whether there's an alternate pattern or approach to make this both easy and reliable. Perhaps I'm also overthinking things, so let me know what you think.

@zackw
Copy link

zackw commented Jun 21, 2023

Hmm, what about an envrc function (for discussion, call it envrc-after-update) that checks whether the active buffer visits a file managed by an .envrc, and if so, runs the supplied hook function immediately if the update has already happened, and also records it as a buffer-local entry in envrc-post-update-hook? Then

(defun my-set-jdtls-path ()
  (setq-local lsp-java-server-install-dir (getenv "JDTLS_PATH")))

(defun my-enable-sync-jdtls-path ()
  (envrc-after-update 'my-set-jdtls-path))

(add-hook 'java-mode-hook 'my-enable-sync-jdtls-path)

should do the Right Thing regardless of whether envrc-post-update-hook runs before or after java-mode-hook, or am I missing something?

@accelbread
Copy link
Contributor

What I currently usually do is in the mode hook, I add a buffer-local hook to hack-local-variables-hook to defer stuff until after envrc is loaded.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Question about other buffer local variables

4 participants