Skip to content

Commit 0ecf2d7

Browse files
committed
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`).
1 parent 0ee3ffe commit 0ecf2d7

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

crates/zed/src/main.rs

+22-1
Original file line numberDiff line numberDiff line change
@@ -839,8 +839,29 @@ async fn load_login_shell_environment() -> Result<()> {
839839
let shell = env::var("SHELL").context(
840840
"SHELL environment variable is not assigned so we can't source login environment variables",
841841
)?;
842+
843+
// If possible, we want to `cd` in the user's `$HOME` to trigger programs
844+
// such as direnv, asdf, mise, ... to adjust the PATH. These tools often hook
845+
// into shell's `cd` command (and hooks) to manipulate env.
846+
// We do this so that we get the env a user would have when spawning a shell
847+
// in home directory.
848+
let shell_cmd_prefix = std::env::var_os("HOME")
849+
.and_then(|home| home.into_string().ok())
850+
.map(|home| format!("cd {home};"));
851+
852+
// The `exit 0` is the result of hours of debugging, trying to find out
853+
// why running this command here, without `exit 0`, would mess
854+
// up signal process for our process so that `ctrl-c` doesn't work
855+
// anymore.
856+
// We still don't know why `$SHELL -l -i -c '/usr/bin/env -0'` would
857+
// do that, but it does, and `exit 0` helps.
858+
let shell_cmd = format!(
859+
"{}echo {marker}; /usr/bin/env -0; exit 0;",
860+
shell_cmd_prefix.as_deref().unwrap_or("")
861+
);
862+
842863
let output = Command::new(&shell)
843-
.args(["-l", "-i", "-c", &format!("echo {marker}; /usr/bin/env -0")])
864+
.args(["-l", "-i", "-c", &shell_cmd])
844865
.output()
845866
.await
846867
.context("failed to spawn login shell to source login environment variables")?;

0 commit comments

Comments
 (0)