@@ -689,56 +689,58 @@ impl Config {
689
689
}
690
690
}
691
691
692
+ /// The purpose of this function is to aid in the transition to using
693
+ /// .toml extensions on Cargo's config files, which were historically not used.
694
+ /// Both 'config.toml' and 'credentials.toml' should be valid with or without extension.
695
+ /// When both exist, we want to prefer the one without an extension for
696
+ /// backwards compatibility, but warn the user appropriately.
697
+ fn get_file_path (
698
+ & self ,
699
+ dir : & Path ,
700
+ filename_without_extension : & str ,
701
+ warn : bool ,
702
+ ) -> CargoResult < Option < PathBuf > > {
703
+ let possible = dir. join ( filename_without_extension) ;
704
+ let possible_with_extension = dir. join ( format ! ( "{}.toml" , filename_without_extension) ) ;
705
+
706
+ if fs:: metadata ( & possible) . is_ok ( ) {
707
+ if warn && fs:: metadata ( & possible_with_extension) . is_ok ( ) {
708
+ self . shell ( ) . warn ( format ! (
709
+ "Both `{}` and `{}` exist. Using `{}`" ,
710
+ possible. display( ) ,
711
+ possible_with_extension. display( ) ,
712
+ possible. display( )
713
+ ) ) ?;
714
+ }
715
+
716
+ Ok ( Some ( possible) )
717
+ } else if fs:: metadata ( & possible_with_extension) . is_ok ( ) {
718
+ Ok ( Some ( possible_with_extension) )
719
+ } else {
720
+ Ok ( None )
721
+ }
722
+ }
723
+
692
724
fn walk_tree < F > ( & self , pwd : & Path , home : & Path , mut walk : F ) -> CargoResult < ( ) >
693
725
where
694
726
F : FnMut ( & Path ) -> CargoResult < ( ) > ,
695
727
{
696
728
let mut stash: HashSet < PathBuf > = HashSet :: new ( ) ;
697
729
698
730
for current in paths:: ancestors ( pwd) {
699
- let possible = current. join ( ".cargo" ) . join ( "config" ) ;
700
- let possible_with_extension = current. join ( ".cargo" ) . join ( "config.toml" ) ;
701
-
702
- // If both 'config' and 'config.toml' exist, we should use 'config'
703
- // for backward compatibility, but we should warn the user.
704
- if fs:: metadata ( & possible) . is_ok ( ) {
705
- if fs:: metadata ( & possible_with_extension) . is_ok ( ) {
706
- self . shell ( ) . warn ( format ! (
707
- "Both `{}` and `{}` exist. Using `{}`" ,
708
- possible. display( ) ,
709
- possible_with_extension. display( ) ,
710
- possible. display( )
711
- ) ) ?;
712
- }
713
-
714
- walk ( & possible) ?;
715
- stash. insert ( possible) ;
716
- } else if fs:: metadata ( & possible_with_extension) . is_ok ( ) {
717
- walk ( & possible_with_extension) ?;
718
- stash. insert ( possible) ;
731
+ if let Some ( path) = self . get_file_path ( & current. join ( ".cargo" ) , "config" , true ) ? {
732
+ walk ( & path) ?;
733
+ stash. insert ( path) ;
719
734
}
720
735
}
721
736
722
737
// Once we're done, also be sure to walk the home directory even if it's not
723
738
// in our history to be sure we pick up that standard location for
724
739
// information.
725
- let config = home. join ( "config" ) ;
726
- let config_with_extension = home. join ( "config.toml" ) ;
727
- if !stash. contains ( & config) && fs:: metadata ( & config) . is_ok ( ) {
728
- if fs:: metadata ( & config_with_extension) . is_ok ( ) {
729
- self . shell ( ) . warn ( format ! (
730
- "Both `{}` and `{}` exist. Using `{}`" ,
731
- config. display( ) ,
732
- config_with_extension. display( ) ,
733
- config. display( )
734
- ) ) ?;
740
+ if let Some ( path) = self . get_file_path ( home, "config" , true ) ? {
741
+ if !stash. contains ( & path) {
742
+ walk ( & path) ?;
735
743
}
736
-
737
- walk ( & config) ?;
738
- } else if !stash. contains ( & config_with_extension)
739
- && fs:: metadata ( & config_with_extension) . is_ok ( )
740
- {
741
- walk ( & config_with_extension) ?;
742
744
}
743
745
744
746
Ok ( ( ) )
@@ -781,10 +783,10 @@ impl Config {
781
783
/// present.
782
784
fn load_credentials ( & self , cfg : & mut ConfigValue ) -> CargoResult < ( ) > {
783
785
let home_path = self . home_path . clone ( ) . into_path_unlocked ( ) ;
784
- let credentials = home_path . join ( "credentials" ) ;
785
- if fs :: metadata ( & credentials) . is_err ( ) {
786
- return Ok ( ( ) ) ;
787
- }
786
+ let credentials = match self . get_file_path ( & home_path , "credentials" , true ) ? {
787
+ Some ( credentials) => credentials ,
788
+ None => return Ok ( ( ) ) ,
789
+ } ;
788
790
789
791
let mut contents = String :: new ( ) ;
790
792
let mut file = File :: open ( & credentials) ?;
@@ -1729,10 +1731,22 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
1729
1731
}
1730
1732
1731
1733
pub fn save_credentials ( cfg : & Config , token : String , registry : Option < String > ) -> CargoResult < ( ) > {
1734
+ // If 'credentials.toml' exists, we should write to that, otherwise
1735
+ // use the legacy 'credentials'. There's no need to print the warning
1736
+ // here, because it would already be printed at load time.
1737
+ let home_path = cfg. home_path . clone ( ) . into_path_unlocked ( ) ;
1738
+ let filename = match cfg. get_file_path ( & home_path, "credentials" , false ) ? {
1739
+ Some ( path) => match path. file_name ( ) {
1740
+ Some ( filename) => Path :: new ( filename) . to_owned ( ) ,
1741
+ None => Path :: new ( "credentials" ) . to_owned ( ) ,
1742
+ } ,
1743
+ None => Path :: new ( "credentials" ) . to_owned ( ) ,
1744
+ } ;
1745
+
1732
1746
let mut file = {
1733
1747
cfg. home_path . create_dir ( ) ?;
1734
1748
cfg. home_path
1735
- . open_rw ( Path :: new ( "credentials" ) , cfg, "credentials' config file" ) ?
1749
+ . open_rw ( filename , cfg, "credentials' config file" ) ?
1736
1750
} ;
1737
1751
1738
1752
let ( key, value) = {
0 commit comments