Skip to content

Commit 23ee204

Browse files
committed
Fix skipping over invalid registry packages
This accidentally regressed in the previous caching PR for Cargo. Invalid lines of JSON in the registry are intended to be skipped over, but when skipping we forgot to update some indices which meant that all future versions would fail to parse as well!
1 parent afd240e commit 23ee204

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

src/cargo/sources/registry/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,7 @@ impl Summaries {
520520
// interpretation of each line here and older cargo will simply
521521
// ignore the new lines.
522522
let line = &contents[start..end];
523+
start = end + 1;
523524
let summary = match IndexSummary::parse(line, source_id) {
524525
Ok(summary) => summary,
525526
Err(e) => {
@@ -530,7 +531,6 @@ impl Summaries {
530531
let version = summary.summary.package_id().version().clone();
531532
cache.versions.push((version.clone(), line));
532533
ret.versions.insert(version, summary.into());
533-
start = end + 1;
534534
}
535535
if let Some(index_version) = index_version {
536536
cache_bytes = Some(cache.serialize(index_version));

tests/testsuite/registry.rs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1953,3 +1953,31 @@ fn rename_deps_and_features() {
19531953
p.cargo("build --features bar/foo01").run();
19541954
p.cargo("build --features bar/another").run();
19551955
}
1956+
1957+
#[test]
1958+
fn ignore_invalid_json_lines() {
1959+
Package::new("foo", "0.1.0").publish();
1960+
Package::new("foo", "0.1.1")
1961+
.invalid_json(true)
1962+
.publish();
1963+
Package::new("foo", "0.2.0").publish();
1964+
1965+
let p = project()
1966+
.file(
1967+
"Cargo.toml",
1968+
r#"
1969+
[project]
1970+
name = "a"
1971+
version = "0.5.0"
1972+
authors = []
1973+
1974+
[dependencies]
1975+
foo = '0.1.0'
1976+
foo02 = { version = '0.2.0', package = 'foo' }
1977+
"#,
1978+
)
1979+
.file("src/lib.rs", "")
1980+
.build();
1981+
1982+
p.cargo("build").run();
1983+
}

tests/testsuite/support/registry.rs

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ use cargo::sources::CRATES_IO_INDEX;
77
use cargo::util::Sha256;
88
use flate2::write::GzEncoder;
99
use flate2::Compression;
10-
use git2;
11-
use hex;
1210
use tar::{Builder, Header};
1311
use url::Url;
1412

@@ -137,6 +135,7 @@ pub struct Package {
137135
features: HashMap<String, Vec<String>>,
138136
local: bool,
139137
alternative: bool,
138+
invalid_json: bool,
140139
}
141140

142141
#[derive(Clone)]
@@ -232,6 +231,7 @@ impl Package {
232231
features: HashMap::new(),
233232
local: false,
234233
alternative: false,
234+
invalid_json: false,
235235
}
236236
}
237237

@@ -342,6 +342,13 @@ impl Package {
342342
self
343343
}
344344

345+
/// Causes the JSON line emitted in the index to be invalid, presumably
346+
/// causing Cargo to skip over this version.
347+
pub fn invalid_json(&mut self, invalid: bool) -> &mut Package {
348+
self.invalid_json = invalid;
349+
self
350+
}
351+
345352
/// Creates the package and place it in the registry.
346353
///
347354
/// This does not actually use Cargo's publishing system, but instead
@@ -384,8 +391,13 @@ impl Package {
384391
t!(t!(File::open(&self.archive_dst())).read_to_end(&mut c));
385392
cksum(&c)
386393
};
394+
let name = if self.invalid_json {
395+
serde_json::json!(1)
396+
} else {
397+
serde_json::json!(self.name)
398+
};
387399
let line = serde_json::json!({
388-
"name": self.name,
400+
"name": name,
389401
"vers": self.vers,
390402
"deps": deps,
391403
"cksum": cksum,

0 commit comments

Comments
 (0)