Subject: bug#48452: 28.0.50; flymake for elisp does not respect `load-path`

From: João Távora <[email-masked]>
Date: Sat, 23 Jul 2022 18:45:04 +0100

Stefan Monnier <[email-masked]> writes:

>> At some point in the past, Stefan was working on a "sandboxed" Emacs
>> that could, in theory, pave the way for automatically enabled Elisp
>> Flymake, but I haven't heard of that effort lately.
>
> I need to get back to that indeed :-)
>
>>> It seems to me that there's two useful values for load-path in the
>>> Flymake backend: Either just the standard load-path (so that you
>>> actually get the same results as when doing a batch byte-compile) or the
>>> current running load-path (so that you get the same results as when you
>>> `require' the file from your .emacs, say).  Altering the load-path to
>>> also include the ELPA directories doesn't really help much, because
>>> people have all kinds of code that's not in ELPA (but is in their
>>> load-path).
>
> AFAIC, it's not just `load-path`: the set of autoloaded functions (and
> a few other similar things) is also relevant.

I presume those relevant things are setup by package-initialize, right?
My proposed patch uses that.

>> I think we have to ask ourselves: what is Flymake used for?  The most
>> useful answers will come from the people who actually use it, though
>> potential uses are also interesting.
>
> I don't think we can hope to make flymake-elisp work correctly in all
> existing cases, because there are conflicting requirements there.
>
> So, we should take it for granted that some use-cases will be considered
> as "unsupported", and the important thing is to figure out what behavior
> to provide such that all(?) use-cases can be adapted (and such that the
> behavior is sane enough to be described, understandable, and
> predictable).
>
>> Having './' in the default load-path for elisp-flymake-byte-compile is
>> fundamental for the accuracy of this prediction.  Why?  Because the
>> clients of my packages -- regardless if they use package.el,
>> straight.el, etc or just simply using a git checkout -- will always have
>> the the files I have in some directory in some other directory in their
>> machines, and _that_ directory will be in the load-path.
>
> BTW, while the GNUmakefile of `elpa-admin` also adds `.` to the
> `load-path`, there are cases where this is harmful.

I don't dispute that, but in my experience (and as far as I can see) it
is _not_ functionally harmful to have ./ in the
elisp-flymake-byte-compile-load-path for the use case that I described:
developing package.el packages that can be distributed and installed
through a number of ways.

But is it potentially "dangerous/disastrous"?  I don't think so either,
but I haven't a clear picture of the disaster scenario.  Lars mentioned
"editing files in /tmp".

Maybe Lars is worried about some user e.g. having /tmp/bad.el and
opening some /tmp/good.el and slowly typing in

   (require 'badminton)

By the time (require 'bad) is in the buffer, disaster strikes.  But this
disaster could just as well happen by simply visiting /tmp/bad.el to see
what's in it.  Except that malicious files don't advertise themselves
like that in their file names...

Or maybe -- again, I'm just guessing -- the danger is that that bad.el
is disguised under /tmp/pcase.el and /tmp/good.el has a perfectly
legitimate.

   (require 'pcase)

Simply visitng /tmp/good.el with Flymake on would lead to disaster.  If
that's the case, it's as easy as applying this patch

diff --git a/lisp/progmodes/elisp-mode.el b/lisp/progmodes/elisp-mode.el
index 0c4a9bfdbe..01c0679c76 100644
--- a/lisp/progmodes/elisp-mode.el
+++ b/lisp/progmodes/elisp-mode.el
@@ -2144,7 +2144,7 @@ elisp-flymake-byte-compile
                    "-Q"
                    "--batch"
                    ;; "--eval" "(setq load-prefer-newer t)" ; for testing
-                   ,@(mapcan (lambda (path) (list "-L" path))
+                   ,@(mapcan (lambda (path) (list "-L" (format ":%s" path)))
                              elisp-flymake-byte-compile-load-path)
                    "-f" "elisp-flymake--batch-compile-for-flymake"
                    ,temp-file)

João