@@ -15,13 +15,14 @@ use cargo_metadata::{
1515 semver:: { Version , VersionReq } ,
1616} ;
1717use clap:: { Parser , crate_version} ;
18- use crates_index:: GitIndex ;
18+ use crates_index:: { Error , GitIndex } ;
1919use home:: cargo_home;
2020use std:: {
2121 cell:: RefCell ,
2222 collections:: { HashMap , HashSet } ,
2323 env:: args,
2424 ffi:: OsStr ,
25+ fmt:: Write ,
2526 fs:: File ,
2627 io:: { BufRead , IsTerminal } ,
2728 path:: { Path , PathBuf } ,
@@ -212,16 +213,36 @@ macro_rules! warn {
212213 } ;
213214}
214215
215- thread_local ! {
216- #[ allow( clippy:: unwrap_used) ]
217- static INDEX : LazyLock <GitIndex > = LazyLock :: new( || {
218- let _lock = lock_index( ) . unwrap( ) ;
219- let mut index = GitIndex :: new_cargo_default( ) . unwrap( ) ;
220- if let Err ( error) = index. update( ) {
221- warn!( "failed to update index: {}" , error) ;
216+ #[ allow( clippy:: unwrap_used) ]
217+ fn create_index ( ) -> GitIndex {
218+ let _lock = lock_index ( ) . unwrap ( ) ;
219+ #[ allow( clippy:: panic) ]
220+ let mut index = match GitIndex :: new_cargo_default ( ) {
221+ Ok ( index) => index,
222+ Err ( error) => {
223+ let mut msg = format ! ( "failed to create index: {error}" ) ;
224+ // smoelius: I once saw a recommendation to delete a corrupt crates.io index, but I
225+ // cannot remember where. Maybe the following?
226+ // https://github.com/rust-lang/cargo/issues/2403#issuecomment-186874266
227+ if let Error :: MissingHead { repo_path, .. } = & error {
228+ write ! (
229+ msg,
230+ "\n \n If the problem persists, consider deleting this directory: {}" ,
231+ repo_path. display( )
232+ )
233+ . unwrap ( ) ;
234+ }
235+ panic ! ( "{msg}" ) ;
222236 }
223- index
224- } ) ;
237+ } ;
238+ if let Err ( error) = index. update ( ) {
239+ warn ! ( "failed to update index: {}" , error) ;
240+ }
241+ index
242+ }
243+
244+ thread_local ! {
245+ static INDEX : LazyLock <GitIndex > = LazyLock :: new( create_index) ;
225246 static PROGRESS : RefCell <Option <progress:: Progress >> = const { RefCell :: new( None ) } ;
226247 // smoelius: The next four statics are "in-memory" caches.
227248 // smoelius: Note that repositories are (currently) stored in both an in-memory cache and an
0 commit comments