Skip to content

feat(cargo-lock): support lockfile v4 - #1206

Merged
tarcieri merged 4 commits into
rustsec:mainfrom
weihanglo:resolve-version
Oct 9, 2024
Merged

feat(cargo-lock): support lockfile v4#1206
tarcieri merged 4 commits into
rustsec:mainfrom
weihanglo:resolve-version

Conversation

@weihanglo

Copy link
Copy Markdown
Contributor

In 1.78.0 Cargo introduce v4 lockfile:
rust-lang/cargo#12852

The v3-to-v4 change is minimal: encode URL params with URL encoding.
However, roundtrip test failed because v4 we can only have one
serialization implementation at a time.

I don't know how to proceed. Maybe we could

  • Make v4 serialization the default when it becomes the default in Cargo
  • Provide API for people to generate different versions of lockfiles.
  • Work with upstream Cargo with an in-tree pacakge for lock serialization. (This is on me?)

Opened this PR for discussion.

@weihanglo weihanglo changed the title test(cargo-lock): show we cannot to parse v4 feat(cargo-lock): show we cannot to parse v4 Jun 24, 2024
@weihanglo weihanglo changed the title feat(cargo-lock): show we cannot to parse v4 feat(cargo-lock): support lockfile v4 Jun 24, 2024
@weihanglo
weihanglo marked this pull request as draft June 24, 2024 18:17
[[package]]
name = "toml"
version = "0.8.14"
source = "git+https://github.com/weihanglo/toml.git?branch=a-_%2B%23%24%29z#9e406273177740fa85b86b78e6d5105e932edef0"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weihanglo

Copy link
Copy Markdown
Contributor Author

Hmm… missing some commits. Wait a sec.

[[package]]
name = "url"
version = "2.5.2"
source = "git+https://github.com/weihanglo/rust-url.git?tag=a-_%2B%23%24%29z#54346fa288e16b25b71c45149d7067c752b450e0"

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@weihanglo weihanglo Aug 19, 2024

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that in 3bcf68b, a bug was identified, unrelated to lockfile v4. See the commit message for more.

@tarcieri

Copy link
Copy Markdown
Member

Provide API for people to generate different versions of lockfiles.

@weihanglo it already supports this. Lockfile::version is consulted at serialization time and used to generate different versions of lockfiles.

@weihanglo

weihanglo commented Jun 24, 2024

Copy link
Copy Markdown
Contributor Author

Provide API for people to generate different versions of lockfiles.

@weihanglo it already supports this. Lockfile::version is consulted at serialization time and used to generate different versions of lockfiles.

Yeah but not for SourceId. We need to make SourceId aware of resolve version. On the Cargo side there is an EncodableSourceId doing that job. I don't think this cargo-lock crate is particularly interested in copying over the wrapper from there.

@tarcieri

Copy link
Copy Markdown
Member

A lot of the code has been copied from there. It would be good to follow the same approach they're using.

@weihanglo

Copy link
Copy Markdown
Contributor Author

Work with upstream Cargo with an in-tree pacakge for lock serialization. (This is on me?)

I am mostly thinking through this. The Cargo team has published a new crate https://crates.io/crates/cargo-util-schemas providing basic serialization schemas for common types. I wonder if we could get there so cargo-lock can be a thin tool built upon crates from Cargo.

@tarcieri

Copy link
Copy Markdown
Member

Sure, that looks potentially useful

@tarcieri

tarcieri commented Aug 1, 2024

Copy link
Copy Markdown
Member

FWIW I took a look at cargo-util-schemas. It sure would be nice to use several types defined there, like PackageIdSpec, SourceKind and GitReference, however for those in particular they'd be nice to have as part of the public API, and unfortunately the latter two are enums so they can't really be wrapped in newtypes.

Unfortunately, it doesn't seem ready for that as they just released semver breaking changes a week ago.

In the meantime it doesn't otherwise seem to have types relating specifically to Cargo.lock, only Cargo.toml.

@weihanglo

Copy link
Copy Markdown
Contributor Author

In the meantime it doesn't otherwise seem to have types relating specifically to Cargo.lock, only Cargo.toml.

That's true.

I think maybe we should just copy over from there one more time 😞.

While this was discovered that`Lockfile::load` cannot parse the
unambiguous source id in dependency list correctly,
this is not directly related to lockfile v4 so leave off for now.

Example lockfile

```toml
version = 3

[[package]]
name = "a"
version = "0.1.0"
dependencies = [
 "url 2.5.2 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
name = "b"
version = "0.1.0"
dependencies = [
 "url 2.5.2 (git+https://github.com/servo/rust-url.git)",
]

[[package]]
name = "url"
version = "2.5.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22784dbdf76fdde8af1aeda5622b546b422b6fc585325248a2bf9f5e41e94d6c"

[[package]]
name = "url"
version = "2.5.2"
source = "git+https://github.com/servo/rust-url.git#54346fa288e16b25b71c45149d7067c752b450e0"
```
In 1.78.0 Cargo introduce v4 lockfile:

rust-lang/cargo#12852

The v3-to-v4 change is minimal: encode URL params with URL encoding.
However, roundtrip test failed because v4 we can only have one
serialization implementation at a time.

I don't know how to proceed. Maybe we could

* Make v4 serialization the default when it becomes the default in Cargo
* Provide API for people to generate different versions of lockfiles.
* Work with upstream Cargo with an in-tree pacakge for lock serialization.
  (This is on me?)

Opened this PR for discussion.
@weihanglo
weihanglo marked this pull request as ready for review August 19, 2024 18:15
@weihanglo

Copy link
Copy Markdown
Contributor Author

@tarcieri this is ready for review. Take your time :)

Most of the code are copied or adapted from cargo the lib.

Note that I've identified a bug not related to lockfile format version: #1206 (comment)

@tarcieri

Copy link
Copy Markdown
Member

I'm busy for the rest of the week but will try to look this weekend

@71

71 commented Oct 9, 2024

Copy link
Copy Markdown

Note for anyone stumbling on this PR when getting "parse error: invalid Cargo.lock format version: `4`" (and who control the workspace whose Cargo.lock is causing problems): in the meantime you can add

rust-version = "1.81" # ensure it stays in lockfile v3

to the Cargo.toml to prevent Cargo from upgrading the lockfile format to v4 (source).

ia0 added a commit to ia0/data-encoding that referenced this pull request Oct 9, 2024
ia0 added a commit to ia0/data-encoding that referenced this pull request Oct 9, 2024
@tarcieri
tarcieri merged commit feeb220 into rustsec:main Oct 9, 2024
@tarcieri

tarcieri commented Oct 9, 2024

Copy link
Copy Markdown
Member

@weihanglo thank you!

@weihanglo
weihanglo deleted the resolve-version branch October 9, 2024 20:32
@tarcieri tarcieri mentioned this pull request Oct 15, 2024
Pi-Cla pushed a commit to Pi-Cla/obs-service-cargo that referenced this pull request Nov 1, 2024
uncomfyhalomacro pushed a commit to openSUSE-Rust/obs-service-cargo that referenced this pull request Nov 2, 2024
StressTestor pushed a commit to StressTestor/rustsec that referenced this pull request Jun 1, 2026
In 1.78.0 Cargo introduced v4 lockfiles: rust-lang/cargo#12852

The v3-to-v4 change is minimal: encode URL params with URL encoding.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants