Skip to content

Commit fd30316

Browse files
authored
Handle crates without versions gracefully (#6795)
This is a situation that technically should not happen, but has managed to happen a couple of times in the past. This change will ensure that at least the crate index is not messed up and that the crates.io team gets a notification about the problem on Sentry.
1 parent edcc9cf commit fd30316

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

src/worker/git.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use anyhow::Context;
55
use chrono::Utc;
66
use crates_io_index::{Crate, Repository};
77
use diesel::prelude::*;
8+
use sentry::Level;
89
use std::fs::{self, File};
910
use std::io::{BufRead, BufReader, ErrorKind, Write};
1011
use std::process::Command;
@@ -93,6 +94,18 @@ pub fn get_index_data(name: &str, conn: &mut PgConnection) -> anyhow::Result<Opt
9394
.index_metadata(conn)
9495
.context("Failed to gather index metadata")?;
9596

97+
// This can sometimes happen when we delete versions upon owner request
98+
// but don't realize that the crate is now left with no versions at all.
99+
//
100+
// In this case we will delete the crate from the index and log a warning to
101+
// Sentry to clean this up in the database.
102+
if crates.is_empty() {
103+
let message = format!("Crate `{name}` has no versions left");
104+
sentry::capture_message(&message, Level::Warning);
105+
106+
return Ok(None);
107+
}
108+
96109
debug!("Serializing index data");
97110
let mut bytes = Vec::new();
98111
crates_io_index::write_crates(&crates, &mut bytes)

0 commit comments

Comments
 (0)