Skip to content

Commit d949021

Browse files
committed
Auto merge of #14530 - stormshield-guillaumed:vendor, r=weihanglo
fix(vendor): trust crate version only when coming from registries ### What does this PR try to resolve? Fixes #8181 Relates to #11897 and #14525 ### How should we test and review this PR? As mentioned in the contribution guide, I made a first commit adding a test that passes with the actual behaviour. Then, I made a second commit with a fix and modified the test with the new expected behaviour. ### Additional information The fix doesn't take into account switching from a git dependency to crates.io, which is not handled correctly on master either, and would probably require the vendoring to serialize the source ID to detect source changes. I specifically limited the trust of immutable version to crates.io, but it could be extended to other registries.
2 parents 643a025 + a53b81a commit d949021

File tree

2 files changed

+65
-2
lines changed

2 files changed

+65
-2
lines changed

src/cargo/ops/vendor.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,10 @@ fn sync(
207207
let dst = canonical_destination.join(&dst_name);
208208
to_remove.remove(&dst);
209209
let cksum = dst.join(".cargo-checksum.json");
210-
if dir_has_version_suffix && cksum.exists() {
211-
// Always re-copy directory without version suffix in case the version changed
210+
// Registries are the only immutable sources,
211+
// path and git dependencies' versions cannot be trusted to mean "no change"
212+
if dir_has_version_suffix && id.source_id().is_registry() && cksum.exists() {
213+
// Don't re-copy directory with version suffix in case it comes from a registry
212214
continue;
213215
}
214216

tests/testsuite/vendor.rs

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1568,6 +1568,67 @@ path = [..]
15681568
);
15691569
}
15701570

1571+
#[cargo_test]
1572+
fn git_update_rev() {
1573+
let (git_project, git_repo) = git::new_repo("git", |p| {
1574+
p.file("Cargo.toml", &basic_manifest("a", "0.1.0"))
1575+
.file("src/lib.rs", "")
1576+
});
1577+
let url = git_project.url();
1578+
let ref_1 = "initial";
1579+
let ref_2 = "update";
1580+
1581+
git::tag(&git_repo, ref_1);
1582+
1583+
git_project.change_file("src/lib.rs", "pub fn f() {}");
1584+
git::add(&git_repo);
1585+
git::commit(&git_repo);
1586+
git::tag(&git_repo, ref_2);
1587+
1588+
let p = project()
1589+
.file(
1590+
"Cargo.toml",
1591+
&format!(
1592+
r#"
1593+
[package]
1594+
name = "foo"
1595+
version = "0.1.0"
1596+
1597+
[dependencies]
1598+
a = {{ git = '{url}', rev = '{ref_1}' }}
1599+
"#
1600+
),
1601+
)
1602+
.file("src/lib.rs", "")
1603+
.build();
1604+
1605+
p.cargo("vendor --respect-source-config --versioned-dirs")
1606+
.run();
1607+
1608+
let lib = p.read_file("vendor/a-0.1.0/src/lib.rs");
1609+
assert_e2e().eq(lib, "");
1610+
1611+
p.change_file(
1612+
"Cargo.toml",
1613+
&format!(
1614+
r#"
1615+
[package]
1616+
name = "foo"
1617+
version = "0.1.0"
1618+
1619+
[dependencies]
1620+
a = {{ git = '{url}', rev = '{ref_2}' }}
1621+
"#
1622+
),
1623+
);
1624+
1625+
p.cargo("vendor --respect-source-config --versioned-dirs")
1626+
.run();
1627+
1628+
let lib = p.read_file("vendor/a-0.1.0/src/lib.rs");
1629+
assert_e2e().eq(lib, "pub fn f() {}");
1630+
}
1631+
15711632
#[cargo_test]
15721633
fn depend_on_vendor_dir_not_deleted() {
15731634
let p = project()

0 commit comments

Comments
 (0)