1
1
use anyhow:: { anyhow, Context as _} ;
2
- use cargo:: core:: shell:: Shell ;
3
2
use cargo:: core:: { features, CliUnstable } ;
4
3
use cargo:: { drop_print, drop_println, CargoResult } ;
5
4
use clap:: builder:: UnknownArgumentValueParser ;
@@ -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 ( lazy_gctx : & mut LazyContext ) -> CliResult {
17
+ pub fn main ( gctx : & mut GlobalContext ) -> 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(lazy_gctx: &mut LazyContext) -> CliResult {
38
41
. into ( ) ) ;
39
42
}
40
43
std:: env:: set_current_dir ( & new_cwd) . context ( "could not change to requested directory" ) ?;
41
- lazy_gctx . get_mut ( ) . reload_cwd ( ) ?;
44
+ gctx . 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 gctx = lazy_gctx. get_mut ( ) ;
48
-
49
47
let ( expanded_args, global_args) = expand_aliases ( gctx, args, vec ! [ ] ) ?;
50
48
51
49
if expanded_args
@@ -645,43 +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 [`GlobalContext`] until access.
649
- ///
650
- /// In the common path, the [`GlobalContext`] 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 LazyContext {
655
- gctx : Option < GlobalContext > ,
656
- }
657
-
658
- impl LazyContext {
659
- pub fn new ( ) -> Self {
660
- Self { gctx : 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 ) -> & GlobalContext {
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 GlobalContext {
674
- self . gctx
675
- . get_or_insert_with ( || match GlobalContext :: default ( ) {
676
- Ok ( cfg) => cfg,
677
- Err ( e) => {
678
- let mut shell = Shell :: new ( ) ;
679
- cargo:: exit_with_error ( e. into ( ) , & mut shell)
680
- }
681
- } )
682
- }
683
- }
684
-
685
646
#[ test]
686
647
fn verify_cli ( ) {
687
648
cli ( ) . debug_assert ( ) ;
0 commit comments