]> git.proxmox.com Git - cargo.git/commitdiff
Fix skipping over invalid registry packages
authorAlex Crichton <alex@alexcrichton.com>
Mon, 6 May 2019 18:35:17 +0000 (11:35 -0700)
committerAlex Crichton <alex@alexcrichton.com>
Mon, 6 May 2019 20:01:19 +0000 (13:01 -0700)
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!

src/cargo/sources/registry/index.rs
tests/testsuite/registry.rs
tests/testsuite/support/registry.rs

index f30ac76ea2a0aca43747b8857488a41de3b74c28..3fc7e4ddfb528581d893eaf411fc2099a7007907 100644 (file)
@@ -520,6 +520,7 @@ impl Summaries {
                 // interpretation of each line here and older cargo will simply
                 // ignore the new lines.
                 let line = &contents[start..end];
+                start = end + 1;
                 let summary = match IndexSummary::parse(line, source_id) {
                     Ok(summary) => summary,
                     Err(e) => {
@@ -530,7 +531,6 @@ impl Summaries {
                 let version = summary.summary.package_id().version().clone();
                 cache.versions.push((version.clone(), line));
                 ret.versions.insert(version, summary.into());
-                start = end + 1;
             }
             if let Some(index_version) = index_version {
                 cache_bytes = Some(cache.serialize(index_version));
index c99f081092df48385c0c665e4d654e9e55657422..3413dc923043e6f12e5f2d30da62abeb9f56b488 100644 (file)
@@ -1953,3 +1953,31 @@ fn rename_deps_and_features() {
     p.cargo("build --features bar/foo01").run();
     p.cargo("build --features bar/another").run();
 }
+
+#[test]
+fn ignore_invalid_json_lines() {
+    Package::new("foo", "0.1.0").publish();
+    Package::new("foo", "0.1.1")
+        .invalid_json(true)
+        .publish();
+    Package::new("foo", "0.2.0").publish();
+
+    let p = project()
+        .file(
+            "Cargo.toml",
+            r#"
+                [project]
+                name = "a"
+                version = "0.5.0"
+                authors = []
+
+                [dependencies]
+                foo = '0.1.0'
+                foo02 = { version = '0.2.0', package = 'foo' }
+            "#,
+        )
+        .file("src/lib.rs", "")
+        .build();
+
+    p.cargo("build").run();
+}
index db796301e81646ef00e069ccad42ee464dc81b87..023ece913208ce1b32f568f932f266403990bc55 100644 (file)
@@ -7,8 +7,6 @@ use cargo::sources::CRATES_IO_INDEX;
 use cargo::util::Sha256;
 use flate2::write::GzEncoder;
 use flate2::Compression;
-use git2;
-use hex;
 use tar::{Builder, Header};
 use url::Url;
 
@@ -137,6 +135,7 @@ pub struct Package {
     features: HashMap<String, Vec<String>>,
     local: bool,
     alternative: bool,
+    invalid_json: bool,
 }
 
 #[derive(Clone)]
@@ -232,6 +231,7 @@ impl Package {
             features: HashMap::new(),
             local: false,
             alternative: false,
+            invalid_json: false,
         }
     }
 
@@ -342,6 +342,13 @@ impl Package {
         self
     }
 
+    /// Causes the JSON line emitted in the index to be invalid, presumably
+    /// causing Cargo to skip over this version.
+    pub fn invalid_json(&mut self, invalid: bool) -> &mut Package {
+        self.invalid_json = invalid;
+        self
+    }
+
     /// Creates the package and place it in the registry.
     ///
     /// This does not actually use Cargo's publishing system, but instead
@@ -384,8 +391,13 @@ impl Package {
             t!(t!(File::open(&self.archive_dst())).read_to_end(&mut c));
             cksum(&c)
         };
+        let name = if self.invalid_json {
+            serde_json::json!(1)
+        } else {
+            serde_json::json!(self.name)
+        };
         let line = serde_json::json!({
-            "name": self.name,
+            "name": name,
             "vers": self.vers,
             "deps": deps,
             "cksum": cksum,