-
Notifications
You must be signed in to change notification settings - Fork 893
/
self_update.rs
1290 lines (1116 loc) · 41.9 KB
/
self_update.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//! Self-installation and updating
//!
//! This is the installer at the heart of Rust. If it breaks
//! everything breaks. It is conceptually very simple, as rustup is
//! distributed as a single binary, and installation mostly requires
//! copying it into place. There are some tricky bits though, mostly
//! because of workarounds to self-delete an exe on Windows.
//!
//! During install (as `rustup-init`):
//!
//! * copy the self exe to $CARGO_HOME/bin
//! * hardlink rustc, etc to *that*
//! * update the PATH in a system-specific way
//! * run the equivalent of `rustup default stable`
//!
//! During upgrade (`rustup self upgrade`):
//!
//! * download rustup-init to $CARGO_HOME/bin/rustup-init
//! * run rustup-init with appropriate flags to indicate
//! this is a self-upgrade
//! * rustup-init copies bins and hardlinks into place. On windows
//! this happens *after* the upgrade command exits successfully.
//!
//! During uninstall (`rustup self uninstall`):
//!
//! * Delete `$RUSTUP_HOME`.
//! * Delete everything in `$CARGO_HOME`, including
//! the rustup binary and its hardlinks
//!
//! Deleting the running binary during uninstall is tricky
//! and racy on Windows.
use std::borrow::Cow;
use std::env::consts::EXE_SUFFIX;
use std::fs;
use std::io::Write;
use std::path::{Component, Path, PathBuf, MAIN_SEPARATOR};
use std::process::Command;
use std::str::FromStr;
use std::{env, fmt};
use anyhow::{anyhow, Context, Result};
use cfg_if::cfg_if;
use clap::builder::PossibleValue;
use clap::ValueEnum;
use itertools::Itertools;
use same_file::Handle;
use serde::{Deserialize, Serialize};
use tracing::{error, info, trace, warn};
use crate::{
cli::{
common::{self, ignorable_error, report_error, Confirm, PackageUpdate},
errors::*,
markdown::md,
},
config::Cfg,
dist::{self, PartialToolchainDesc, Profile, TargetTriple, ToolchainDesc},
errors::RustupError,
install::UpdateStatus,
process::{terminalsource, Process},
toolchain::{
DistributableToolchain, MaybeOfficialToolchainName, ResolvableToolchainName, Toolchain,
ToolchainName,
},
utils::{self, Notification},
DUP_TOOLS, TOOLS,
};
#[cfg(unix)]
mod shell;
#[cfg(unix)]
mod unix;
#[cfg(unix)]
use unix::{delete_rustup_and_cargo_home, do_add_to_path, do_remove_from_path};
#[cfg(unix)]
pub(crate) use unix::{run_update, self_replace};
#[cfg(windows)]
mod windows;
#[cfg(windows)]
pub use windows::complete_windows_uninstall;
#[cfg(windows)]
use windows::{delete_rustup_and_cargo_home, do_add_to_path, do_remove_from_path};
#[cfg(all(windows, feature = "test"))]
pub use windows::{get_path, RegistryGuard, RegistryValueId, USER_PATH};
#[cfg(windows)]
pub(crate) use windows::{run_update, self_replace};
pub(crate) struct InstallOpts<'a> {
pub default_host_triple: Option<String>,
pub default_toolchain: Option<MaybeOfficialToolchainName>,
pub profile: Profile,
pub no_modify_path: bool,
pub no_update_toolchain: bool,
pub components: &'a [&'a str],
pub targets: &'a [&'a str],
}
impl InstallOpts<'_> {
fn install(self, cfg: &mut Cfg<'_>) -> Result<Option<ToolchainDesc>> {
let Self {
default_host_triple,
default_toolchain,
profile,
no_modify_path: _no_modify_path,
no_update_toolchain,
components,
targets,
} = self;
cfg.set_profile(profile)?;
if let Some(default_host_triple) = &default_host_triple {
// Set host triple now as it will affect resolution of toolchain_str
info!("setting default host triple to {}", default_host_triple);
cfg.set_default_host_triple(default_host_triple.to_owned())?;
} else {
info!("default host triple is {}", cfg.get_default_host_triple()?);
}
let user_specified_something = default_toolchain.is_some()
|| !targets.is_empty()
|| !components.is_empty()
|| !no_update_toolchain;
// If the user specified they want no toolchain, we skip this, otherwise
// if they specify something directly, or we have no default, then we install
// a toolchain (updating if it's already present) and then if neither of
// those are true, we have a user who doesn't mind, and already has an
// install, so we leave their setup alone.
if matches!(default_toolchain, Some(MaybeOfficialToolchainName::None)) {
info!("skipping toolchain installation");
if !components.is_empty() {
warn!(
"ignoring requested component{}: {}",
if components.len() == 1 { "" } else { "s" },
components.join(", ")
);
}
if !targets.is_empty() {
warn!(
"ignoring requested target{}: {}",
if targets.len() == 1 { "" } else { "s" },
targets.join(", ")
);
}
writeln!(cfg.process.stdout().lock())?;
Ok(None)
} else if user_specified_something
|| (!no_update_toolchain && cfg.find_default()?.is_none())
{
Ok(match default_toolchain {
Some(s) => {
let toolchain_name = match s {
MaybeOfficialToolchainName::None => unreachable!(),
MaybeOfficialToolchainName::Some(n) => n,
};
Some(toolchain_name.resolve(&cfg.get_default_host_triple()?)?)
}
None => match cfg.get_default()? {
// Default is installable
Some(ToolchainName::Official(t)) => Some(t),
// Default is custom, presumably from a prior install. Do nothing.
Some(ToolchainName::Custom(_)) => None,
None => Some(
"stable"
.parse::<PartialToolchainDesc>()?
.resolve(&cfg.get_default_host_triple()?)?,
),
},
})
} else {
info!("updating existing rustup installation - leaving toolchains alone");
writeln!(cfg.process.stdout().lock())?;
Ok(None)
}
}
// Interactive editing of the install options
fn customize(&mut self, process: &Process) -> Result<()> {
writeln!(
process.stdout().lock(),
"I'm going to ask you the value of each of these installation options.\n\
You may simply press the Enter key to leave unchanged."
)?;
writeln!(process.stdout().lock())?;
self.default_host_triple = Some(common::question_str(
"Default host triple?",
&self
.default_host_triple
.take()
.unwrap_or_else(|| TargetTriple::from_host_or_build(process).to_string()),
process,
)?);
self.default_toolchain = Some(MaybeOfficialToolchainName::try_from(common::question_str(
"Default toolchain? (stable/beta/nightly/none)",
&self
.default_toolchain
.as_ref()
.map(ToString::to_string)
.unwrap_or("stable".into()),
process,
)?)?);
self.profile = <Profile as FromStr>::from_str(&common::question_str(
&format!(
"Profile (which tools and data to install)? ({})",
Profile::value_variants().iter().join("/"),
),
self.profile.as_str(),
process,
)?)?;
self.no_modify_path =
!common::question_bool("Modify PATH variable?", !self.no_modify_path, process)?;
Ok(())
}
fn validate(&self, process: &Process) -> Result<()> {
common::warn_if_host_is_emulated(process);
let host_triple = self
.default_host_triple
.as_ref()
.map(dist::TargetTriple::new)
.unwrap_or_else(|| TargetTriple::from_host_or_build(process));
let partial_channel = match &self.default_toolchain {
None | Some(MaybeOfficialToolchainName::None) => {
ResolvableToolchainName::try_from("stable")?
}
Some(MaybeOfficialToolchainName::Some(s)) => s.into(),
};
let resolved = partial_channel.resolve(&host_triple)?;
trace!("Successfully resolved installation toolchain as: {resolved}");
Ok(())
}
}
#[cfg(feature = "no-self-update")]
pub(crate) const NEVER_SELF_UPDATE: bool = true;
#[cfg(not(feature = "no-self-update"))]
pub(crate) const NEVER_SELF_UPDATE: bool = false;
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, PartialEq, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum SelfUpdateMode {
#[default]
Enable,
Disable,
CheckOnly,
}
impl SelfUpdateMode {
pub(crate) fn as_str(&self) -> &'static str {
match self {
Self::Enable => "enable",
Self::Disable => "disable",
Self::CheckOnly => "check-only",
}
}
}
impl ValueEnum for SelfUpdateMode {
fn value_variants<'a>() -> &'a [Self] {
&[Self::Enable, Self::Disable, Self::CheckOnly]
}
fn to_possible_value(&self) -> Option<PossibleValue> {
Some(PossibleValue::new(self.as_str()))
}
fn from_str(input: &str, _: bool) -> Result<Self, String> {
<Self as FromStr>::from_str(input).map_err(|e| e.to_string())
}
}
impl FromStr for SelfUpdateMode {
type Err = anyhow::Error;
fn from_str(mode: &str) -> Result<Self> {
match mode {
"enable" => Ok(Self::Enable),
"disable" => Ok(Self::Disable),
"check-only" => Ok(Self::CheckOnly),
_ => Err(anyhow!(format!(
"unknown self update mode: '{}'; valid modes are {}",
mode,
Self::value_variants().iter().join(", ")
))),
}
}
}
impl std::fmt::Display for SelfUpdateMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
// The big installation messages. These are macros because the first
// argument of format! needs to be a literal.
macro_rules! pre_install_msg_template {
($platform_msg:literal) => {
concat!(
r"
# Welcome to Rust!
This will download and install the official compiler for the Rust
programming language, and its package manager, Cargo.
Rustup metadata and toolchains will be installed into the Rustup
home directory, located at:
{rustup_home}
This can be modified with the RUSTUP_HOME environment variable.
The Cargo home directory is located at:
{cargo_home}
This can be modified with the CARGO_HOME environment variable.
The `cargo`, `rustc`, `rustup` and other commands will be added to
Cargo's bin directory, located at:
{cargo_home_bin}
",
$platform_msg,
r#"
You can uninstall at any time with `rustup self uninstall` and
these changes will be reverted.
"#
)
};
}
#[cfg(not(windows))]
macro_rules! pre_install_msg_unix {
() => {
pre_install_msg_template!(
"This path will then be added to your `PATH` environment variable by
modifying the profile file{plural} located at:
{rcfiles}"
)
};
}
#[cfg(windows)]
macro_rules! pre_install_msg_win {
() => {
pre_install_msg_template!(
"This path will then be added to your `PATH` environment variable by
modifying the `HKEY_CURRENT_USER/Environment/PATH` registry key."
)
};
}
macro_rules! pre_install_msg_no_modify_path {
() => {
pre_install_msg_template!(
"This path needs to be in your `PATH` environment variable,
but will not be added automatically."
)
};
}
#[cfg(not(windows))]
macro_rules! post_install_msg_unix_source_env {
() => {
r#"To configure your current shell, you need to source
the corresponding `env` file under {cargo_home}.
This is usually done by running one of the following (note the leading DOT):
. "{cargo_home}/env" # For sh/bash/zsh/ash/dash/pdksh
source "{cargo_home}/env.fish" # For fish
source "{cargo_home}/env.nu" # For nushell
"#
};
}
#[cfg(not(windows))]
macro_rules! post_install_msg_unix {
() => {
concat!(
r"# Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload your `PATH` environment variable to include
Cargo's bin directory ({cargo_home}/bin).
",
post_install_msg_unix_source_env!(),
)
};
}
#[cfg(windows)]
macro_rules! post_install_msg_win {
() => {
r"# Rust is installed now. Great!
To get started you may need to restart your current shell.
This would reload its `PATH` environment variable to include
Cargo's bin directory ({cargo_home}\\bin).
"
};
}
#[cfg(not(windows))]
macro_rules! post_install_msg_unix_no_modify_path {
() => {
concat!(
r"# Rust is installed now. Great!
To get started you need Cargo's bin directory ({cargo_home}/bin) in your `PATH`
environment variable. This has not been done automatically.
",
post_install_msg_unix_source_env!(),
)
};
}
#[cfg(windows)]
macro_rules! post_install_msg_win_no_modify_path {
() => {
r"# Rust is installed now. Great!
To get started you need Cargo's bin directory ({cargo_home}\\bin) in your `PATH`
environment variable. This has not been done automatically.
"
};
}
macro_rules! pre_uninstall_msg {
() => {
r"# Thanks for hacking in Rust!
This will uninstall all Rust toolchains and data, and remove
`{cargo_home}/bin` from your `PATH` environment variable.
"
};
}
static DEFAULT_UPDATE_ROOT: &str = "https://static.rust-lang.org/rustup";
fn update_root(process: &Process) -> String {
process
.var("RUSTUP_UPDATE_ROOT")
.inspect(|url| trace!("`RUSTUP_UPDATE_ROOT` has been set to `{url}`"))
.unwrap_or_else(|_| String::from(DEFAULT_UPDATE_ROOT))
}
/// `CARGO_HOME` suitable for display, possibly with $HOME
/// substituted for the directory prefix
fn canonical_cargo_home(process: &Process) -> Result<Cow<'static, str>> {
let path = process.cargo_home()?;
let default_cargo_home = process
.home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".cargo");
Ok(if default_cargo_home == path {
cfg_if! {
if #[cfg(windows)] {
r"%USERPROFILE%\.cargo".into()
} else {
"$HOME/.cargo".into()
}
}
} else {
path.to_string_lossy().into_owned().into()
})
}
/// Installing is a simple matter of copying the running binary to
/// `CARGO_HOME`/bin, hard-linking the various Rust tools to it,
/// and adding `CARGO_HOME`/bin to PATH.
pub(crate) async fn install(
current_dir: PathBuf,
no_prompt: bool,
quiet: bool,
mut opts: InstallOpts<'_>,
process: &Process,
) -> Result<utils::ExitCode> {
#[cfg_attr(not(unix), allow(unused_mut))]
let mut exit_code = utils::ExitCode(0);
opts.validate(process).map_err(|e| {
anyhow!(
"Pre-checks for host and toolchain failed: {e}\n\
If you are unsure of suitable values, the 'stable' toolchain is the default.\n\
Valid host triples look something like: {}",
TargetTriple::from_host_or_build(process)
)
})?;
if process
.var_os("RUSTUP_INIT_SKIP_EXISTENCE_CHECKS")
.is_none_or(|s| s != "yes")
{
check_existence_of_rustc_or_cargo_in_path(no_prompt, process)?;
check_existence_of_settings_file(process)?;
}
#[cfg(unix)]
{
exit_code &= unix::do_anti_sudo_check(no_prompt, process)?;
}
let mut term = process.stdout().terminal(process);
#[cfg(windows)]
windows::maybe_install_msvc(&mut term, no_prompt, quiet, &opts, process).await?;
if !no_prompt {
let msg = pre_install_msg(opts.no_modify_path, process)?;
md(&mut term, msg);
let mut customized_install = false;
loop {
md(&mut term, current_install_opts(&opts, process));
match common::confirm_advanced(customized_install, process)? {
Confirm::No => {
info!("aborting installation");
return Ok(utils::ExitCode(0));
}
Confirm::Yes => {
break;
}
Confirm::Advanced => {
customized_install = true;
opts.customize(process)?;
}
}
}
}
let no_modify_path = opts.no_modify_path;
if let Err(e) = maybe_install_rust(current_dir, quiet, opts, process).await {
report_error(&e, process);
// On windows, where installation happens in a console
// that may have opened just for this purpose, give
// the user an opportunity to see the error before the
// window closes.
#[cfg(windows)]
if !no_prompt {
windows::ensure_prompt(process)?;
}
return Ok(utils::ExitCode(1));
}
let cargo_home = canonical_cargo_home(process)?;
#[cfg(windows)]
let cargo_home = cargo_home.replace('\\', r"\\");
#[cfg(windows)]
let msg = if no_modify_path {
format!(
post_install_msg_win_no_modify_path!(),
cargo_home = cargo_home
)
} else {
format!(post_install_msg_win!(), cargo_home = cargo_home)
};
#[cfg(not(windows))]
let msg = if no_modify_path {
format!(
post_install_msg_unix_no_modify_path!(),
cargo_home = cargo_home
)
} else {
format!(post_install_msg_unix!(), cargo_home = cargo_home)
};
md(&mut term, msg);
#[cfg(windows)]
if !no_prompt {
// On windows, where installation happens in a console
// that may have opened just for this purpose, require
// the user to press a key to continue.
windows::ensure_prompt(process)?;
}
Ok(exit_code)
}
fn rustc_or_cargo_exists_in_path(process: &Process) -> Result<()> {
// Ignore rustc and cargo if present in $HOME/.cargo/bin or a few other directories
#[allow(clippy::ptr_arg)]
fn ignore_paths(path: &PathBuf) -> bool {
!path
.components()
.any(|c| c == Component::Normal(".cargo".as_ref()))
}
if let Some(paths) = process.var_os("PATH") {
let paths = env::split_paths(&paths).filter(ignore_paths);
for path in paths {
let rustc = path.join(format!("rustc{EXE_SUFFIX}"));
let cargo = path.join(format!("cargo{EXE_SUFFIX}"));
if rustc.exists() || cargo.exists() {
return Err(anyhow!("{}", path.to_str().unwrap().to_owned()));
}
}
}
Ok(())
}
fn check_existence_of_rustc_or_cargo_in_path(no_prompt: bool, process: &Process) -> Result<()> {
// Only the test runner should set this
let skip_check = process.var_os("RUSTUP_INIT_SKIP_PATH_CHECK");
// Skip this if the environment variable is set
if skip_check == Some("yes".into()) {
return Ok(());
}
if let Err(path) = rustc_or_cargo_exists_in_path(process) {
warn!("It looks like you have an existing installation of Rust at:");
warn!("{}", path);
warn!("It is recommended that rustup be the primary Rust installation.");
warn!("Otherwise you may have confusion unless you are careful with your PATH.");
warn!("If you are sure that you want both rustup and your already installed Rust");
warn!("then please reply `y' or `yes' or set RUSTUP_INIT_SKIP_PATH_CHECK to yes");
warn!("or pass `-y' to ignore all ignorable checks.");
ignorable_error("cannot install while Rust is installed", no_prompt, process)?;
}
Ok(())
}
fn check_existence_of_settings_file(process: &Process) -> Result<()> {
let rustup_dir = process.rustup_home()?;
let settings_file_path = rustup_dir.join("settings.toml");
if utils::path_exists(&settings_file_path) {
warn!("It looks like you have an existing rustup settings file at:");
warn!("{}", settings_file_path.display());
warn!("Rustup will install the default toolchain as specified in the settings file,");
warn!("instead of the one inferred from the default host triple.");
}
Ok(())
}
fn pre_install_msg(no_modify_path: bool, process: &Process) -> Result<String> {
let cargo_home = process.cargo_home()?;
let cargo_home_bin = cargo_home.join("bin");
let rustup_home = home::rustup_home()?;
if !no_modify_path {
// Brittle code warning: some duplication in unix::do_add_to_path
#[cfg(not(windows))]
{
let rcfiles = shell::get_available_shells(process)
.flat_map(|sh| sh.update_rcs(process).into_iter())
.map(|rc| format!(" {}", rc.display()))
.collect::<Vec<_>>();
let plural = if rcfiles.len() > 1 { "s" } else { "" };
let rcfiles = rcfiles.join("\n");
Ok(format!(
pre_install_msg_unix!(),
cargo_home = cargo_home.display(),
cargo_home_bin = cargo_home_bin.display(),
plural = plural,
rcfiles = rcfiles,
rustup_home = rustup_home.display(),
))
}
#[cfg(windows)]
Ok(format!(
pre_install_msg_win!(),
cargo_home = cargo_home.display(),
cargo_home_bin = cargo_home_bin.display(),
rustup_home = rustup_home.display(),
))
} else {
Ok(format!(
pre_install_msg_no_modify_path!(),
cargo_home = cargo_home.display(),
cargo_home_bin = cargo_home_bin.display(),
rustup_home = rustup_home.display(),
))
}
}
fn current_install_opts(opts: &InstallOpts<'_>, process: &Process) -> String {
format!(
r"Current installation options:
- ` `default host triple: `{}`
- ` `default toolchain: `{}`
- ` `profile: `{}`
- modify PATH variable: `{}`
",
opts.default_host_triple
.as_ref()
.map(TargetTriple::new)
.unwrap_or_else(|| TargetTriple::from_host_or_build(process)),
opts.default_toolchain
.as_ref()
.map(ToString::to_string)
.unwrap_or("stable (default)".into()),
opts.profile,
if !opts.no_modify_path { "yes" } else { "no" }
)
}
fn install_bins(process: &Process) -> Result<()> {
let bin_path = process.cargo_home()?.join("bin");
let this_exe_path = utils::current_exe()?;
let rustup_path = bin_path.join(format!("rustup{EXE_SUFFIX}"));
utils::ensure_dir_exists("bin", &bin_path, &|_: Notification<'_>| {})?;
// NB: Even on Linux we can't just copy the new binary over the (running)
// old binary; we must unlink it first.
if rustup_path.exists() {
utils::remove_file("rustup-bin", &rustup_path)?;
}
utils::copy_file(&this_exe_path, &rustup_path)?;
utils::make_executable(&rustup_path)?;
install_proxies(process)
}
pub(crate) fn install_proxies(process: &Process) -> Result<()> {
let bin_path = process.cargo_home()?.join("bin");
let rustup_path = bin_path.join(format!("rustup{EXE_SUFFIX}"));
let rustup = Handle::from_path(&rustup_path)?;
let mut tool_handles = Vec::new();
let mut link_afterwards = Vec::new();
// Try to symlink all the Rust exes to the rustup exe. Some systems,
// like Windows, do not always support symlinks, so we fallback to hard links.
//
// Note that this function may not be running in the context of a fresh
// self update but rather as part of a normal update to fill in missing
// proxies. In that case our process may actually have the `rustup.exe`
// file open, and on systems like Windows that means that you can't
// even remove other hard links to the same file. Basically if we have
// `rustup.exe` open and running and `cargo.exe` is a hard link to that
// file, we can't remove `cargo.exe`.
//
// To avoid unnecessary errors from being returned here we use the
// `same-file` crate and its `Handle` type to avoid clobbering hard links
// that are already valid. If a hard link already points to the
// `rustup.exe` file then we leave it alone and move to the next one.
//
// As yet one final caveat, when we're looking at handles for files we can't
// actually delete files (they'll say they're deleted but they won't
// actually be on Windows). As a result we manually drop all the
// `tool_handles` later on. This'll allow us, afterwards, to actually
// overwrite all the previous soft or hard links with new ones.
for tool in TOOLS {
let tool_path = bin_path.join(format!("{tool}{EXE_SUFFIX}"));
if let Ok(handle) = Handle::from_path(&tool_path) {
tool_handles.push(handle);
if rustup == *tool_handles.last().unwrap() {
continue;
}
}
link_afterwards.push(tool_path);
}
for tool in DUP_TOOLS {
let tool_path = bin_path.join(format!("{tool}{EXE_SUFFIX}"));
if let Ok(handle) = Handle::from_path(&tool_path) {
// Like above, don't clobber anything that's already linked to
// avoid extraneous errors from being returned.
if rustup == handle {
continue;
}
// If this file exists and is *not* equivalent to all other
// preexisting tools we found, then we're going to assume that it
// was preinstalled and actually pointing to a totally different
// binary. This is intended for cases where historically users
// ran `cargo install rustfmt` and so they had custom `rustfmt`
// and `cargo-fmt` executables lying around, but we as rustup have
// since started managing these tools.
//
// If the file is managed by rustup it should be equivalent to some
// previous file, and if it's not equivalent to anything then it's
// pretty likely that it needs to be dealt with manually.
if tool_handles.iter().all(|h| *h != handle) {
warn!("tool `{}` is already installed, remove it from `{}`, then run `rustup update` \
to have rustup manage this tool.",
tool, bin_path.display());
continue;
}
}
utils::symlink_or_hardlink_file(&rustup_path, &tool_path)?;
}
drop(tool_handles);
for path in link_afterwards {
utils::symlink_or_hardlink_file(&rustup_path, &path)?;
}
Ok(())
}
async fn maybe_install_rust(
current_dir: PathBuf,
quiet: bool,
opts: InstallOpts<'_>,
process: &Process,
) -> Result<()> {
install_bins(process)?;
#[cfg(unix)]
unix::do_write_env_files(process)?;
if !opts.no_modify_path {
do_add_to_path(process)?;
}
// If RUSTUP_HOME is not set, make sure it exists
if process.var_os("RUSTUP_HOME").is_none() {
let home = process
.home_dir()
.map(|p| p.join(".rustup"))
.ok_or_else(|| anyhow::anyhow!("could not find home dir to put .rustup in"))?;
fs::create_dir_all(home).context("unable to create ~/.rustup")?;
}
let mut cfg = common::set_globals(current_dir, quiet, process)?;
let (components, targets) = (opts.components, opts.targets);
let toolchain = opts.install(&mut cfg)?;
if let Some(ref desc) = toolchain {
let status = if Toolchain::exists(&cfg, &desc.into())? {
warn!("Updating existing toolchain, profile choice will be ignored");
// If we have a partial install we might not be able to read content here. We could:
// - fail and folk have to delete the partially present toolchain to recover
// - silently ignore it (and provide inconsistent metadata for reporting the install/update change)
// - delete the partial install and start over
// For now, we error.
let mut toolchain = DistributableToolchain::new(&cfg, desc.clone())?;
toolchain
.update(components, targets, cfg.get_profile()?)
.await?
} else {
DistributableToolchain::install(
&cfg,
desc,
components,
targets,
cfg.get_profile()?,
true,
)
.await?
.0
};
cfg.set_default(Some(&desc.into()))?;
writeln!(process.stdout().lock())?;
common::show_channel_update(&cfg, PackageUpdate::Toolchain(desc.clone()), Ok(status))?;
}
Ok(())
}
pub(crate) fn uninstall(no_prompt: bool, process: &Process) -> Result<utils::ExitCode> {
if NEVER_SELF_UPDATE {
error!("self-uninstall is disabled for this build of rustup");
error!("you should probably use your system package manager to uninstall rustup");
return Ok(utils::ExitCode(1));
}
let cargo_home = process.cargo_home()?;
if !cargo_home.join(format!("bin/rustup{EXE_SUFFIX}")).exists() {
return Err(CLIError::NotSelfInstalled { p: cargo_home }.into());
}
if !no_prompt {
writeln!(process.stdout().lock())?;
let msg = format!(
pre_uninstall_msg!(),
cargo_home = canonical_cargo_home(process)?
);
md(&mut process.stdout().terminal(process), msg);
if !common::confirm("\nContinue? (y/N)", false, process)? {
info!("aborting uninstallation");
return Ok(utils::ExitCode(0));
}
}
info!("removing rustup home");
// Delete RUSTUP_HOME
let rustup_dir = home::rustup_home()?;
if rustup_dir.exists() {
utils::remove_dir("rustup_home", &rustup_dir, &|_: Notification<'_>| {})?;
}
info!("removing cargo home");
// Remove CARGO_HOME/bin from PATH
do_remove_from_path(process)?;
// Delete everything in CARGO_HOME *except* the rustup bin
// First everything except the bin directory
let diriter = fs::read_dir(&cargo_home).map_err(|e| CLIError::ReadDirError {
p: cargo_home.clone(),
source: e,
})?;
for dirent in diriter {
let dirent = dirent.map_err(|e| CLIError::ReadDirError {
p: cargo_home.clone(),
source: e,
})?;
if dirent.file_name().to_str() != Some("bin") {
if dirent.path().is_dir() {
utils::remove_dir("cargo_home", &dirent.path(), &|_: Notification<'_>| {})?;
} else {
utils::remove_file("cargo_home", &dirent.path())?;
}
}
}
// Then everything in bin except rustup and tools. These can't be unlinked
// until this process exits (on windows).
let tools = TOOLS
.iter()
.chain(DUP_TOOLS.iter())
.map(|t| format!("{t}{EXE_SUFFIX}"));
let tools: Vec<_> = tools.chain(vec![format!("rustup{EXE_SUFFIX}")]).collect();
let bin_dir = cargo_home.join("bin");
let diriter = fs::read_dir(&bin_dir).map_err(|e| CLIError::ReadDirError {
p: bin_dir.clone(),
source: e,
})?;
for dirent in diriter {
let dirent = dirent.map_err(|e| CLIError::ReadDirError {
p: bin_dir.clone(),
source: e,
})?;
let name = dirent.file_name();
let file_is_tool = name.to_str().map(|n| tools.iter().any(|t| *t == n));
if file_is_tool == Some(false) {
if dirent.path().is_dir() {
utils::remove_dir("cargo_home", &dirent.path(), &|_: Notification<'_>| {})?;
} else {
utils::remove_file("cargo_home", &dirent.path())?;
}
}
}
info!("removing rustup binaries");
// Delete rustup. This is tricky because this is *probably*
// the running executable and on Windows can't be unlinked until
// the process exits.
delete_rustup_and_cargo_home(process)?;
info!("rustup is uninstalled");
Ok(utils::ExitCode(0))
}
/// Self update downloads rustup-init to `CARGO_HOME`/bin/rustup-init
/// and runs it.
///
/// It does a few things to accommodate self-delete problems on windows:
///
/// rustup-init is run in two stages, first with `--self-upgrade`,
/// which displays update messages and asks for confirmations, etc;
/// then with `--self-replace`, which replaces the rustup binary and
/// hardlinks. The last step is done without waiting for confirmation
/// on windows so that the running exe can be deleted.
///
/// Because it's again difficult for rustup-init to delete itself
/// (and on windows this process will not be running to do it),
/// rustup-init is stored in `CARGO_HOME`/bin, and then deleted next
/// time rustup runs.
pub(crate) async fn update(cfg: &Cfg<'_>) -> Result<utils::ExitCode> {
common::warn_if_host_is_emulated(cfg.process);
use common::SelfUpdatePermission::*;
let update_permitted = if NEVER_SELF_UPDATE {
HardFail
} else {