Skip to content

Commit

Permalink
feat: stabilize lockfile v4
Browse files Browse the repository at this point in the history
  • Loading branch information
weihanglo committed Oct 18, 2023
1 parent 0820fa1 commit 54a7132
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 88 deletions.
19 changes: 7 additions & 12 deletions src/cargo/core/resolver/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,18 +154,11 @@ impl EncodableResolve {
/// primary uses is to be used with `resolve_with_previous` to guide the
/// resolver to create a complete Resolve.
pub fn into_resolve(self, original: &str, ws: &Workspace<'_>) -> CargoResult<Resolve> {
let unstable_lockfile_version_allowed = ws.config().cli_unstable().next_lockfile_bump;
let path_deps = build_path_deps(ws)?;
let mut checksums = HashMap::new();

let mut version = match self.version {
Some(4) if ws.config().nightly_features_allowed => {
if unstable_lockfile_version_allowed {
ResolveVersion::V4
} else {
anyhow::bail!("lock file version 4 requires `-Znext-lockfile-bump`");
}
}
Some(4) => ResolveVersion::V4,
Some(3) => ResolveVersion::V3,
Some(n) => bail!(
"lock file version `{}` was found, but this version of Cargo \
Expand Down Expand Up @@ -797,9 +790,11 @@ fn encodable_source_id(id: SourceId, version: ResolveVersion) -> Option<Encodabl
if id.is_path() {
None
} else {
Some(match version {
ResolveVersion::V4 => EncodableSourceId::new(id),
_ => EncodableSourceId::without_url_encoded(id),
})
let id = if version >= ResolveVersion::V4 {
EncodableSourceId::new(id)
} else {
EncodableSourceId::without_url_encoded(id)
};
Some(id)
}
}
8 changes: 3 additions & 5 deletions src/cargo/core/resolver/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,11 +80,9 @@ pub enum ResolveVersion {
/// V3 by default staring in 1.53.
#[default]
V3,
/// Unstable. Will collect a certain amount of changes and then go.
///
/// Changes made:
///
/// * SourceId URL serialization is aware of URL encoding.
/// SourceId URL serialization is aware of URL encoding. For example,
/// `?branch=foo bar` is now encoded as `?branch=foo+bar` and can be decoded
/// back and forth correctly. Introduced in 2023 in version 1.75.
V4,
}

Expand Down
5 changes: 1 addition & 4 deletions src/cargo/core/source_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -618,10 +618,7 @@ impl fmt::Display for SourceId {
// Don't replace the URL display for git references,
// because those are kind of expected to be URLs.
write!(f, "{}", self.inner.url)?;
// TODO(-Znext-lockfile-bump): set it to true when stabilizing
// lockfile v4, because we want Source ID serialization to be
// consistent with lockfile.
if let Some(pretty) = reference.pretty_ref(false) {
if let Some(pretty) = reference.pretty_ref(true) {
write!(f, "?{}", pretty)?;
}

Expand Down
2 changes: 1 addition & 1 deletion src/cargo/ops/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn write_pkg_lockfile(ws: &Workspace<'_>, resolve: &mut Resolve) -> CargoRes
if resolve.version() < ResolveVersion::default() {
resolve.set_version(ResolveVersion::default());
out = serialize_resolve(resolve, orig.as_deref());
} else if resolve.version() > ResolveVersion::default()
} else if resolve.version() > ResolveVersion::V4
&& !ws.config().cli_unstable().next_lockfile_bump
{
// The next version hasn't yet stabilized.
Expand Down
6 changes: 1 addition & 5 deletions src/cargo/sources/git/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -167,11 +167,7 @@ fn ident_shallow(id: &SourceId, is_shallow: bool) -> String {
impl<'cfg> Debug for GitSource<'cfg> {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "git repo at {}", self.remote.url())?;

// TODO(-Znext-lockfile-bump): set it to true when stabilizing
// lockfile v4, because we want Source ID serialization to be
// consistent with lockfile.
match self.manifest_reference.pretty_ref(false) {
match self.manifest_reference.pretty_ref(true) {
Some(s) => write!(f, " ({})", s),
None => Ok(()),
}
Expand Down
6 changes: 1 addition & 5 deletions src/cargo/util/toml_mut/dependency.rs
Original file line number Diff line number Diff line change
Expand Up @@ -881,11 +881,7 @@ impl GitSource {
impl std::fmt::Display for GitSource {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let git_ref = self.git_ref();

// TODO(-Znext-lockfile-bump): set it to true when stabilizing
// lockfile v4, because we want Source ID serialization to be
// consistent with lockfile.
if let Some(pretty_ref) = git_ref.pretty_ref(false) {
if let Some(pretty_ref) = git_ref.pretty_ref(true) {
write!(f, "{}?{}", self.git, pretty_ref)
} else {
write!(f, "{}", self.git)
Expand Down
62 changes: 6 additions & 56 deletions tests/testsuite/lockfile_compat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -889,51 +889,6 @@ perhaps a crate was updated and forgotten to be re-vendored?
.run();
}

#[cargo_test]
fn v4_is_unstable() {
let p = project()
.file(
"Cargo.toml",
&format!(
r#"
[package]
name = "foo"
version = "0.0.1"
"#,
),
)
.file("src/lib.rs", "")
.file("Cargo.lock", "version = 4")
.build();

p.cargo("fetch")
.with_status(101)
.with_stderr(
"\
error: failed to parse lock file at: [CWD]/Cargo.lock
Caused by:
lock file version `4` was found, but this version of Cargo does not \
understand this lock file, perhaps Cargo needs to be updated?
",
)
.run();

// On nightly, let the user know about the `-Z` flag.
p.cargo("fetch")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
.with_status(101)
.with_stderr(
"\
error: failed to parse lock file at: [CWD]/Cargo.lock
Caused by:
lock file version 4 requires `-Znext-lockfile-bump`
",
)
.run();
}

#[cargo_test]
fn v4_cannot_be_created_from_scratch() {
let p = project()
Expand All @@ -950,9 +905,7 @@ fn v4_cannot_be_created_from_scratch() {
.file("src/lib.rs", "")
.build();

p.cargo("fetch -Znext-lockfile-bump")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
.run();
p.cargo("fetch").run();

let lockfile = r#"# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
Expand Down Expand Up @@ -993,6 +946,7 @@ fn v3_and_git_url_encoded(ref_kind: &str, f: impl FnOnce(&git2::Repository, &str
let head_id = repo.head().unwrap().target().unwrap();
// Ref name with special characters
let git_ref = "a-_+#$)";
let encoded_ref = "a-_%2B%23%24%29";
f(&repo, git_ref, head_id);

let lockfile = format!(
Expand Down Expand Up @@ -1036,7 +990,7 @@ dependencies = [
.with_stderr(format!(
"\
[UPDATING] git repository `{url}`
[CHECKING] dep1 v0.5.0 ({url}?{ref_kind}={git_ref}#[..])
[CHECKING] dep1 v0.5.0 ({url}?{ref_kind}={encoded_ref}#[..])
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [..]
"
Expand Down Expand Up @@ -1124,12 +1078,11 @@ dependencies = [
.file("Cargo.lock", "version = 4")
.build();

p.cargo("check -Znext-lockfile-bump")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
p.cargo("check")
.with_stderr(format!(
"\
[UPDATING] git repository `{url}`
[CHECKING] dep1 v0.5.0 ({url}?{ref_kind}={git_ref}#[..])
[CHECKING] dep1 v0.5.0 ({url}?{ref_kind}={encoded_ref}#[..])
[CHECKING] foo v0.0.1 ([CWD])
[FINISHED] dev [..]
"
Expand All @@ -1141,10 +1094,7 @@ dependencies = [

// Unlike v3_and_git_url_encoded, v4 encodes URL parameters so no git
// repository re-clone happen.
p.cargo("check -Znext-lockfile-bump")
.masquerade_as_nightly_cargo(&["-Znext-lockfile-bump"])
.with_stderr("[FINISHED] dev [..]")
.run();
p.cargo("check").with_stderr("[FINISHED] dev [..]").run();
}

#[cargo_test]
Expand Down

0 comments on commit 54a7132

Please sign in to comment.