1
1
use anyhow:: { anyhow, Context as _} ;
2
- use cargo:: core:: shell:: Shell ;
3
2
use cargo:: core:: { features, CliUnstable } ;
4
3
use cargo:: { self , drop_print, drop_println, CargoResult , CliResult , Config } ;
5
4
use clap:: { builder:: UnknownArgumentValueParser , Arg , ArgMatches } ;
@@ -15,7 +14,11 @@ use crate::command_prelude::*;
15
14
use crate :: util:: is_rustup;
16
15
use cargo:: util:: style;
17
16
18
- pub fn main ( config : & mut LazyConfig ) -> CliResult {
17
+ pub fn main ( config : & mut Config ) -> CliResult {
18
+ // CAUTION: Be careful with using `config` until it is configured below.
19
+ // In general, try to avoid loading config values unless necessary (like
20
+ // the [alias] table).
21
+
19
22
let args = cli ( ) . try_get_matches ( ) ?;
20
23
21
24
// Update the process-level notion of cwd
@@ -38,14 +41,9 @@ pub fn main(config: &mut LazyConfig) -> CliResult {
38
41
. into ( ) ) ;
39
42
}
40
43
std:: env:: set_current_dir ( & new_cwd) . context ( "could not change to requested directory" ) ?;
41
- config. get_mut ( ) . reload_cwd ( ) ;
44
+ config. reload_cwd ( ) ? ;
42
45
}
43
46
44
- // CAUTION: Be careful with using `config` until it is configured below.
45
- // In general, try to avoid loading config values unless necessary (like
46
- // the [alias] table).
47
- let config = config. get_mut ( ) ;
48
-
49
47
let ( expanded_args, global_args) = expand_aliases ( config, args, vec ! [ ] ) ?;
50
48
51
49
if expanded_args
@@ -645,42 +643,6 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
645
643
. subcommands ( commands:: builtin ( ) )
646
644
}
647
645
648
- /// Delay loading [`Config`] until access.
649
- ///
650
- /// In the common path, the [`Config`] is dependent on CLI parsing and shouldn't be loaded until
651
- /// after that is done but some other paths (like fix or earlier errors) might need access to it,
652
- /// so this provides a way to share the instance and the implementation across these different
653
- /// accesses.
654
- pub struct LazyConfig {
655
- config : Option < Config > ,
656
- }
657
-
658
- impl LazyConfig {
659
- pub fn new ( ) -> Self {
660
- Self { config : None }
661
- }
662
-
663
- /// Get the config, loading it if needed
664
- ///
665
- /// On error, the process is terminated
666
- pub fn get ( & mut self ) -> & Config {
667
- self . get_mut ( )
668
- }
669
-
670
- /// Get the config, loading it if needed
671
- ///
672
- /// On error, the process is terminated
673
- pub fn get_mut ( & mut self ) -> & mut Config {
674
- self . config . get_or_insert_with ( || match Config :: default ( ) {
675
- Ok ( cfg) => cfg,
676
- Err ( e) => {
677
- let mut shell = Shell :: new ( ) ;
678
- cargo:: exit_with_error ( e. into ( ) , & mut shell)
679
- }
680
- } )
681
- }
682
- }
683
-
684
646
#[ test]
685
647
fn verify_cli ( ) {
686
648
cli ( ) . debug_assert ( ) ;
0 commit comments