use-package icon indicating copy to clipboard operation
use-package copied to clipboard

Flycheck mode somehow affects global state?

Open justinabrahms opened this issue 6 years ago • 4 comments

I think this is a use-package problem, but I could be wrong. I'm having an issue where the first thing that I'm porting to use use-package is polluting global state where my .sh files are being rendered in js2-mode.

My minimal emacs config to reproduce the issue is below. It assumes you have flycheck installed.

(require 'package)

(add-to-list 'package-archives
	     '("melpa" . "http://melpa.org/packages/") t)
(add-to-list 'package-archives 
             '("org" . "http://orgmode.org/elpa/") t)

(package-initialize)

(when (not package-archive-contents)
  (package-refresh-contents))

(eval-when-compile (require 'use-package))

(use-package js2-mode
  :ensure
  :mode ("\\.js" . js2-mode)
  :hook (flycheck-mode))

(add-hook 'sh-mode-hook 'flycheck-mode)

If you open a .js file then open a .sh file, it will be in js2-mode. I think this has something to do with how the hooks work. Hopefully someone can help?

justinabrahms avatar Oct 29 '19 06:10 justinabrahms

Tue Oct 29 09:53:02 GMT 2019

Using emacs-lisp-macroexpand on the use-package snippet above, produces:

 (add-hook 'flycheck-mode-hook #'js2-mode)

This means that whenever flycheck-mode is activated, js2-mode will be activated.

Your intention is to add flycheck-mode to js2-mode-hook.

This is achieved by the following use-package :hook snippet:

:hook (js2-mode . flycheck-mode)

HTH

rprimus avatar Oct 29 '19 10:10 rprimus

Thanks so much @rprimus! I misunderstood what :hooks does, but I don't see a facility for "when I launch js2-mode, please also load these other packages". This seems pretty standard. Is this a feature of use-package that I'm missing?

justinabrahms avatar Oct 30 '19 03:10 justinabrahms

Wed Oct 30 09:32:43 GMT 2019

I don't see a facility for "when I launch js2-mode, please also load these other packages"

@justinabrahms That's exactly what hooks are used for. Do have a read of: Hooks from the Emacs manual.

use-package is just a macro to help contain the configuration for a particular package.

This issue https://github.com/abo-abo/swiper/issues/2267 (the first two comments) may help put things in perspective (about the convenience of use-package).

rprimus avatar Oct 30 '19 09:10 rprimus

agree with @rprimus. :hook (js2-mode . flycheck-mode) is recommend too. I show another option, create ``flycheckuse-package block and configure:hook` keyword.

(use-package flycheck-mode
  :hook js2-mode emacs-lisp-mode web-mode)

I hope this helps you.

conao3 avatar Feb 22 '21 16:02 conao3

This support question was answered (thanks!) so I'm closing this issue.

skangas avatar Nov 13 '22 19:11 skangas