Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ZSH pollutes environment on macOS even with $SHELL set to bash #7886

Closed
Learath2 opened this issue Apr 1, 2024 · 11 comments · Fixed by #7950
Closed

ZSH pollutes environment on macOS even with $SHELL set to bash #7886

Learath2 opened this issue Apr 1, 2024 · 11 comments · Fixed by #7950

Comments

@Learath2
Copy link

Learath2 commented Apr 1, 2024

With $SHELL set to /bin/bash ~/.zshenv still seems to get run, which is not really great. This is a result of the choice to execute the shell in a very roundabout way through zsh -c by default. Is this really necessary? Wouldn't /usr/bin/login -fp $user $SHELL be sufficient as we seem to counteract the -l immediately using an exec -a anyway.

If that doesn't work for some reason I can't foresee, perhaps zsh should be executed with -f/--no-rcs

System

OS: macOS
Version: alacritty 0.13.2 (bb8ea18)

@Learath2
Copy link
Author

Learath2 commented Apr 1, 2024

I took a quick look at the git blame and it seems the -l is needed to keep the current directory. I guess passing -f to zsh is the way to go.

@nixpulvis
Copy link
Contributor

This has nothing to do with alacritty, I recommend playing with your shell until it behaves as expected.

@Learath2
Copy link
Author

Learath2 commented Apr 1, 2024

This has nothing to do with alacritty, I recommend playing with your shell until it behaves as expected.

I respectfully disagree. This has something to do with alacritty as alacritty's choice to invoke the user login shell through zsh is what pollutes the environment. It's not expected behaviour that your terminal emulator runs an extra shell. Especially a shell that is not the login shell you picked with no recourse but to fork and compile your own.

@nixpulvis
Copy link
Contributor

That’s not alacritty doing that, I believe this was a choice macOS made recently. Feel free to change your shell invocation in the alacritty config to get around it though, or write your own wrapper shell as your default shell.

@Learath2
Copy link
Author

Learath2 commented Apr 1, 2024

I even linked the line where alacritty starts the login shell through zsh -c how is that a choice by macOS? The only reason alacritty uses zsh there seems to be the fact that it needs exec -a. zsh -fc would be the correct way to abuse zsh there without polluting the environment.

@nixpulvis
Copy link
Contributor

Apologies. I forgot all about that logic and was assuming that something changed when macOS defaulted to ZSH somewhat recently.

@nixpulvis
Copy link
Contributor

nixpulvis commented Apr 2, 2024

@Learath2 perhaps reading over #6426 will shed some light on this issue. I vaguely recall being a bit confused about this myself a while ago now that I’m thinking more about it. Though I assume you have at this point.

@nixpulvis
Copy link
Contributor

So now I'm curious and I'm trying running the suggested change here:

diff --git a/alacritty_terminal/src/tty/unix.rs b/alacritty_terminal/src/tty/unix.rs
index a4b07b74..d559a4e4 100644
--- a/alacritty_terminal/src/tty/unix.rs
+++ b/alacritty_terminal/src/tty/unix.rs
@@ -162,21 +162,12 @@ fn default_shell_command(shell: &str, _user: &str) -> Command {
 
 #[cfg(target_os = "macos")]
 fn default_shell_command(shell: &str, user: &str) -> Command {
-    let shell_name = shell.rsplit('/').next().unwrap();
-
     // On macOS, use the `login` command so the shell will appear as a tty session.
     let mut login_command = Command::new("/usr/bin/login");
 
-    // Exec the shell with argv[0] prepended by '-' so it becomes a login shell.
-    // `login` normally does this itself, but `-l` disables this.
-    let exec = format!("exec -a -{} {}", shell_name, shell);
-
     // -f: Bypasses authentication for the already-logged-in user.
-    // -l: Skips changing directory to $HOME and prepending '-' to argv[0].
     // -p: Preserves the environment.
-    //
-    // XXX: we use zsh here over sh due to `exec -a`.
-    login_command.args(["-flp", user, "/bin/zsh", "-c", &exec]);
+    login_command.args(["-fp", user, shell]);
     login_command
 }

Seems to work for me, so I'm not sure why things are as they are. Also, it's worth noting that having your shell.program set to something without the login call also results in a terminal without a registered session, which I suspect many people have. I don't honestly know what the consequences of not being a registered session are, but all my alacritty instances don't show up when I call who.

Hope this helps a bit.

@kchibisov
Copy link
Member

You can look into the git history to figure out why the changes are like that, but most likely because of some macOS bug.

PR that added that is also relatively new thing.

Also, none of that triggers if you run the command on your own iirc.

@nixpulvis
Copy link
Contributor

It just surprised me that all this special logic is skipped if you choose your own shell in the config. Shouldn’t it be applied to custom shells too?

@Learath2
Copy link
Author

Learath2 commented Apr 2, 2024

Careful. Removing the -l isn't correct. It'll switch directories to home. I'm running with -f added to the zsh invocation, and that has been working fine for me, albeit only tested for a week or so.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

3 participants