@@ -662,7 +662,7 @@ impl Config {
662
662
let mut cfg = CV :: Table ( HashMap :: new ( ) , PathBuf :: from ( "." ) ) ;
663
663
let home = self . home_path . clone ( ) . into_path_unlocked ( ) ;
664
664
665
- walk_tree ( path, & home, |path| {
665
+ self . walk_tree ( path, & home, |path| {
666
666
let mut contents = String :: new ( ) ;
667
667
let mut file = File :: open ( & path) ?;
668
668
file. read_to_string ( & mut contents)
@@ -689,6 +689,76 @@ 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
+ // We don't want to print a warning if the version
709
+ // without the extension is just a symlink to the version
710
+ // WITH an extension, which people may want to do to
711
+ // support multiple Cargo versions at once and not
712
+ // get a warning.
713
+ let skip_warning = if let Ok ( target_path) = fs:: read_link ( & possible) {
714
+ target_path == possible_with_extension
715
+ } else {
716
+ false
717
+ } ;
718
+
719
+ if !skip_warning {
720
+ self . shell ( ) . warn ( format ! (
721
+ "Both `{}` and `{}` exist. Using `{}`" ,
722
+ possible. display( ) ,
723
+ possible_with_extension. display( ) ,
724
+ possible. display( )
725
+ ) ) ?;
726
+ }
727
+ }
728
+
729
+ Ok ( Some ( possible) )
730
+ } else if fs:: metadata ( & possible_with_extension) . is_ok ( ) {
731
+ Ok ( Some ( possible_with_extension) )
732
+ } else {
733
+ Ok ( None )
734
+ }
735
+ }
736
+
737
+ fn walk_tree < F > ( & self , pwd : & Path , home : & Path , mut walk : F ) -> CargoResult < ( ) >
738
+ where
739
+ F : FnMut ( & Path ) -> CargoResult < ( ) > ,
740
+ {
741
+ let mut stash: HashSet < PathBuf > = HashSet :: new ( ) ;
742
+
743
+ for current in paths:: ancestors ( pwd) {
744
+ if let Some ( path) = self . get_file_path ( & current. join ( ".cargo" ) , "config" , true ) ? {
745
+ walk ( & path) ?;
746
+ stash. insert ( path) ;
747
+ }
748
+ }
749
+
750
+ // Once we're done, also be sure to walk the home directory even if it's not
751
+ // in our history to be sure we pick up that standard location for
752
+ // information.
753
+ if let Some ( path) = self . get_file_path ( home, "config" , true ) ? {
754
+ if !stash. contains ( & path) {
755
+ walk ( & path) ?;
756
+ }
757
+ }
758
+
759
+ Ok ( ( ) )
760
+ }
761
+
692
762
/// Gets the index for a registry.
693
763
pub fn get_registry_index ( & self , registry : & str ) -> CargoResult < Url > {
694
764
validate_package_name ( registry, "registry name" , "" ) ?;
@@ -726,10 +796,10 @@ impl Config {
726
796
/// present.
727
797
fn load_credentials ( & self , cfg : & mut ConfigValue ) -> CargoResult < ( ) > {
728
798
let home_path = self . home_path . clone ( ) . into_path_unlocked ( ) ;
729
- let credentials = home_path . join ( "credentials" ) ;
730
- if fs :: metadata ( & credentials) . is_err ( ) {
731
- return Ok ( ( ) ) ;
732
- }
799
+ let credentials = match self . get_file_path ( & home_path , "credentials" , true ) ? {
800
+ Some ( credentials) => credentials ,
801
+ None => return Ok ( ( ) ) ,
802
+ } ;
733
803
734
804
let mut contents = String :: new ( ) ;
735
805
let mut file = File :: open ( & credentials) ?;
@@ -1673,36 +1743,23 @@ pub fn homedir(cwd: &Path) -> Option<PathBuf> {
1673
1743
:: home:: cargo_home_with_cwd ( cwd) . ok ( )
1674
1744
}
1675
1745
1676
- fn walk_tree < F > ( pwd : & Path , home : & Path , mut walk : F ) -> CargoResult < ( ) >
1677
- where
1678
- F : FnMut ( & Path ) -> CargoResult < ( ) > ,
1679
- {
1680
- let mut stash: HashSet < PathBuf > = HashSet :: new ( ) ;
1681
-
1682
- for current in paths:: ancestors ( pwd) {
1683
- let possible = current. join ( ".cargo" ) . join ( "config" ) ;
1684
- if fs:: metadata ( & possible) . is_ok ( ) {
1685
- walk ( & possible) ?;
1686
- stash. insert ( possible) ;
1687
- }
1688
- }
1689
-
1690
- // Once we're done, also be sure to walk the home directory even if it's not
1691
- // in our history to be sure we pick up that standard location for
1692
- // information.
1693
- let config = home. join ( "config" ) ;
1694
- if !stash. contains ( & config) && fs:: metadata ( & config) . is_ok ( ) {
1695
- walk ( & config) ?;
1696
- }
1697
-
1698
- Ok ( ( ) )
1699
- }
1700
-
1701
1746
pub fn save_credentials ( cfg : & Config , token : String , registry : Option < String > ) -> CargoResult < ( ) > {
1747
+ // If 'credentials.toml' exists, we should write to that, otherwise
1748
+ // use the legacy 'credentials'. There's no need to print the warning
1749
+ // here, because it would already be printed at load time.
1750
+ let home_path = cfg. home_path . clone ( ) . into_path_unlocked ( ) ;
1751
+ let filename = match cfg. get_file_path ( & home_path, "credentials" , false ) ? {
1752
+ Some ( path) => match path. file_name ( ) {
1753
+ Some ( filename) => Path :: new ( filename) . to_owned ( ) ,
1754
+ None => Path :: new ( "credentials" ) . to_owned ( ) ,
1755
+ } ,
1756
+ None => Path :: new ( "credentials" ) . to_owned ( ) ,
1757
+ } ;
1758
+
1702
1759
let mut file = {
1703
1760
cfg. home_path . create_dir ( ) ?;
1704
1761
cfg. home_path
1705
- . open_rw ( Path :: new ( "credentials" ) , cfg, "credentials' config file" ) ?
1762
+ . open_rw ( filename , cfg, "credentials' config file" ) ?
1706
1763
} ;
1707
1764
1708
1765
let ( key, value) = {
0 commit comments