-
Notifications
You must be signed in to change notification settings - Fork 38
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
direnv loads too late sometimes #17
Comments
That is the way I solved it for setup. |
this packages uses:
which puts it at the beginning:
but packages loaded later may use the same pattern, causing direnv to get push further down. perhaps this package could try to move its own hook to the front all the time. |
Thanks for the pointer @bkchr and that does indeed solve the flycheck issue but it is a workaround specific to flycheck and a proper fix for direnv would be very neat. |
Yeah a proper fix would be better :) I already do this "hack" for flycheck and the lsp-mode. |
actually i am not convinced what the right "fix" is tbh, since i do not fully understand the actual issue here. for instance, i use direnv with python and virtualenvs which also contain linters (flake8) and i have never experienced issues even though the flycheck linter requires the |
The right fix would something that runs direnv directly after switching to the buffer, before any minor/major mode hooks are called. For example the lsp mode starts are server after entering a buffer and to start the server it needs to know the location of the executable. So, the executable of the server is searched when the major-mode is loaded and that is before direnv has setup the environment. |
ok. if you know the right trick/hook (if it exists at all) to get direnv to run at the right time, lemme know, because i have no idea apart from |
Maybe |
but that hook is definitely not good enough to cover the main use case of this package: when switching between files (buffers, windows, frames) the env needs to keep "in sync" with the current file. perhaps both could work? can you try adding |
fwiw, for flycheck it seems this does the trick: (add-hook 'flycheck-before-syntax-check-hook 'direnv-update-environment) |
Hey all, I've been playing with (use-package direnv
:hook (flycheck-before-syntax-check . direnv-update-environment)
:config
(direnv-mode))
(use-package flycheck
:init
(global-flycheck-mode))
(use-package flycheck-haskell
:hook (haskell-mode . flycheck-haskell-setup))
(use-package haskell) I get something like a (use-package direnv
:config
(direnv-mode))
(use-package flycheck
:init
(global-flycheck-mode)
:config
(setq flycheck-executable-find
(lambda (cmd) (direnv-update-environment default-directory) (executable-find cmd))))
(use-package flycheck-haskell
:hook (haskell-mode . flycheck-haskell-setup))
(use-package haskell)
|
what happens when you also add direnv to find-file-hook? (without "patching" flycheck)? |
Same issue ( (use-package direnv
:hook ((find-file flycheck-before-syntax-check) . direnv-update-environment)
:config
(direnv-mode))
(use-package flycheck
:init
(global-flycheck-mode))
(use-package flycheck-haskell
:hook (haskell-mode . flycheck-haskell-setup))
(use-package haskell) |
In case any of you are using this with spacemacs, the trick to get everything to pay nice was to modify from:
Then you end up with a proper environment. |
@peterhoeg what's |
|
If anyone have issues with (add-hook 'before-hack-local-variables-hook #'direnv-update-environment) |
I was having the same issue with (use-package direnv
:config
(direnv-mode)
(advice-add 'python-mode :before #'direnv-update-environment)) You can probably use Now |
Shouldn't this work then? (use-package direnv
:init
(add-hook 'prog-mode-hook #'direnv-update-environment)
:config
(direnv-mode)) |
@hakanserce @peterhoeg would (add-hook 'before-hack-local-variables-hook #'direnv-update-environment) if so, perhaps that should be default for |
Ohhh, didn't know about that one. Looks much more appropriate! |
...when direnv-mode is used. this should cause direnv to load earlier which can avoid certain issues when opening a file in a direnv controlled directory for the first time. see #17.
i've opened #54 to use |
...when direnv-mode is used. this should cause direnv to load earlier which can avoid certain issues when opening a file in a direnv controlled directory for the first time. see #17.
Based on @wbolster's suggestion, this works for me: (use-package direnv
;; Ensures that external dependencies are available before they are called.
:hook (before-hack-local-variables . #'direnv-update-environment)
:config
(add-to-list 'direnv-non-file-modes 'vterm-mode)
(direnv-mode 1)) |
FWIW, none of the above solutions (tried with both (add-hook 'prog-mode-hook
(lambda () (progn (direnv-update-environment) (lsp)))) |
I encountered the problem for the first time today in a Haskell project that uses The fixes above did not work for me, but I found another (very strange) workaround. Instead of starting my emacs session (I use the daemon and only tested it with the TTY emacsclient) like this from the project directory:
I found that just starting emacs from within my home directory like this solves the problem:
I don't know why, but simply starting from another directory might help other people too. I'd love an explanation though :D Edit: Another (probably simpler) workaround for me was just opening a non-Haskell file inside the project directory first. |
I'm guessing you have |
No, I just started using |
...when direnv-mode is used. this should cause direnv to load earlier which can avoid certain issues when opening a file in a direnv controlled directory for the first time. see #17.
with #54 merged i will close this issue... let's see how it works out for the many people who commented here 😬 🤞 |
I'm running into this issue where lsp-mode isn't playing nice, with the It looks like that runs so early that |
I found a use case and that this issue is still a problem. When providing the language server with per-project resources using nix, the shell entry needs to be done, as others have identified, when direnv can succeed at updating, but before lsp starts. The exact case is rust-analyzer needing to know |
(use-package direnv ; direnv integration
:after lsp
:delight 'direnv-mode
:config
;; Ensures that external dependencies are available before they are called.
(add-hook 'prog-mode-hook #'direnv--maybe-update-environment)
(setq direnv-always-show-summary nil)
(direnv-mode 1)) I switched from a Rustic advice to a hook and got back rolling. I like the use of |
The |
I must admit that I've taken the easy way out - disabled emacs-direnv and then simply launch an emacs instance per project. Regular file editing goes via the daemon but everything else is a project specific instance. |
direnv doesn't add itself to the front of the
post-command-hook
meaning other things get to run before direnv has set up the environment.One example is a haskell/stack project directory, where flycheck cannot find the executables because it runs before direnv has updated the
PATH
.Cc: @bkchr
The text was updated successfully, but these errors were encountered: