Subject: Re: bug#78097: 30.1; editorconfig can pass nil to file-name-directory

From: Stefan Monnier <[email-masked]>
Date: Sat, 17 May 2025 11:15:54 -0400

>>> FWIW, the fact that `editorconfig-get-local-variables-functions' is run
>>> when there are no `.editorconfig` files is an accident.
>> The reason I was using `editorconfig-get-local-variables-functions' is
>> because both `editorconfig-after-apply-functions' and
>> `editorconfig-hack-properties-functions' are documented to behave that
>> way but are deprecated and `editorconfig-hack-properties-functions'
>> reports that `editorconfig-get-local-variables-functions' should be used
>> instead.
>> If none of these hooks are available for this purpose can you recommend
>> a way that I can achieve my goal?
>
> I'm afraid I don't have a ready answer for it.
> Maybe something like:
>
>     (add-hook 'change-major-mode-after-body-hook #'my-global-file-defaults)
>     (defun my-global-file-defaults ()
>       (when buffer-file-name
>         (add-hook 'before-save-hook
>                   #'editorconfig--delete-trailing-whitespace nil t)))
>
> Obviously, Emacs lacks an official "delete-trailing-whitespace mode":
> the above should not have to rely on an editorconfig-specific function.
> 🙁

There is now a `delete-trailing-whitespace-mode` in `master, so the
above snippet can be updated to something like:

    (add-hook 'change-major-mode-after-body-hook #'my-global-file-defaults)
    (defun my-global-file-defaults ()
      (when buffer-file-name
        (if (fboundp 'delete-trailing-whitespace-mode) ;; Emacs≥31
            (delete-trailing-whitespace-mode 1)
          (add-hook 'before-save-hook
                    #'editorconfig--delete-trailing-whitespace nil t))))

Eli writes:
> Ping! How should we proceed with this issue?  Was it resolved?

I think the above provides the OP with a good alternative solution.
Their code relied on an accidental "feature" which is that
`editorconfig-get-local-variables-functions` is run even if we found no
`.editorconfig` settings.  I think that in the absence of such settings,
`editorconfig-mode` should have no effect, so I suggest we install the
patch below.

I'd lean towards pushing it to `master` rather than `emacs-30`.

        Stefan

2025-05-17  Stefan Monnier  <[email-masked]>

    * lisp/editorconfig.el (editorconfig--get-dir-local-variables):
    Don't run `editorconfig-get-local-variables-functions` when we found no
    EditorConfig settings since that hook's purpose is "to convert
    EditorConfig settings to their Emacs equivalent".

diff --git a/lisp/editorconfig.el b/lisp/editorconfig.el
index bfc8ef46ed3..3658343cfac 100644
--- a/lisp/editorconfig.el
+++ b/lisp/editorconfig.el
@@ -709,7 +708,8 @@ editorconfig--get-dir-local-variables
 Meant to be used on `hack-dir-local-get-variables-functions'."
   (when (stringp buffer-file-name)
     (let* ((props (editorconfig-call-get-properties-function buffer-file-name))
-           (alist (editorconfig--get-local-variables props)))
+           (alist (if (< 0 (hash-table-count props))
+                      (editorconfig--get-local-variables props))))
       ;; FIXME: If there's `/foo/.editorconfig', `/foo/bar/.dir-locals.el',
       ;; and `/foo/bar/baz/.editorconfig', it would be nice to return two
       ;; pairs here, so that hack-dir-local can give different priorities