zsh-workers
 help / color / Atom feed
* zcompile does not expand aliases defined in the compiled script
@ 2020-07-07 10:00 Roman Perepelitsa
  2020-07-07 20:28 ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2020-07-07 10:00 UTC (permalink / raw)
  To: Zsh hackers list

I naively expected that `zcompile x && source x` has the same effect
as plain `source x` but apparently when using the former aliases
defined within `x` aren't expanded during the evaluation of `x`.

Is this expected behavior?

  % zsh -f
  % print 'alias x=:; x' >x
  % zcompile x
  % source x
  x:1: command not found: x

Roman.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: zcompile does not expand aliases defined in the compiled script
  2020-07-07 10:00 zcompile does not expand aliases defined in the compiled script Roman Perepelitsa
@ 2020-07-07 20:28 ` Mikael Magnusson
  2020-07-08  5:18   ` Roman Perepelitsa
  0 siblings, 1 reply; 4+ messages in thread
From: Mikael Magnusson @ 2020-07-07 20:28 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh hackers list

On 7/7/20, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> I naively expected that `zcompile x && source x` has the same effect
> as plain `source x` but apparently when using the former aliases
> defined within `x` aren't expanded during the evaluation of `x`.
>
> Is this expected behavior?
>
>   % zsh -f
>   % print 'alias x=:; x' >x
>   % zcompile x
>   % source x
>   x:1: command not found: x

It is expected, but you are not correct that it works when you didn't
compile the file first:
% source x
zsh: command not found: x

However, sourcing a non-compiled file multiple times will of course
work on subsequent runs because the alias is carried across in the
shell session. If you zcompile the file after sourcing it once, it
will also work subsequently (even in new sessions) as aliases, if any,
are expanded at compile time (unless inhibited by -U).

It is possible that you changed the example in the mail without trying
what you changed; if you have the statements
alias x=:
x
in the file x, then sourcing it (non-compiled) will work, since it is
parsed and executed line by line. This does not change anything for
the zcompile case from the above paragraph as the entire file is
parsed and nothing is executed.

Hopefully all of the above is true, I did my best to test it  :).

-- 
Mikael Magnusson

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: zcompile does not expand aliases defined in the compiled script
  2020-07-07 20:28 ` Mikael Magnusson
@ 2020-07-08  5:18   ` Roman Perepelitsa
  2020-07-08 16:59     ` Mikael Magnusson
  0 siblings, 1 reply; 4+ messages in thread
From: Roman Perepelitsa @ 2020-07-08  5:18 UTC (permalink / raw)
  To: Mikael Magnusson; +Cc: Zsh hackers list

On Tue, Jul 7, 2020 at 10:28 PM Mikael Magnusson <mikachu@gmail.com> wrote:
> It is possible that you changed the example in the mail without trying
> what you changed; if you have the statements
> alias x=:
> x
> in the file x, then sourcing it (non-compiled) will work

Yes, that's exactly it. Given a script like this:

  alias x=:
  x

Sourcing it without zcompile works (`x` on the second line is
alias-expanded), but once the script is zcompiled an attempt to source
it results in an error (`x` doesn't get expanded).

I looked at the source code of zcompile and I understand why this
happens. It makes perfect sense when zcompiling functions. It is,
however, surprising when zcompiling scripts.

Roman.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: zcompile does not expand aliases defined in the compiled script
  2020-07-08  5:18   ` Roman Perepelitsa
@ 2020-07-08 16:59     ` Mikael Magnusson
  0 siblings, 0 replies; 4+ messages in thread
From: Mikael Magnusson @ 2020-07-08 16:59 UTC (permalink / raw)
  To: Roman Perepelitsa; +Cc: Zsh hackers list

On 7/8/20, Roman Perepelitsa <roman.perepelitsa@gmail.com> wrote:
> On Tue, Jul 7, 2020 at 10:28 PM Mikael Magnusson <mikachu@gmail.com> wrote:
>> It is possible that you changed the example in the mail without trying
>> what you changed; if you have the statements
>> alias x=:
>> x
>> in the file x, then sourcing it (non-compiled) will work
>
> Yes, that's exactly it. Given a script like this:
>
>   alias x=:
>   x
>
> Sourcing it without zcompile works (`x` on the second line is
> alias-expanded), but once the script is zcompiled an attempt to source
> it results in an error (`x` doesn't get expanded).
>
> I looked at the source code of zcompile and I understand why this
> happens. It makes perfect sense when zcompiling functions. It is,
> however, surprising when zcompiling scripts.

In general it is a good idea to not use aliases in scripts and/or
functions, they are mostly intended to save typing interactively.
Small details such as the above are another reason.

-- 
Mikael Magnusson

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, back to index

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-07 10:00 zcompile does not expand aliases defined in the compiled script Roman Perepelitsa
2020-07-07 20:28 ` Mikael Magnusson
2020-07-08  5:18   ` Roman Perepelitsa
2020-07-08 16:59     ` Mikael Magnusson

zsh-workers

Archives are clonable: git clone --mirror http://inbox.vuxu.org/zsh-workers

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://inbox.vuxu.org/vuxu.archive.zsh.workers


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git