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

Don't configure a package if not installed? #693

Closed
MetroWind opened this issue Jun 20, 2018 · 5 comments
Closed

Don't configure a package if not installed? #693

MetroWind opened this issue Jun 20, 2018 · 5 comments
Labels

Comments

@MetroWind
Copy link

What's the best way to not configure a package if it's not installed?

Suppose I have this in my .emacs.el:

(use-package web-mode
  :mode "\\.html?$"
  :config
  ;; some setqs...
  )

If I don't have web-mode installed, the :config part will never trigger, but looks like the :mode setting is still done. So if I try to open a HTML file, it will complain about cannot loading web-mode. Ideally, it would be best that if web-mode is not installed the whole use-package call is disabled. I suppose I can put a :if (package-installed-p 'web-mode) there, but that only works if web-mode is installed through elpa (I'm not sure, is that true?). It could also be slower than necessary (again I'm not sure. Is it slow?). Is there a better way?

@waymondo
Copy link
Collaborator

waymondo commented Jul 3, 2018

The :if (package-installed-p 'web-mode) method you've described seems reasonable, and will simply look for the presence of the package regardless of the package archive it was installed with.

Do you have a specific reason you don't want to just use :ensure t in this case?

@noctuid
Copy link

noctuid commented Oct 19, 2018

Use-package uses locate-library for use-package-check-before-init. It would be great if there was an equivalent variable that worked the same way as :if (i.e. it put all non-installation configuration inside a (when (locate-library (symbol-name package)) ...)). Right now I'm using a wrapper around use-package that does this, but it would be nicer if this was builtin.

Edit: This isn't really necessary if you don't have debug-on-error non-nil during init (because of the condition-case-unless-debug), but I set it to t for my init in order to debug non-recoverable errors in my init without having to rerun Emacs. I guess an alternative would be to have a variable to use condition-case instead and never error?

@skangas
Copy link
Collaborator

skangas commented Dec 8, 2022

I think you should use :if (locate-library "web-mode"), or :if (or (locate-library "web-mode") (package-installed-p 'web-mode)). Perhaps we should add some notes to that effect to the documentation.

@skangas skangas added the docs label Dec 8, 2022
@skangas
Copy link
Collaborator

skangas commented Dec 9, 2022

I've now added such an example to the use-package manual: https://git.savannah.gnu.org/cgit/emacs.git/commit/?h=emacs-29&id=a17a6036dd4549514c7cecc7cdd93a571bf57ece

This change will be a part of the use-package shipped with Emacs 29. I'm therefore closing this issue.

@skangas skangas closed this as completed Dec 9, 2022
yangyingchao pushed a commit to yangyingchao/emacs that referenced this issue Dec 9, 2022
* doc/misc/use-package.texi (Conditional loading): Expand section with
examples.

Resolves jwiegley/use-package#693
@monnier
Copy link

monnier commented Dec 10, 2022

I don't really understand why people want to use locate-library here (seems costly to me), but FWIW I think the better test here is :if (fboundp 'web-mode).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants