Skip to content

Commit

Permalink
If possible, spawn login shell in user's home directory
Browse files Browse the repository at this point in the history
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`).
  • Loading branch information
mrnugget committed Feb 23, 2024
1 parent 0ee3ffe commit 0ecf2d7
Showing 1 changed file with 22 additions and 1 deletion.
23 changes: 22 additions & 1 deletion crates/zed/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")?;
Expand Down

0 comments on commit 0ecf2d7

Please sign in to comment.