Skip to content

Commit 3509215

Browse files
Fix rustup-init failure to read ZDOTDIR from zsh when SHELL is not zsh (#3584)
1 parent 631f03a commit 3509215

File tree

2 files changed

+40
-1
lines changed

2 files changed

+40
-1
lines changed

src/cli/self_update/shell.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ impl Zsh {
166166
}
167167
} else {
168168
match std::process::Command::new("zsh")
169-
.args(["-c", "'echo $ZDOTDIR'"])
169+
.args(["-c", "echo -n $ZDOTDIR"])
170170
.output()
171171
{
172172
Ok(io) if !io.stdout.is_empty() => Ok(PathBuf::from(OsStr::from_bytes(&io.stdout))),

tests/suite/cli_paths.rs

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,45 @@ export PATH="$HOME/apple/bin"
144144
});
145145
}
146146

147+
#[test]
148+
fn install_with_zdotdir_from_calling_zsh() {
149+
// This test requires that zsh is callable.
150+
if std::process::Command::new("zsh")
151+
.arg("-c")
152+
.arg("true")
153+
.status()
154+
.is_err()
155+
{
156+
return;
157+
}
158+
clitools::test(Scenario::Empty, &|config| {
159+
let zdotdir = tempfile::Builder::new()
160+
.prefix("zdotdir")
161+
.tempdir()
162+
.unwrap();
163+
let rc = zdotdir.path().join(".zshenv");
164+
raw::write_file(&rc, FAKE_RC).unwrap();
165+
166+
// If $SHELL doesn't include "zsh", Zsh::zdotdir() will call zsh to obtain $ZDOTDIR.
167+
// ZDOTDIR could be set directly in the environment, but having ~/.zshenv set
168+
// ZDOTDIR is a normal setup, and ensures that the value came from calling zsh.
169+
let home_zshenv = config.homedir.join(".zshenv");
170+
let export_zdotdir = format!(
171+
"export ZDOTDIR=\"{}\"\n",
172+
zdotdir.path().as_os_str().to_str().unwrap()
173+
);
174+
raw::write_file(&home_zshenv, &export_zdotdir).unwrap();
175+
176+
let mut cmd = clitools::cmd(config, "rustup-init", &INIT_NONE[1..]);
177+
cmd.env("SHELL", "/bin/sh");
178+
assert!(cmd.output().unwrap().status.success());
179+
180+
let new_rc = fs::read_to_string(&rc).unwrap();
181+
let expected = FAKE_RC.to_owned() + &source(config.cargodir.display(), POSIX_SH);
182+
assert_eq!(new_rc, expected);
183+
});
184+
}
185+
147186
#[test]
148187
fn install_adds_path_to_rc_just_once() {
149188
clitools::test(Scenario::Empty, &|config| {

0 commit comments

Comments
 (0)