Subject: bug#23407: .dir-local settings get obliterated on running a major mode function.

From: Stefan Monnier <[email-masked]>
Date: Tue, 03 May 2016 14:10:45 -0400

>> > +    ;; delay-mode-hooks is set when `byte-compile-file' is the caller.
>> > +    ;; It is essential that we call `hack-local-variables' in order to
>> > +    ;; set up `lexical-binding', since `run-mode-hooks' is prevented
>> > +    ;; from doing its job.
>> > +    (when delay-mode-hooks
>> > +      (with-demoted-errors "File local-variables error: %s"
>> > +        (hack-local-variables 'no-mode))))

>> But this seems terribly brittle.  Do we care about delay-mode-hooks (as
>> the code says) or about byte-compile-file (as the comment says)?

> This bit of code was necessitated by:

> #########################################################################
> commit 3ba6b3a9c1e0565ee5f45f11a9c09702a24f8453
> Author: Artur Malabarba <[email-masked]>
> Date:   Sun Apr 12 03:23:35 2015 +0100
>
>     Speed up byte-compilation and autoload generation by avoiding mode-hooks
>
>     This prevents emacs-lisp-mode-hook from being run everytime an
>     autoload file is generated, which can account for a fraction of
>     package installation time depending on the hooks the user has
>     configured.
>
>     * lisp/emacs-lisp/bytecomp.el (byte-compile-file): Use
>     * delay-mode-hooks.
>
>     * lisp/emacs-lisp/autoload.el (autoload-find-file)
>     (autoload-find-generated-file): Use delay-mode-hooks.
> #########################################################################

Hmm... so you're working around someone else's hack!

> if hack-local-variables isn't run, lexical-binding (for example) doesn't
> get set up, and make bootstrap fails.

Of course: hack-local-variables should be run unconditionally.

>> If it's the former, then the comment needs to be fixed, if it's the
>> latter, than we need to find some other way to tell this code what's
>> going on.
> I don't really understand the question.  Sure, that bit of code is ugly.
> But the comment both motivates ("it's `byte-compile-file''s fault") and
> explains the problem (which is that `delay-mode-hooks' being set would
> prevent `run-mode-hook', and thus `hack-local-variable' from running).
> It is possible that other stuff might call `normal-mode' like this.
> What sort of changes do you advocate for the comment (or for the code)?

How 'bout doing

    (setq-local hack-local-variables--done t)

in hack-local-variables, and then testing that instead of testing
delay-mode-hooks?

        Stefan