marlonrichert / zsh-hist

đź“ť Edit your Zsh history from the command line.
MIT License
148 stars 13 forks source link

Aliases are automatically expanded before the command is executed #5

Closed romkatv closed 3 years ago

romkatv commented 3 years ago

To reproduce:

% zsh -f
% git clone https://github.com/marlonrichert/zsh-hist.git
% source zsh-hist/zsh-hist.plugin.zsh
% alias x='echo hello'
% x

Expected: upon hitting Enter after entering the last command the following lines appear:

% x
hello

Actual: these lines appear instead:

%   echo hello
hello

Commit: bf89313fe17e13aa8449218f952ba594a21dcfde.

marlonrichert commented 3 years ago

Yeah, I know. It’s a side effect of using $functions to format the code. Personally, I don’t mind. Do you?

As a workaround, you can disable automatic code formatting with

add-zle-hook-widget -d line-finish .hist.format.hook
marlonrichert commented 3 years ago

Actually, I just realized it’s a side effect of using eval. I’ll see if I can work around it/not use eval for this.

marlonrichert commented 3 years ago

setopt localoptions NO_aliases should probably do the trick. I’ll have to test it.

romkatv commented 3 years ago

Personally, I don’t mind. Do you?

I've opened this issue because automatic alias expansion looks like an important enough effect to warrant a mention in the docs. It's an odd thing to bundle with formatting.

Note that alias expansion can change the effect of typed commands. For example:

% alias echo='echo foo'
% echo bar

Normally this will print "foo bar" but with automatic formatting it'll print "foo foo bar".

There are also more obscure cases where automatic formatting introduces side effects. For example, try executing the following command:

: }; touch foo; () { :

Without automatic formatting this command fails to parse and does nothing. With automatic formatting it touches file foo.

I don't mind any of it. Feel free to close this issue if or when you find it appropriate. Whatever you decide to do (including nothing at all) is fine with me.

marlonrichert commented 3 years ago

Thanks for the detailed motivation. After reading this, I definitely want to fix it. It threw me off guard, too, the first time the alias expansion happened to me.

There are also more obscure cases where automatic formatting introduces side effects. For example, try executing the following command:

: }; touch foo; () { :

Without automatic formatting this command fails to parse and does nothing. With automatic formatting it touches file foo.

Ah, that’s nasty. I’ll definitely have to see if I can fix that. It probably won’t come up often, but it looks like it can have seriously bad side effects.

marlonrichert commented 3 years ago

Problem solved.

romkatv commented 3 years ago

I took a look at the fix. There are still corner cases where enabling auto-formatting will change the effect of commands. Example:

% alias -g ';'=
% echo foo; echo bar

Without automatic formatting this prints "foo echo bar". With automatic formatting it prints "foo" and "bar".

marlonrichert commented 3 years ago

Is there a real use case for a global alias like that?

romkatv commented 3 years ago

Is there a real use case for a global alias like that?

I don't know. Does it matter?

It would be useful to provide the following guarantee:

When you put it this way, it would appear very surprising if this guarantee weren't provided. Yet there are many violations of this guarantee right now. I've posted one example above. Here's another:

% emulate zsh -o extended_glob
% echo '
do
'

With automatic formatting the last command is rewritten to this:

echo '; do
'

The usual disclaimer applies: I don't have a personal preference one way or another. It's fine with me if you do nothing or change the code in any way you like.

marlonrichert commented 3 years ago

Hm, that last example is definitely a bug. I think I'll have to change my approach on how to implement this.

marlonrichert commented 3 years ago

Is there a real use case for a global alias like that?

I don't know. Does it matter?

Yes, that matters. It's a cost-benefit analysis: If there's no way this is going to bother anyone, then there's no value in fixing it. It's not worth it to spend time on things that don't benefit anyone.

romkatv commented 3 years ago

It’s not something I want to argue about.

marlonrichert commented 3 years ago

OK, I pushed in another fix. This should take care of everything except the global ; alias thing.

romkatv commented 3 years ago

The guarantee I’ve mentioned above is still not provided even in the environment without aliases. Unfortunately, I’ve run out of goodwill to post more test cases. You’ll probably find them easily if you look at the code with the intent of seeing the bugs in it.

marlonrichert commented 3 years ago

@romkatv Patches welcome.

romkatv commented 3 years ago

I don’t know how to implement automatic parsing without changing the effect of entered commands. At the same time I think it crazy to use a plugin that can change the effect of typed commands in unspecified circumstances. I’ve opened this issue in case you find the information useful.