@@ -2,7 +2,7 @@ use crate::core::{Edition, Shell, Workspace};
22use crate :: util:: errors:: CargoResult ;
33use crate :: util:: { existing_vcs_repo, FossilRepo , GitRepo , HgRepo , PijulRepo } ;
44use crate :: util:: { restricted_names, Config } ;
5- use anyhow:: Context as _;
5+ use anyhow:: { anyhow , Context as _} ;
66use cargo_util:: paths;
77use serde:: de;
88use serde:: Deserialize ;
@@ -595,9 +595,22 @@ impl IgnoreList {
595595 /// already exists. It reads the contents of the given `BufRead` and
596596 /// checks if the contents of the ignore list are already existing in the
597597 /// file.
598- fn format_existing < T : BufRead > ( & self , existing : T , vcs : VersionControl ) -> String {
599- // TODO: is unwrap safe?
600- let existing_items = existing. lines ( ) . collect :: < Result < Vec < _ > , _ > > ( ) . unwrap ( ) ;
598+ fn format_existing < T : BufRead > ( & self , existing : T , vcs : VersionControl ) -> CargoResult < String > {
599+ let mut existing_items = Vec :: new ( ) ;
600+ for ( i, item) in existing. lines ( ) . enumerate ( ) {
601+ match item {
602+ Ok ( s) => existing_items. push ( s) ,
603+ Err ( err) => match err. kind ( ) {
604+ ErrorKind :: InvalidData => {
605+ return Err ( anyhow ! (
606+ "Character at line {} is invalid. Cargo only supports UTF-8." ,
607+ i
608+ ) )
609+ }
610+ _ => return Err ( anyhow ! ( err) ) ,
611+ } ,
612+ }
613+ }
601614
602615 let ignore_items = match vcs {
603616 VersionControl :: Hg => & self . hg_ignore ,
@@ -631,7 +644,7 @@ impl IgnoreList {
631644 out. push ( '\n' ) ;
632645 }
633646
634- out
647+ Ok ( out)
635648 }
636649}
637650
@@ -660,7 +673,7 @@ fn write_ignore_file(base_path: &Path, list: &IgnoreList, vcs: VersionControl) -
660673 Some ( io_err) if io_err. kind ( ) == ErrorKind :: NotFound => list. format_new ( vcs) ,
661674 _ => return Err ( err) ,
662675 } ,
663- Ok ( file) => list. format_existing ( BufReader :: new ( file) , vcs) ,
676+ Ok ( file) => list. format_existing ( BufReader :: new ( file) , vcs) ? ,
664677 } ;
665678
666679 paths:: append ( & fp_ignore, ignore. as_bytes ( ) ) ?;
0 commit comments