11use anyhow:: { anyhow, Context as _} ;
2- use cargo:: core:: shell:: Shell ;
32use cargo:: core:: { features, CliUnstable } ;
43use cargo:: { drop_print, drop_println, CargoResult } ;
54use clap:: builder:: UnknownArgumentValueParser ;
@@ -15,7 +14,11 @@ use crate::command_prelude::*;
1514use crate :: util:: is_rustup;
1615use cargo:: util:: style;
1716
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+
1922 let args = cli ( ) . try_get_matches ( ) ?;
2023
2124 // Update the process-level notion of cwd
@@ -38,14 +41,9 @@ pub fn main(lazy_gctx: &mut LazyContext) -> CliResult {
3841 . into ( ) ) ;
3942 }
4043 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 ( ) ?;
4245 }
4346
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-
4947 let ( expanded_args, global_args) = expand_aliases ( gctx, args, vec ! [ ] ) ?;
5048
5149 if expanded_args
@@ -645,43 +643,6 @@ See '<cyan,bold>cargo help</> <cyan><<command>></>' for more information on a sp
645643 . subcommands ( commands:: builtin ( ) )
646644}
647645
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-
685646#[ test]
686647fn verify_cli ( ) {
687648 cli ( ) . debug_assert ( ) ;
0 commit comments