From 0ecf2d70bb80a67476d665c6113886759d7438cb Mon Sep 17 00:00:00 2001 From: Thorsten Ball Date: Fri, 23 Feb 2024 15:01:25 +0100 Subject: [PATCH] If possible, spawn login shell in user's home directory This is related to #8188 and should fix two things: 1. It spawns a login shell that `cd`s into a users home directory, which causes tools like `asdf`, `mise`, `direnv`, ... to fire. That means that if I don't have `gopls` installed, for example, but `go` (via `mise`) that Zed will then use my `go` when launched from `Zed.app`. 2. It appends `exit 0;` to the login shell command. It's the same mysterious fix that we had in #8188 for the login shell. We thought that this shell here was immune but turns out it's not when launched via `cli` (`cargo build && cargo run -p cli --bin cli -- -b target/debug/Zed`). --- crates/zed/src/main.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/crates/zed/src/main.rs b/crates/zed/src/main.rs index 25e0980f90f1b..e0a5d3eff8b78 100644 --- a/crates/zed/src/main.rs +++ b/crates/zed/src/main.rs @@ -839,8 +839,29 @@ async fn load_login_shell_environment() -> Result<()> { let shell = env::var("SHELL").context( "SHELL environment variable is not assigned so we can't source login environment variables", )?; + + // If possible, we want to `cd` in the user's `$HOME` to trigger programs + // such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook + // into shell's `cd` command (and hooks) to manipulate env. + // We do this so that we get the env a user would have when spawning a shell + // in home directory. + let shell_cmd_prefix = std::env::var_os("HOME") + .and_then(|home| home.into_string().ok()) + .map(|home| format!("cd {home};")); + + // The `exit 0` is the result of hours of debugging, trying to find out + // why running this command here, without `exit 0`, would mess + // up signal process for our process so that `ctrl-c` doesn't work + // anymore. + // We still don't know why `$SHELL -l -i -c '/usr/bin/env -0'` would + // do that, but it does, and `exit 0` helps. + let shell_cmd = format!( + "{}echo {marker}; /usr/bin/env -0; exit 0;", + shell_cmd_prefix.as_deref().unwrap_or("") + ); + let output = Command::new(&shell) - .args(["-l", "-i", "-c", &format!("echo {marker}; /usr/bin/env -0")]) + .args(["-l", "-i", "-c", &shell_cmd]) .output() .await .context("failed to spawn login shell to source login environment variables")?;