To use directory-files-recursively in Emacs 24.x, I'm adding the following to a file cider-compat.el:

  (unless (fboundp 'directory-files-recursively)
    (defun directory-files-recursively (dir regexp &optional include-directories)

Byte-compiling the file, however, gives an error in Emacs 24.x:

In toplevel form:
cider-compat.el:191:1:Error: the function `directory-files-recursively' might not be defined at runtime.
make: *** [cider-compat.elc-test] Error 1

Why is this? The eval-and-compile seems to work fine for other defuns, e.g. internal--build-binding.


That's because the compiler simply sees that at compile-time the function is defined (because of the eval-and-), but it sees that at runtime, the function will only be defined (if <blabla>, IOW it will only be defined in some circumstances, and it doesn't bother to try to figure out what those circumstances would be, so it emits the warning because of the doubt.

You can typically avoid these kinds of issues (and avoid eval-and-compile as well, which tends to be a bit quirky) with:

(defalias 'my-directory-files-recursively
  (if (fboundp 'directory-files-recursively
    (lambda (dir regexp &optional include-directories)
  • That's one solution, but a with-no-warnings around the body seems to work as well. Jul 22 '17 at 3:42
  • Better than with-no-warning (which is a blunt tool), if you only want to silence the warning rather than make the code understandable to the compiler, you can use declare-function.
    – Stefan
    Jul 22 '17 at 16:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.