-
Notifications
You must be signed in to change notification settings - Fork 893
/
config.rs
1265 lines (1148 loc) · 44.2 KB
/
config.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
use std::fmt::{self, Debug, Display};
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::Arc;
use std::{env, io};
use anyhow::{anyhow, bail, Context, Result};
use serde::Deserialize;
use thiserror::Error as ThisError;
use tokio_stream::StreamExt;
use tracing::trace;
use crate::{
cli::{common, self_update::SelfUpdateMode},
dist::{
self, download::DownloadCfg, temp, PartialToolchainDesc, Profile, TargetTriple,
ToolchainDesc,
},
errors::RustupError,
fallback_settings::FallbackSettings,
install::UpdateStatus,
notifications::*,
process::Process,
settings::{MetadataVersion, Settings, SettingsFile},
toolchain::{
CustomToolchainName, DistributableToolchain, LocalToolchainName, PathBasedToolchainName,
ResolvableLocalToolchainName, ResolvableToolchainName, Toolchain, ToolchainName,
},
utils,
};
#[derive(Debug, ThisError)]
enum OverrideFileConfigError {
#[error("empty toolchain override file detected. Please remove it, or else specify the desired toolchain properties in the file")]
Empty,
#[error("missing toolchain properties in toolchain override file")]
Invalid,
#[error("error parsing override file")]
Parsing,
}
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
struct OverrideFile {
toolchain: ToolchainSection,
}
impl OverrideFile {
fn is_empty(&self) -> bool {
self.toolchain.is_empty()
}
}
#[derive(Debug, Default, Deserialize, PartialEq, Eq)]
struct ToolchainSection {
channel: Option<String>,
path: Option<PathBuf>,
components: Option<Vec<String>>,
targets: Option<Vec<String>>,
profile: Option<String>,
}
impl ToolchainSection {
fn is_empty(&self) -> bool {
self.channel.is_none()
&& self.components.is_none()
&& self.targets.is_none()
&& self.path.is_none()
}
}
impl<T: Into<String>> From<T> for OverrideFile {
fn from(channel: T) -> Self {
let override_ = channel.into();
if Path::new(&override_).is_absolute() {
Self {
toolchain: ToolchainSection {
path: Some(PathBuf::from(override_)),
..Default::default()
},
}
} else {
Self {
toolchain: ToolchainSection {
channel: Some(override_),
..Default::default()
},
}
}
}
}
// Represents the reason why the active toolchain is active.
#[derive(Debug)]
pub(crate) enum ActiveReason {
Default,
Environment,
CommandLine,
OverrideDB(PathBuf),
ToolchainFile(PathBuf),
}
impl Display for ActiveReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> std::result::Result<(), fmt::Error> {
match self {
Self::Default => write!(f, "it's the default toolchain"),
Self::Environment => write!(f, "overridden by environment variable RUSTUP_TOOLCHAIN"),
Self::CommandLine => write!(f, "overridden by +toolchain on the command line"),
Self::OverrideDB(path) => write!(f, "directory override for '{}'", path.display()),
Self::ToolchainFile(path) => write!(f, "overridden by '{}'", path.display()),
}
}
}
// Represents a toolchain override from a +toolchain command line option,
// RUSTUP_TOOLCHAIN environment variable, or rust-toolchain.toml file etc. Can
// include components and targets from a rust-toolchain.toml that should be
// downloaded and installed.
#[derive(Clone, Debug)]
enum OverrideCfg {
PathBased(PathBasedToolchainName),
Custom(CustomToolchainName),
Official {
toolchain: ToolchainDesc,
components: Vec<String>,
targets: Vec<String>,
profile: Option<Profile>,
},
}
impl OverrideCfg {
fn from_file(cfg: &Cfg<'_>, file: OverrideFile) -> Result<Self> {
let toolchain_name = match (file.toolchain.channel, file.toolchain.path) {
(Some(name), None) => {
ResolvableToolchainName::try_from(name)?.resolve(&cfg.get_default_host_triple()?)?
}
(None, Some(path)) => {
if file.toolchain.targets.is_some()
|| file.toolchain.components.is_some()
|| file.toolchain.profile.is_some()
{
bail!(
"toolchain options are ignored for path toolchain ({})",
path.display()
)
}
// We -do not- support relative paths, they permit trivial
// completely arbitrary code execution in a directory.
// Longer term we'll not support path based toolchains at
// all, because they also permit arbitrary code execution,
// though with more challenges to exploit.
return Ok(Self::PathBased(PathBasedToolchainName::try_from(
&path as &Path,
)?));
}
(Some(channel), Some(path)) => {
bail!(
"cannot specify both channel ({}) and path ({}) simultaneously",
channel,
path.display()
)
}
(None, None) => cfg
.get_default()?
.ok_or_else(|| no_toolchain_error(cfg.process))?,
};
Ok(match toolchain_name {
ToolchainName::Official(desc) => {
let components = file.toolchain.components.unwrap_or_default();
let targets = file.toolchain.targets.unwrap_or_default();
Self::Official {
toolchain: desc,
components,
targets,
profile: file
.toolchain
.profile
.as_deref()
.map(Profile::from_str)
.transpose()?,
}
}
ToolchainName::Custom(name) => {
if file.toolchain.targets.is_some()
|| file.toolchain.components.is_some()
|| file.toolchain.profile.is_some()
{
bail!(
"toolchain options are ignored for a custom toolchain ({})",
name
)
}
Self::Custom(name)
}
})
}
fn into_local_toolchain_name(self) -> LocalToolchainName {
match self {
Self::PathBased(path_based_name) => path_based_name.into(),
Self::Custom(custom_name) => custom_name.into(),
Self::Official { ref toolchain, .. } => toolchain.into(),
}
}
}
impl From<ToolchainName> for OverrideCfg {
fn from(value: ToolchainName) -> Self {
match value {
ToolchainName::Official(desc) => Self::Official {
toolchain: desc,
components: vec![],
targets: vec![],
profile: None,
},
ToolchainName::Custom(name) => Self::Custom(name),
}
}
}
impl From<LocalToolchainName> for OverrideCfg {
fn from(value: LocalToolchainName) -> Self {
match value {
LocalToolchainName::Named(name) => Self::from(name),
LocalToolchainName::Path(path_name) => Self::PathBased(path_name),
}
}
}
#[cfg(unix)]
pub(crate) const UNIX_FALLBACK_SETTINGS: &str = "/etc/rustup/settings.toml";
pub(crate) struct Cfg<'a> {
profile_override: Option<Profile>,
pub rustup_dir: PathBuf,
pub settings_file: SettingsFile,
pub fallback_settings: Option<FallbackSettings>,
pub toolchains_dir: PathBuf,
pub update_hash_dir: PathBuf,
pub download_dir: PathBuf,
pub tmp_cx: temp::Context,
pub toolchain_override: Option<ResolvableToolchainName>,
pub env_override: Option<LocalToolchainName>,
pub dist_root_url: String,
pub notify_handler: Arc<dyn Fn(Notification<'_>)>,
pub current_dir: PathBuf,
pub process: &'a Process,
}
impl<'a> Cfg<'a> {
pub(crate) fn from_env(
current_dir: PathBuf,
notify_handler: Arc<dyn Fn(Notification<'_>)>,
process: &'a Process,
) -> Result<Self> {
// Set up the rustup home directory
let rustup_dir = process.rustup_home()?;
utils::ensure_dir_exists("home", &rustup_dir, notify_handler.as_ref())?;
let settings_file = SettingsFile::new(rustup_dir.join("settings.toml"));
settings_file.with(|s| {
(notify_handler)(Notification::ReadMetadataVersion(s.version));
if s.version == MetadataVersion::default() {
Ok(())
} else {
Err(anyhow!(
"rustup's metadata is out of date. run `rustup self upgrade-data`"
))
}
})?;
// Centralised file for multi-user systems to provide admin/distributor set initial values.
#[cfg(unix)]
let fallback_settings = FallbackSettings::new(
// If present, use the RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS environment
// variable as settings path, or UNIX_FALLBACK_SETTINGS otherwise
match process.var("RUSTUP_OVERRIDE_UNIX_FALLBACK_SETTINGS") {
Ok(s) => PathBuf::from(s),
Err(_) => PathBuf::from(UNIX_FALLBACK_SETTINGS),
},
)?;
#[cfg(windows)]
let fallback_settings = None;
let toolchains_dir = rustup_dir.join("toolchains");
let update_hash_dir = rustup_dir.join("update-hashes");
let download_dir = rustup_dir.join("downloads");
// Figure out get_default_host_triple before Config is populated
let default_host_triple =
settings_file.with(|s| Ok(get_default_host_triple(s, process)))?;
// Environment override
let env_override = non_empty_env_var("RUSTUP_TOOLCHAIN", process)?
.map(ResolvableLocalToolchainName::try_from)
.transpose()?
.map(|t| t.resolve(&default_host_triple))
.transpose()?;
let dist_root_server = dist_root_server(process)?;
let notify_clone = notify_handler.clone();
let tmp_cx = temp::Context::new(
rustup_dir.join("tmp"),
dist_root_server.as_str(),
Box::new(move |n| (notify_clone)(n.into())),
);
let dist_root = dist_root_server + "/dist";
let cfg = Self {
profile_override: None,
rustup_dir,
settings_file,
fallback_settings,
toolchains_dir,
update_hash_dir,
download_dir,
tmp_cx,
notify_handler,
toolchain_override: None,
env_override,
dist_root_url: dist_root,
current_dir,
process,
};
// Run some basic checks against the constructed configuration
// For now, that means simply checking that 'stable' can resolve
// for the current configuration.
ResolvableToolchainName::try_from("stable")?.resolve(
&cfg.get_default_host_triple()
.context("Unable parse configuration")?,
)?;
Ok(cfg)
}
/// construct a download configuration
pub(crate) fn download_cfg(
&'a self,
notify_handler: &'a dyn Fn(crate::dist::Notification<'_>),
) -> DownloadCfg<'a> {
DownloadCfg {
dist_root: &self.dist_root_url,
tmp_cx: &self.tmp_cx,
download_dir: &self.download_dir,
notify_handler,
process: self.process,
}
}
pub(crate) fn set_profile_override(&mut self, profile: Profile) {
self.profile_override = Some(profile);
}
pub(crate) fn set_default(&self, toolchain: Option<&ToolchainName>) -> Result<()> {
self.settings_file.with_mut(|s| {
s.default_toolchain = toolchain.map(|t| t.to_string());
Ok(())
})?;
(self.notify_handler)(Notification::SetDefaultToolchain(toolchain));
Ok(())
}
pub(crate) fn set_profile(&mut self, profile: Profile) -> Result<()> {
self.profile_override = None;
self.settings_file.with_mut(|s| {
s.profile = Some(profile);
Ok(())
})?;
(self.notify_handler)(Notification::SetProfile(profile.as_str()));
Ok(())
}
pub(crate) fn set_auto_self_update(&mut self, mode: SelfUpdateMode) -> Result<()> {
self.settings_file.with_mut(|s| {
s.auto_self_update = Some(mode);
Ok(())
})?;
(self.notify_handler)(Notification::SetSelfUpdate(mode.as_str()));
Ok(())
}
pub(crate) fn set_toolchain_override(&mut self, toolchain_override: &ResolvableToolchainName) {
self.toolchain_override = Some(toolchain_override.to_owned());
}
// Returns a profile, if one exists in the settings file.
//
// Returns `Err` if the settings file could not be read or the profile is
// invalid. Returns `Ok(...)` if there is a valid profile, and `Ok(Profile::Default)`
// if there is no profile in the settings file. The last variant happens when
// a user upgrades from a version of Rustup without profiles to a version of
// Rustup with profiles.
pub(crate) fn get_profile(&self) -> Result<Profile> {
if let Some(p) = self.profile_override {
return Ok(p);
}
self.settings_file
.with(|s| Ok(s.profile.unwrap_or_default()))
}
pub(crate) fn get_self_update_mode(&self) -> Result<SelfUpdateMode> {
if self.process.var("CI").is_ok() && self.process.var("RUSTUP_CI").is_err() {
// If we're in CI (but not rustup's own CI, which wants to test this stuff!),
// disable automatic self updates.
return Ok(SelfUpdateMode::Disable);
}
self.settings_file.with(|s| {
Ok(match s.auto_self_update {
Some(mode) => mode,
None => SelfUpdateMode::Enable,
})
})
}
pub(crate) fn ensure_toolchains_dir(&self) -> Result<(), anyhow::Error> {
utils::ensure_dir_exists("toolchains", &self.toolchains_dir, &|n| {
(self.notify_handler)(n)
})?;
Ok(())
}
pub(crate) fn installed_paths<'b>(
&self,
desc: &ToolchainDesc,
path: &'b Path,
) -> anyhow::Result<Vec<InstalledPath<'b>>> {
Ok(vec![
InstalledPath::File {
name: "update hash",
path: self.get_hash_file(desc, false)?,
},
InstalledPath::Dir { path },
])
}
pub(crate) fn get_hash_file(
&self,
toolchain: &ToolchainDesc,
create_parent: bool,
) -> Result<PathBuf> {
if create_parent {
utils::ensure_dir_exists(
"update-hash",
&self.update_hash_dir,
self.notify_handler.as_ref(),
)?;
}
Ok(self.update_hash_dir.join(toolchain.to_string()))
}
#[tracing::instrument(level = "trace", skip_all)]
pub(crate) fn upgrade_data(&self) -> Result<()> {
let current_version = self.settings_file.with(|s| Ok(s.version))?;
if current_version == MetadataVersion::default() {
(self.notify_handler)(Notification::MetadataUpgradeNotNeeded(current_version));
return Ok(());
}
(self.notify_handler)(Notification::UpgradingMetadata(
current_version,
MetadataVersion::default(),
));
match current_version {
MetadataVersion::V2 => {
// The toolchain installation format changed. Just delete them all.
(self.notify_handler)(Notification::UpgradeRemovesToolchains);
let dirs = utils::read_dir("toolchains", &self.toolchains_dir)?;
for dir in dirs {
let dir = dir.context("IO Error reading toolchains")?;
utils::remove_dir("toolchain", &dir.path(), self.notify_handler.as_ref())?;
}
// Also delete the update hashes
let files = utils::read_dir("update hashes", &self.update_hash_dir)?;
for file in files {
let file = file.context("IO Error reading update hashes")?;
utils::remove_file("update hash", &file.path())?;
}
self.settings_file.with_mut(|s| {
s.version = MetadataVersion::default();
Ok(())
})
}
MetadataVersion::V12 => unreachable!(),
}
}
pub(crate) fn find_default(&self) -> Result<Option<Toolchain<'_>>> {
Ok(self
.get_default()?
.map(|n| Toolchain::new(self, (&n).into()))
.transpose()?)
}
pub(crate) fn toolchain_from_partial(
&self,
toolchain: Option<PartialToolchainDesc>,
) -> anyhow::Result<Toolchain<'_>> {
let toolchain = toolchain
.map(|desc| {
anyhow::Ok(LocalToolchainName::Named(ToolchainName::Official(
desc.resolve(&self.get_default_host_triple()?)?,
)))
})
.transpose()?;
self.local_toolchain(toolchain)
}
pub(crate) fn find_active_toolchain(
&self,
) -> Result<Option<(LocalToolchainName, ActiveReason)>> {
Ok(
if let Some((override_config, reason)) = self.find_override_config()? {
Some((override_config.into_local_toolchain_name(), reason))
} else {
self.get_default()?
.map(|x| (x.into(), ActiveReason::Default))
},
)
}
fn find_override_config(&self) -> Result<Option<(OverrideCfg, ActiveReason)>> {
let override_config: Option<(OverrideCfg, ActiveReason)> =
// First check +toolchain override from the command line
if let Some(ref name) = self.toolchain_override {
let override_config = name.resolve(&self.get_default_host_triple()?)?.into();
Some((override_config, ActiveReason::CommandLine))
}
// Then check the RUSTUP_TOOLCHAIN environment variable
else if let Some(ref name) = self.env_override {
// Because path based toolchain files exist, this has to support
// custom, distributable, and absolute path toolchains otherwise
// rustup's export of a RUSTUP_TOOLCHAIN when running a process will
// error when a nested rustup invocation occurs
Some((name.clone().into(), ActiveReason::Environment))
}
// Then walk up the directory tree from 'path' looking for either the
// directory in the override database, or a `rust-toolchain{.toml}` file,
// in that order.
else if let Some((override_cfg, active_reason)) = self.settings_file.with(|s| {
self.find_override_from_dir_walk(&self.current_dir, s)
})? {
Some((override_cfg, active_reason))
}
// Otherwise, there is no override.
else {
None
};
Ok(override_config)
}
fn find_override_from_dir_walk(
&self,
dir: &Path,
settings: &Settings,
) -> Result<Option<(OverrideCfg, ActiveReason)>> {
let notify = self.notify_handler.as_ref();
let mut dir = Some(dir);
while let Some(d) = dir {
// First check the override database
if let Some(name) = settings.dir_override(d, notify) {
let reason = ActiveReason::OverrideDB(d.to_owned());
// Note that `rustup override set` fully resolves it's input
// before writing to settings.toml, so resolving here may not
// be strictly necessary (could instead model as ToolchainName).
// However, settings.toml could conceivably be hand edited to
// have an unresolved name. I'm just preserving pre-existing
// behaviour by choosing ResolvableToolchainName here.
let toolchain_name = ResolvableToolchainName::try_from(name)?
.resolve(&get_default_host_triple(settings, self.process))?;
let override_cfg = toolchain_name.into();
return Ok(Some((override_cfg, reason)));
}
// Then look for 'rust-toolchain' or 'rust-toolchain.toml'
let path_rust_toolchain = d.join("rust-toolchain");
let path_rust_toolchain_toml = d.join("rust-toolchain.toml");
let (toolchain_file, contents, parse_mode) = match (
utils::read_file("toolchain file", &path_rust_toolchain),
utils::read_file("toolchain file", &path_rust_toolchain_toml),
) {
(contents, Err(_)) => {
// no `rust-toolchain.toml` exists
(path_rust_toolchain, contents, ParseMode::Both)
}
(Err(_), Ok(contents)) => {
// only `rust-toolchain.toml` exists
(path_rust_toolchain_toml, Ok(contents), ParseMode::OnlyToml)
}
(Ok(contents), Ok(_)) => {
// both `rust-toolchain` and `rust-toolchain.toml` exist
notify(Notification::DuplicateToolchainFile {
rust_toolchain: &path_rust_toolchain,
rust_toolchain_toml: &path_rust_toolchain_toml,
});
(path_rust_toolchain, Ok(contents), ParseMode::Both)
}
};
if let Ok(contents) = contents {
// XXX Should not return the unvalidated contents; but a new
// internal only safe struct
let override_file =
Cfg::parse_override_file(contents, parse_mode).with_context(|| {
RustupError::ParsingFile {
name: "override",
path: toolchain_file.clone(),
}
})?;
if let Some(toolchain_name_str) = &override_file.toolchain.channel {
let toolchain_name = ResolvableToolchainName::try_from(toolchain_name_str)
.map_err(|_| {
anyhow!(
"invalid toolchain name detected in override file '{}'",
toolchain_file.display()
)
})?;
let default_host_triple = get_default_host_triple(settings, self.process);
// Do not permit architecture/os selection in channels as
// these are host specific and toolchain files are portable.
if let ResolvableToolchainName::Official(ref name) = toolchain_name {
if name.has_triple() {
// Permit fully qualified names IFF the toolchain is installed. TODO(robertc): consider
// disabling this and backing out https://github.com/rust-lang/rustup/pull/2141 (but provide
// the base name in the error to help users)
let resolved_name = &ToolchainName::try_from(toolchain_name_str)?;
if !self.list_toolchains()?.iter().any(|s| s == resolved_name) {
return Err(anyhow!(format!(
"target triple in channel name '{name}'"
)));
}
}
}
// XXX: this awkwardness deals with settings file being locked already
let toolchain_name = toolchain_name.resolve(&default_host_triple)?;
match Toolchain::new(self, (&toolchain_name).into()) {
Err(RustupError::ToolchainNotInstalled { .. }) => {
if matches!(toolchain_name, ToolchainName::Custom(_)) {
bail!(
"custom toolchain specified in override file '{}' is not installed",
toolchain_file.display()
)
}
}
Ok(_) => {}
Err(e) => Err(e)?,
}
}
let reason = ActiveReason::ToolchainFile(toolchain_file);
let override_cfg = OverrideCfg::from_file(self, override_file)?;
return Ok(Some((override_cfg, reason)));
}
dir = d.parent();
}
Ok(None)
}
fn parse_override_file<S: AsRef<str>>(
contents: S,
parse_mode: ParseMode,
) -> Result<OverrideFile> {
let contents = contents.as_ref();
match (contents.lines().count(), parse_mode) {
(0, _) => Err(anyhow!(OverrideFileConfigError::Empty)),
(1, ParseMode::Both) => {
let channel = contents.trim();
if channel.is_empty() {
Err(anyhow!(OverrideFileConfigError::Empty))
} else {
Ok(channel.into())
}
}
_ => {
let override_file = toml::from_str::<OverrideFile>(contents)
.context(OverrideFileConfigError::Parsing)?;
if override_file.is_empty() {
Err(anyhow!(OverrideFileConfigError::Invalid))
} else {
Ok(override_file)
}
}
}
}
#[tracing::instrument(level = "trace")]
pub(crate) fn active_rustc_version(&mut self) -> Result<Option<String>> {
if let Some(t) = self.process.args().find(|x| x.starts_with('+')) {
trace!("Fetching rustc version from toolchain `{}`", t);
self.set_toolchain_override(&ResolvableToolchainName::try_from(&t[1..])?);
}
let Some((name, _)) = self.find_active_toolchain()? else {
return Ok(None);
};
Ok(Some(Toolchain::new(self, name)?.rustc_version()))
}
pub(crate) fn resolve_toolchain(
&self,
name: Option<ResolvableToolchainName>,
) -> Result<Toolchain<'_>> {
let toolchain = name
.map(|name| anyhow::Ok(name.resolve(&self.get_default_host_triple()?)?.into()))
.transpose()?;
self.local_toolchain(toolchain)
}
pub(crate) fn resolve_local_toolchain(
&self,
name: Option<ResolvableLocalToolchainName>,
) -> Result<Toolchain<'_>> {
let local = name
.map(|name| name.resolve(&self.get_default_host_triple()?))
.transpose()?;
self.local_toolchain(local)
}
fn local_toolchain(&self, name: Option<LocalToolchainName>) -> Result<Toolchain<'_>> {
let toolchain = match name {
Some(tc) => tc,
None => {
self.find_active_toolchain()?
.ok_or_else(|| no_toolchain_error(self.process))?
.0
}
};
Ok(Toolchain::new(self, toolchain)?)
}
#[tracing::instrument(level = "trace", skip_all)]
pub(crate) async fn find_or_install_active_toolchain(
&self,
force_non_host: bool,
verbose: bool,
) -> Result<(LocalToolchainName, ActiveReason)> {
if let Some((override_config, reason)) = self.find_override_config()? {
let toolchain = override_config.clone().into_local_toolchain_name();
if let OverrideCfg::Official {
toolchain,
components,
targets,
profile,
} = override_config
{
self.ensure_installed(
&toolchain,
components,
targets,
profile,
force_non_host,
verbose,
)
.await?;
} else {
Toolchain::with_reason(self, toolchain.clone(), &reason)?;
}
Ok((toolchain, reason))
} else if let Some(toolchain) = self.get_default()? {
let reason = ActiveReason::Default;
if let ToolchainName::Official(desc) = &toolchain {
self.ensure_installed(desc, vec![], vec![], None, force_non_host, verbose)
.await?;
} else {
Toolchain::with_reason(self, toolchain.clone().into(), &reason)?;
}
Ok((toolchain.into(), reason))
} else {
Err(no_toolchain_error(self.process))
}
}
// Returns a Toolchain matching the given ToolchainDesc, installing it and
// the given components and targets if they aren't already installed.
#[tracing::instrument(level = "trace", err(level = "trace"), skip_all)]
pub(crate) async fn ensure_installed(
&self,
toolchain: &ToolchainDesc,
components: Vec<String>,
targets: Vec<String>,
profile: Option<Profile>,
force_non_host: bool,
verbose: bool,
) -> Result<(UpdateStatus, Toolchain<'_>)> {
common::check_non_host_toolchain(
toolchain.to_string(),
&TargetTriple::from_host_or_build(self.process),
&toolchain.target,
force_non_host,
)?;
if verbose {
(self.notify_handler)(Notification::LookingForToolchain(toolchain));
}
let components: Vec<_> = components.iter().map(AsRef::as_ref).collect();
let targets: Vec<_> = targets.iter().map(AsRef::as_ref).collect();
let profile = match profile {
Some(profile) => profile,
None => self.get_profile()?,
};
let (status, toolchain) = match DistributableToolchain::new(self, toolchain.clone()) {
Err(RustupError::ToolchainNotInstalled { .. }) => {
DistributableToolchain::install(
self,
toolchain,
&components,
&targets,
profile,
false,
)
.await?
}
Ok(mut distributable) => {
if verbose {
(self.notify_handler)(Notification::UsingExistingToolchain(toolchain));
}
let status = if !distributable.components_exist(&components, &targets)? {
distributable.update(&components, &targets, profile).await?
} else {
UpdateStatus::Unchanged
};
(status, distributable)
}
Err(e) => return Err(e.into()),
};
Ok((status, toolchain.into()))
}
/// Get the configured default toolchain.
/// If none is configured, returns None
/// If a bad toolchain name is configured, errors.
pub(crate) fn get_default(&self) -> Result<Option<ToolchainName>> {
let user_opt = self.settings_file.with(|s| Ok(s.default_toolchain.clone()));
let toolchain_maybe_str = if let Some(fallback_settings) = &self.fallback_settings {
match user_opt {
Err(_) | Ok(None) => Ok(fallback_settings.default_toolchain.clone()),
o => o,
}
} else {
user_opt
}?;
toolchain_maybe_str
.map(ResolvableToolchainName::try_from)
.transpose()?
.map(|t| t.resolve(&self.get_default_host_triple()?))
.transpose()
}
/// List all the installed toolchains: that is paths in the toolchain dir
/// that are:
/// - not files
/// - named with a valid resolved toolchain name
/// Currently no notification of incorrect names or entry type is done.
#[tracing::instrument(level = "trace", skip_all)]
pub(crate) fn list_toolchains(&self) -> Result<Vec<ToolchainName>> {
if utils::is_directory(&self.toolchains_dir) {
let mut toolchains: Vec<_> = utils::read_dir("toolchains", &self.toolchains_dir)?
// TODO: this discards errors reading the directory, is that
// correct? could we get a short-read and report less toolchains
// than exist?
.filter_map(io::Result::ok)
.filter(|e| e.file_type().map(|f| !f.is_file()).unwrap_or(false))
.filter_map(|e| e.file_name().into_string().ok())
.filter_map(|n| ToolchainName::try_from(&n).ok())
.collect();
toolchains.sort();
Ok(toolchains)
} else {
Ok(Vec::new())
}
}
pub(crate) fn list_channels(&self) -> Result<Vec<(ToolchainDesc, DistributableToolchain<'_>)>> {
self.list_toolchains()?
.into_iter()
.filter_map(|t| {
if let ToolchainName::Official(desc) = t {
Some(desc)
} else {
None
}
})
.filter(ToolchainDesc::is_tracking)
.map(|n| {
DistributableToolchain::new(self, n.clone())
.map_err(Into::into)
.map(|t| (n.clone(), t))
})
.collect::<Result<Vec<_>>>()
}
/// Create an override for a toolchain
pub(crate) fn make_override(&self, path: &Path, toolchain: &ToolchainName) -> Result<()> {
self.settings_file.with_mut(|s| {
s.add_override(path, toolchain.to_string(), self.notify_handler.as_ref());
Ok(())
})
}
pub(crate) async fn update_all_channels(
&self,
force_update: bool,
) -> Result<Vec<(ToolchainDesc, Result<UpdateStatus>)>> {
let channels = self.list_channels()?;
let channels = channels.into_iter();
let profile = self.get_profile()?;
// Update toolchains and collect the results
let channels = tokio_stream::iter(channels).then(|(desc, mut distributable)| async move {
let st = distributable
.update_extra(&[], &[], profile, force_update, false)
.await;
if let Err(ref e) = st {
(self.notify_handler)(Notification::NonFatalError(e));
}
(desc, st)
});
Ok(channels.collect().await)
}
pub(crate) fn set_default_host_triple(&self, host_triple: String) -> Result<()> {
// Ensure that the provided host_triple is capable of resolving
// against the 'stable' toolchain. This provides early errors
// if the supplied triple is insufficient / bad.
dist::PartialToolchainDesc::from_str("stable")?
.resolve(&TargetTriple::new(host_triple.clone()))?;
self.settings_file.with_mut(|s| {
s.default_host_triple = Some(host_triple);
Ok(())
})
}
#[tracing::instrument(level = "trace", skip_all)]
pub(crate) fn get_default_host_triple(&self) -> Result<TargetTriple> {
self.settings_file
.with(|s| Ok(get_default_host_triple(s, self.process)))
}
/// The path on disk of any concrete toolchain
pub(crate) fn toolchain_path(&self, toolchain: &LocalToolchainName) -> PathBuf {
match toolchain {
LocalToolchainName::Named(name) => self.toolchains_dir.join(name.to_string()),
LocalToolchainName::Path(p) => p.to_path_buf(),
}
}
}
pub(crate) fn dist_root_server(process: &Process) -> Result<String> {
Ok(match non_empty_env_var("RUSTUP_DIST_SERVER", process)? {
Some(s) => {
trace!("`RUSTUP_DIST_SERVER` has been set to `{s}`");
s
}
None => {
// For backward compatibility
non_empty_env_var("RUSTUP_DIST_ROOT", process)?
.inspect(|url| trace!("`RUSTUP_DIST_ROOT` has been set to `{url}`"))
.as_ref()
.map(|root| root.trim_end_matches("/dist"))
.unwrap_or(dist::DEFAULT_DIST_SERVER)
.to_owned()
}
})
}
impl<'a> Debug for Cfg<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let Self {
profile_override,
rustup_dir,
settings_file,
fallback_settings,
toolchains_dir,
update_hash_dir,
download_dir,
tmp_cx,
toolchain_override,
env_override,
dist_root_url,
notify_handler: _,
current_dir,