rustup icon indicating copy to clipboard operation
rustup copied to clipboard

installer produced an invalid .bash_profile

Open bronson opened this issue 2 years ago • 2 comments

Problem

Installing rustup via the bash command messed up my .bash_profile.

The installer truncated the line [ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env" to [ -f "$HOME/.cargo/env" ] && (which is now applying the conditional to some unrelated line) and then appended . "$HOME/.cargo/env" to the end of the file.

Here it is as a patch, my original file in red(-) and the rustup installer's modifications in green(+):

--- a/.bash_profile
+++ b/.bash_profile
@@ -1,7 +1,6 @@
 [[ $- == *i* ]] && [ -f ~/.bashrc ] && source ~/.bashrc
 
-[ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env"
-
+[ -f "$HOME/.cargo/env" ] && 
 # added by Anaconda3 5.0.0 installer
 export PATH="/Users/bronson/anaconda3/bin:$PATH"
 
@@ -9,3 +8,4 @@ export PATH="/Users/bronson/anaconda3/bin:$PATH"
 if [ -e "$HOME/.nix-profile/etc/profile.d/nix.sh" ]; then
   . "$HOME/.nix-profile/etc/profile.d/nix.sh"
 fi
+. "$HOME/.cargo/env"

Steps

  1. Add this line to the start of your .bash_profile: [ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env"
  2. Install rustup via bash: curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
  3. That line has been modified ... poorly. now the conditional is mistakenly applied to whatever the next line is, and the source line has been moved the end of the file.

Possible Solution(s)

I wish the installer wouldn't try to modify lines it doesn't understand.

Notes

No response

Rustup version

rustup 1.24.3 (ce5817a94 2021-05-31)
info: This is the version for the rustup toolchain manager, not the rustc compiler.
info: The currently active `rustc` version is `rustc 1.59.0 (9d1b2106e 2022-02-23)`

Installed toolchains

Default host: aarch64-apple-darwin
rustup home:  /Users/bronson/.rustup

stable-aarch64-apple-darwin (default)
rustc 1.59.0 (9d1b2106e 2022-02-23)

bronson avatar Mar 18 '22 04:03 bronson

Interestingly, it did much better with my .zshrc. The exact same line is in there:

[ -f "$HOME/.cargo/env" ] && source "$HOME/.cargo/env"

but the installer left it alone (rather than truncating it as it did above with .bash_profile).

This is the behavior I would expect.

bronson avatar Mar 18 '22 04:03 bronson

So the reason your .zshrc is left alone is because we edit .zshenv not .zshrc.

The reason that we mess up that line is because we're not checking that there's nothing before the command we "remove" when we are executing cleanup on the rc files.

We'd need to improve this function https://github.com/rust-lang/rustup/blob/master/src/cli/self_update/unix.rs#L55-L75 to fix this.

kinnison avatar May 20 '22 11:05 kinnison

@kinnison I noticed it defaults to .zshenv even on a Mac, which is known to have unexpected behavior that overrides the file. The zshenv file is more for putting personal configuration to run at the very start of shells (interactive and non-interactive).

At least on a Mac, the 'env' in zshenv misleading and ~/.zprofile is a better place to do PATH initialization.

rkulla avatar Nov 28 '22 23:11 rkulla