[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Buffer-local process environments
From: |
Michael Albinus |
Subject: |
Re: Buffer-local process environments |
Date: |
Sun, 09 May 2021 18:38:31 +0200 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) |
Augusto Stoffel <arstoffel@gmail.com> writes:
> Hi Michael,
Hi Augusto,
>>> 2. Introduce a blacklist of variables that are never exported to a
>>> remote. This can be done by extending
>>> `tramp-remote-process-environment' to follow the same convention of
>>> `process-environment' that an entry of the form VAR, without the
>>> =VALUE, means removing the variable.
>>
>> There are already such variables to be unset. These are the variables
>> without any value, like "HISTORY="
>
> This only works for entries in the default toplevel value of
> `process-environment'. Not for things added buffer-locally or
> in a let-binding.
Yes, this is a bug (rather a missing feature) of Tramp. It is
implemented for synchronous processes, but not for asynchronous
processes. I'm aware of this, but I haven't found the mood to implement
it until now.
> The heuristic that every env var which is not in the default toplevel
> value of `process-environment' will be exported to the remote works
> pretty well. But it needs a simple way to be overridden, since it can't
> read the user's mind.
Sure. Tramp shall not read the user's mind.
(As side remark, my wife expects always that I read her mind. I fail
miserably, usually.)
>> TERM is handled special. All Tramp connections add "TERM=dumb",
>> hard-coded. Since this shall not be changed by a user, it isn't
>> configurable here.
>
> Not all connections add "TERM=dumb". Here's a counterexample:
>
> (let ((default-directory "/sudo::/")
> (process-environment `("TERM=dumber"
> ,@process-environment)))
> (shell-command "echo $TERM" t))
> => dumber
>
> How do I tell Tramp not to export TERM=dumber to the remote in this
> case?
You can always shoot yourself in your feed.
> To be clear: this may be a dumb example, but there are plenty of
> interesting use-cases involving PATH, PYTHONPATH, and so on, which are
> analogous. How can I set PYTHONPATH buffer-locally, but disallow Tramp
> to export this variable to a remote?
Set it only, when the buffer is not remote. Something like
(unless (file-remote-p default-directory) ...)
>>> As another example, python.el would append "PYTHONPATH" and
>>> "PYTHONHOME" globally to `tramp-remote-process-environment', since
>>> these variables hold directory names.
>>
>> Yes, if python.el developers prefer that. However, I doubt, that this
>> value is always the same for all different remote hosts, the value might
>> differ depending on the OS the remote host is running.
>
> That's precisely what I meant: you *do not* want to export those variables
> to a remote. But currently Tramp effectively forbids anyone to set
> PYTHONPATH buffer-locally, because this will cause them to be exported.
No. See above.
> My suggestion is easier to implement than explain. So let me repeat it
> in the form of a little (possibly incomplete) patch:
>
> diff --git a/lisp/net/tramp.el b/lisp/net/tramp.el
> index 015f458a63..3749f84e21 100644
> --- a/lisp/net/tramp.el
> +++ b/lisp/net/tramp.el
> @@ -3938,21 +3938,23 @@ tramp-handle-make-process
> (when (string-match-p "=" elt) elt))
> tramp-remote-process-environment))
> ;; We use as environment the difference to toplevel
> ;; `process-environment'.
> (env (dolist (elt process-environment env)
> (when
> (and
> (string-match-p "=" elt)
> (not
> (member
> - elt (default-toplevel-value
> 'process-environment))))
> + elt (default-toplevel-value 'process-environment)))
> + (not (member (car (split-string elt "="))
> + tramp-remote-process-environment)))
> (setq env (cons elt env)))))
> (env (setenv-internal
> env "INSIDE_EMACS" (tramp-inside-emacs) 'keep))
> (env (mapcar #'tramp-shell-quote-argument (delq nil env)))
> ;; Quote command.
> (command (mapconcat #'tramp-shell-quote-argument command " "))
> ;; Set cwd and environment variables.
> (command
> (append `("cd" ,localname "&&" "(" "env") env `(,command ")"))))
>
I see. Interesting.
But this still means, that you use something global in
tramp-remote-process-environment for all remote processes.
What, if you want to set PYTHONPATH for hosta, and not for hostb?
Best regards, Michael.