Skip to content

Commit 7620823

Browse files
committed
Omit checksum verification for local git dependencies
1 parent e691e18 commit 7620823

File tree

4 files changed

+96
-4
lines changed

4 files changed

+96
-4
lines changed

crates/cargo-test-support/src/registry.rs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,7 @@ pub struct Package {
385385
alternative: bool,
386386
invalid_json: bool,
387387
proc_macro: bool,
388+
generate_checksum: bool,
388389
links: Option<String>,
389390
rust_version: Option<String>,
390391
cargo_features: Vec<String>,
@@ -860,6 +861,7 @@ impl Package {
860861
alternative: false,
861862
invalid_json: false,
862863
proc_macro: false,
864+
generate_checksum: true,
863865
links: None,
864866
rust_version: None,
865867
cargo_features: Vec::new(),
@@ -877,6 +879,20 @@ impl Package {
877879
self
878880
}
879881

882+
/// Call with `true` to publish a git dependency in a "local registry".
883+
///
884+
/// The difference between this and [Package::local] is that this will
885+
/// skip checksum generation as git dependencies do not have checksums.
886+
///
887+
/// See `source-replacement.html#local-registry-sources` for more details
888+
/// on local registries. See `local_registry.rs` for the tests that use
889+
/// this.
890+
pub fn local_from_git(&mut self, local: bool) -> &mut Package {
891+
self.local = local;
892+
self.generate_checksum = !local;
893+
self
894+
}
895+
880896
/// Call with `true` to publish in an "alternative registry".
881897
///
882898
/// The name of the alternative registry is called "alternative".
@@ -1075,9 +1091,11 @@ impl Package {
10751091
})
10761092
})
10771093
.collect::<Vec<_>>();
1078-
let cksum = {
1094+
let cksum = if self.generate_checksum {
10791095
let c = t!(fs::read(&self.archive_dst()));
10801096
cksum(&c)
1097+
} else {
1098+
String::new()
10811099
};
10821100
let name = if self.invalid_json {
10831101
serde_json::json!(1)

src/cargo/core/resolver/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ pub fn resolve(
147147

148148
let mut cksums = HashMap::new();
149149
for (summary, _) in cx.activations.values() {
150-
let cksum = summary.checksum().map(|s| s.to_string());
150+
let cksum = summary
151+
.checksum()
152+
.map(|s| s.to_string())
153+
.filter(|s| !s.is_empty());
151154
cksums.insert(summary.package_id(), cksum);
152155
}
153156
let graph = cx.graph();

src/cargo/sources/registry/local.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'cfg> RegistryData for LocalRegistry<'cfg> {
129129
// We don't actually need to download anything per-se, we just need to
130130
// verify the checksum matches the .crate file itself.
131131
let actual = Sha256::new().update_file(&crate_file)?.finish_hex();
132-
if actual != checksum {
132+
if actual != checksum && !checksum.is_empty() {
133133
anyhow::bail!("failed to verify the checksum of `{}`", pkg)
134134
}
135135

tests/testsuite/local_registry.rs

Lines changed: 72 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
33
use cargo_test_support::paths::{self, CargoPathExt};
44
use cargo_test_support::registry::{registry_path, Package};
5-
use cargo_test_support::{basic_manifest, project, t};
5+
use cargo_test_support::{basic_manifest, git, path2url, project, t};
66
use std::fs;
77

88
fn setup() {
@@ -526,3 +526,74 @@ fn crates_io_registry_url_is_optional() {
526526
p.cargo("build").with_stderr("[FINISHED] [..]").run();
527527
p.cargo("test").run();
528528
}
529+
530+
#[cargo_test]
531+
fn git_dependencies_do_not_require_a_checksum() {
532+
let git_project = git::new("dep1", |project| {
533+
project
534+
.file("Cargo.toml", &basic_manifest("bar", "0.0.1"))
535+
.file("src/lib.rs", "pub fn bar() {}")
536+
});
537+
538+
let p = project()
539+
.file(
540+
"Cargo.toml",
541+
&format!(
542+
r#"
543+
[package]
544+
name = "foo"
545+
version = "0.0.1"
546+
authors = []
547+
548+
[dependencies.bar]
549+
git = '{}'
550+
"#,
551+
git_project.url()
552+
),
553+
)
554+
.file(
555+
"src/lib.rs",
556+
"extern crate bar; pub fn foo() { bar::bar(); }",
557+
)
558+
.build();
559+
560+
p.cargo("generate-lockfile").run();
561+
562+
let root = paths::root();
563+
t!(fs::create_dir(&root.join(".cargo")));
564+
565+
Package::new("bar", "0.0.1")
566+
.local_from_git(true)
567+
.file("src/lib.rs", "pub fn bar() {}")
568+
.publish();
569+
570+
t!(fs::write(
571+
root.join(".cargo/config"),
572+
format!(
573+
r#"
574+
[source.my-awesome-git-registry]
575+
git = '{}'
576+
replace-with = 'my-awesome-local-registry'
577+
578+
[source.my-awesome-local-registry]
579+
local-registry = 'registry'
580+
"#,
581+
git_project.url()
582+
)
583+
));
584+
p.cargo("clean").run();
585+
p.cargo("build")
586+
.with_stderr(&format!(
587+
"[UNPACKING] bar v0.0.1 ([..])\n\
588+
[COMPILING] bar v0.0.1 ({}#[..])\n\
589+
[COMPILING] foo v0.0.1 ([CWD])\n\
590+
[FINISHED] [..]\n",
591+
path2url(&git_project.root())
592+
))
593+
.run();
594+
p.cargo("build").with_stderr("[FINISHED] [..]").run();
595+
p.cargo("test").run();
596+
let lockfile = t!(fs::read_to_string(p.root().join("Cargo.lock")));
597+
// We only have one dependency, and it should not contain a checksum in the lockfile
598+
assert!(!lockfile.contains("checksum"));
599+
}

0 commit comments

Comments
 (0)