Use-package: :functions sometimes doesn't have desired effect

Created on 13 Dec 2017  Â·  6Comments  Â·  Source: jwiegley/use-package

Coming over from this conversation on twitter, here's the result of the test @jwiegley asked me to do:

(progn
  (eval-and-compile
    (declare-function hc-highlight-trailing-whitespace "highlight-chars")
    (eval-when-compile
      (with-demoted-errors "Cannot load highlight-chars: %S" nil
                           (load "highlight-chars" nil t))))
  (defvar use-package--warning112
    #'(lambda
        (keyword err)
        (let
            ((msg
              (format "%s/%s: %s" 'highlight-chars keyword
                      (error-message-string err))))
          (display-warning 'use-package msg :error))))
  (condition-case-unless-debug err
      (if
          (not
           (require 'highlight-chars nil t))
          (display-warning 'use-package
                           (format "Cannot load %s" 'highlight-chars)
                           :error)
        (condition-case-unless-debug err
            (progn
              (add-hook 'after-change-major-mode-hook
                        (lambda nil
                          (when
                              (buffer-file-name)
                            (hc-highlight-trailing-whitespace))))
              t)
          (error
           (funcall use-package--warning112 :config err))))
    (error
     (funcall use-package--warning112 :catch err))))

This is expanded from this form:

(use-package highlight-chars
  :ensure t
  :demand
  :functions hc-highlight-trailing-whitespace
  :config
  (add-hook 'after-change-major-mode-hook
            (lambda ()
              (when (buffer-file-name)
                (hc-highlight-trailing-whitespace)))))

The background is that, recently, :functions isn't having the desired effect for me, but only in my full Emacs environment. If I create a minimal one that has enough to make use of use-package I can't recreate the issue. The problem is that, when :functions is used, I still get this error when I compile:

In end of data:
init-packages-melpa.el:246:1:Warning: the function
    ‘hc-highlight-trailing-whitespace’ might not be defined at runtime.

If I swap to using :commands instead it compiles without the warning.

use-package version is use-package-20171210.2136, as pulled from melpa "unstable". Emacs version is 25.3.

bug

Most helpful comment

(eval-when-compile
  (setq use-package-expand-minimally byte-compile-current-file))

In case anyone else is having this problem, this it what let me get rid of the byte compile warnings.

All 6 comments

I see, it appears the logic designed to catch errors is preventing the byte compiler from seeing that there's a require before the function would ever get called.

I wonder if this still happens for you if you have the following variable set before the use-package form: (setq use-package-expand-minimally t).

If that works, I'm not sure there's any way to fix this except by establishing an autoload you don't actually need using :commands, as you suggested.

I no longer get the warning with use-package-expand-minimally set to t.

I'm afraid this isn't something I can fix. If you want fewer warnings during byte-compilation (and a slightly faster startup), you can set: (setq use-package-expand-minimally byte-compile-current-file).

Noted. Thanks for looking at this for me.

I am seeing this issue, too, and neither of the workarounds have any effect for me.

(eval-when-compile
  (setq use-package-expand-minimally byte-compile-current-file))

In case anyone else is having this problem, this it what let me get rid of the byte compile warnings.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Yevgnen picture Yevgnen  Â·  5Comments

DamienCassou picture DamienCassou  Â·  9Comments

manute picture manute  Â·  8Comments

DarkWingMcQuack picture DarkWingMcQuack  Â·  4Comments

DamienCassou picture DamienCassou  Â·  8Comments