Skip to content

Commit 2ac2808

Browse files
authored
Merge pull request #2634 from kinnison/fix-2623
paths: Clean up use of 'source' rather than '.'
2 parents 968a8e8 + 18d1c26 commit 2ac2808

File tree

2 files changed

+32
-7
lines changed

2 files changed

+32
-7
lines changed

src/cli/self_update/unix.rs

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -145,22 +145,38 @@ pub fn self_replace() -> Result<utils::ExitCode> {
145145
Ok(utils::ExitCode(0))
146146
}
147147

148-
fn remove_legacy_paths() -> Result<()> {
149-
let export = format!("export PATH=\"{}/bin:$PATH\"\n", shell::cargo_home_str()?).into_bytes();
148+
fn remove_legacy_source_command(source_cmd: String) -> Result<()> {
149+
let cmd_bytes = source_cmd.into_bytes();
150150
for rc in shell::legacy_paths().filter(|rc| rc.is_file()) {
151151
let file = utils::read_file("rcfile", &rc)?;
152152
let file_bytes = file.into_bytes();
153153
// FIXME: This is whitespace sensitive where it should not be.
154154
if let Some(idx) = file_bytes
155-
.windows(export.len())
156-
.position(|w| w == export.as_slice())
155+
.windows(cmd_bytes.len())
156+
.position(|w| w == cmd_bytes.as_slice())
157157
{
158158
// Here we rewrite the file without the offending line.
159159
let mut new_bytes = file_bytes[..idx].to_vec();
160-
new_bytes.extend(&file_bytes[idx + export.len()..]);
160+
new_bytes.extend(&file_bytes[idx + cmd_bytes.len()..]);
161161
let new_file = String::from_utf8(new_bytes).unwrap();
162162
utils::write_file("rcfile", &rc, &new_file)?;
163163
}
164164
}
165165
Ok(())
166166
}
167+
168+
fn remove_legacy_paths() -> Result<()> {
169+
// Before the work to support more kinds of shells, which was released in
170+
// version 1.23.0 of rustup, we always inserted this line instead, which is
171+
// now considered legacy
172+
remove_legacy_source_command(format!(
173+
"export PATH=\"{}/bin:$PATH\"\n",
174+
shell::cargo_home_str()?
175+
))?;
176+
// Unfortunately in 1.23, we accidentally used `source` rather than `.`
177+
// which, while widely supported, isn't actually POSIX, so we also
178+
// clean that up here. This issue was filed as #2623.
179+
remove_legacy_source_command(format!("source \"{}/env\"\n", shell::cargo_home_str()?))?;
180+
181+
Ok(())
182+
}

tests/cli-paths.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,13 @@ export PATH="$HOME/apple/bin"
3232
format!(". \"{dir}/{sh}\"\n", dir = dir, sh = sh)
3333
}
3434

35+
// In 1.23 we used `source` instead of `.` by accident. This is not POSIX
36+
// so we want to ensure that if we put this into someone's dot files, then
37+
// with newer rustups we will revert that.
38+
fn non_posix_source(dir: impl Display, sh: impl Display) -> String {
39+
format!("source \"{dir}/{sh}\"\n", dir = dir, sh = sh)
40+
}
41+
3542
#[test]
3643
fn install_creates_necessary_scripts() {
3744
clitools::setup(Scenario::Empty, &|config| {
@@ -193,7 +200,8 @@ export PATH="$HOME/apple/bin"
193200
config.homedir.join(".zprofile"),
194201
zdotdir.path().join(".zprofile"),
195202
];
196-
let old_rc = FAKE_RC.to_owned() + DEFAULT_EXPORT;
203+
let old_rc =
204+
FAKE_RC.to_owned() + DEFAULT_EXPORT + &non_posix_source("$HOME/.cargo", POSIX_SH);
197205
for rc in rcs.iter().chain(zprofiles.iter()) {
198206
raw::write_file(&rc, &old_rc).unwrap();
199207
}
@@ -235,7 +243,8 @@ export PATH="$HOME/apple/bin"
235243
.map(|rc| config.homedir.join(rc))
236244
.collect();
237245
rcs.push(zdotdir.path().join(".zprofile"));
238-
let old_rc = FAKE_RC.to_owned() + DEFAULT_EXPORT;
246+
let old_rc =
247+
FAKE_RC.to_owned() + DEFAULT_EXPORT + &non_posix_source("$HOME/.cargo", POSIX_SH);
239248
for rc in &rcs {
240249
raw::write_file(&rc, &old_rc).unwrap();
241250
}

0 commit comments

Comments
 (0)