Skip to content

Resolve direct minimal versions in Cargo-minimal.lock on dependency changes - #4330

Closed
nyonson wants to merge 2 commits into
rust-bitcoin:masterfrom
nyonson:build/direct-minimal-versions
Closed

nyonson wants to merge 2 commits into
rust-bitcoin:masterfrom
nyonson:build/direct-minimal-versions

Conversation

@nyonson

@nyonson nyonson commented Apr 11, 2025 •

Copy link
Copy Markdown
Contributor

Strategy to close #4313.

The first patch has a handful of necessary dependency upgrades in order to use the direct-minimal-versions flag.

  • Standardize the serde_json and serde_test dev dependencies. The direct-minimal-versions flag requires one version for all direct dependencies in a workspace.
  • Makes serde's minimal version explicit with 1.0.156. I originally tried to make 1.0.103 work, but at the very least requires quite a bit of lint config tweaks to build that code. I never got 1.0.103 to actually work.
  • Bumps arbitrary from 1.4.0 to 1.4.1, 1.4.0 does not work with the 1.63.0 MSRV due to core::error::Error usage.

The second patch has the tweak for generating Cargo-minimal.lock with direct dependency versions. In the linked issue, Kix suggests a CI check to make sure the Cargo-minimal.lock file isn't drifting. I don't think that would play nice with the direct-minimal-versions flag since transitive dependency updates would break CI. Another possibility is to just calculate Cargo-minimal.lock on the fly in CI, but I assume having a checked in version is still desired by the maintainers.

After running just update-lock-fileswith the new strategy, there are only upgrades in Cargo-minimal.lock. The upgrades are due to the direct-minimal-versions flag allowing transitive dependency versions to update like normal so that this workspace is not blocked from testing its minimal versions just because the minimal versions of a dependency are broken. No downgrades are listed because serde and arbitrary, the two drifters, required upgrades to actually build addressed in the first patch.

Calling out one potential pain-point to this strategy, as seen with the serde_json and serde_test dev dependencies in patch 1, direct-minimal-versions requires just one minimum constraint definition per-dependency in a workspace. So all the crates in rust-bitcoin need to move in lockstep for each dep. This is more strict than it has been, but I think if a crate needs a dramatically different version of a dep, it might just be a good sign it should be moved outside of the workspace.

Maximums?

Cargo-recent.lock could be resolved with a more aggressive cargo update, where everything goes to its "max" version (minor and patch updates) given the constraints. This would test a wider window of direct dependency versions, but I do not know if it is in the original spirit of what Cargo-recent.lock was meant to capture. Here are the changes if updated with cargo update.

  +---------------------+------------+----------+------------+
  | Package             | Recent     | Change   | Maximum    |
  +---------------------+------------+----------+------------+
  | ADDED/REMOVED       |            |          |            |
  +---------------------+------------+----------+------------+
  | byteorder           | 1.5.0      | --->     | N/A        |
  +---------------------+------------+----------+------------+
  | MINOR CHANGES       |            |          |            |
  +---------------------+------------+----------+------------+
  | cc                  | 1.1.22     | --->     | 1.2.19     |
  | zerocopy            | 0.7.35     | --->     | 0.8.24     |
  | zerocopy-derive     | 0.7.35     | --->     | 0.8.24     |
  +---------------------+------------+----------+------------+
  | PATCH CHANGES       |            |          |            |
  +---------------------+------------+----------+------------+
  | honggfuzz           | 0.5.56     | --->     | 0.5.57     |
  | itoa                | 1.0.11     | --->     | 1.0.15     |
  | libc                | 0.2.159    | --->     | 0.2.172    |
  | ppv-lite86          | 0.2.20     | --->     | 0.2.21     |
  | proc-macro2         | 1.0.86     | --->     | 1.0.94     |
  | quote               | 1.0.37     | --->     | 1.0.40     |
  | ryu                 | 1.0.18     | --->     | 1.0.20     |
  | semver              | 1.0.23     | --->     | 1.0.26     |
  | serde               | 1.0.210    | --->     | 1.0.219    |
  | serde_derive        | 1.0.210    | --->     | 1.0.219    |
  | serde_json          | 1.0.128    | --->     | 1.0.140    |
  | syn                 | 2.0.79     | --->     | 2.0.100    |
  | unicode-ident       | 1.0.13     | --->     | 1.0.18     |
  +---------------------+------------+----------+------------+

@github-actions github-actions Bot added C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate C-units PRs modifying the units crate test C-primitives labels Apr 11, 2025
@coveralls

coveralls commented Apr 11, 2025 •

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14503841131

Details

  • 0 of 0 changed or added relevant lines in 0 files are covered.
  • 1 unchanged line in 1 file lost coverage.
  • Overall coverage decreased (-0.004%) to 83.848%

Files with Coverage Reduction New Missed Lines %
bitcoin/src/psbt/mod.rs 1 85.84%
Totals Coverage Status
Change from base Build 14500624498: -0.004%
Covered Lines: 22416
Relevant Lines: 26734

💛 - Coveralls

@apoelstra

Copy link
Copy Markdown
Member

Regarding serde, we should just set it (in the lockfiles) to the minimal version that works at all. That might be 1.0.156.

@nyonson

nyonson commented Apr 11, 2025 •

Copy link
Copy Markdown
Contributor Author

Regarding serde, we should just set it (in the lockfiles) to the minimal version that works at all. That might be 1.0.156.

Are you proposing to use some cargo update --precises to lock in 1.0.156? That keeps the constraint more flexible, but a bummer that the minimum version wouldn't actually be tested.

From what I can tell, the failures for 1.0.103 are just linting based. It looks like the modern versions of the rust compiler are a little more strict and don't like the relatively old code (5 years) in this serde version. I can "fix" one of the issues by adding cfg(feature, values("cargo-clippy")) to the lints.rust.unexpected_cfgs setting of a few crates. I can quite the other issue by tagging a few structs with #[allow(non_local_definitions)]. I am not sure how gross both of these are, but would allow keeping and testing the 1.0.103 min version.

A third more drastic option given these are just lints is to bump the minimum version to something like 1.0.156 since serde does fix this stuff up. Figured that was rocking the boat too much though.

@apoelstra

Copy link
Copy Markdown
Member

Are you proposing to use some cargo update --precises to lock in 1.0.156? That keeps the constraint more flexible, but a bummer that the minimum version wouldn't actually be tested.

I'm saying we should update all our .toml and .lock files to use 1.0.156 as the minimum. I'm not sure what you mean by "the minimum version wouldn't be tested". 1.0.156 is the minimum version. Versions before that don't compile.

A third more drastic option given these are just lints is to bump the minimum version to something like 1.0.156 since serde does fix this stuff up. Figured that was rocking the boat too much though.

I don't think this is so drastic. serde is really not a first-class citizen here and we shouldn't bend over backward to try to get their broken code to work.

@nyonson

nyonson commented Apr 14, 2025

Copy link
Copy Markdown
Contributor Author

I'm saying we should update all our .toml and .lock files to use 1.0.156 as the minimum. I'm not sure what you mean by "the minimum version wouldn't be tested". 1.0.156 is the minimum version. Versions before that don't compile.

Ah, I misinterpreted as only change the lockfile. Thanks for the clarification.

I don't think this is so drastic. serde is really not a first-class citizen here and we shouldn't bend over backward to try to get their broken code to work.

And yea, related, been trying to make 1.0.103 happy with no luck. Being able to just roll with 1.0.156 is great.

@nyonson
nyonson force-pushed the build/direct-minimal-versions branch from 0772e46 to c4f36e0 Compare April 15, 2025 13:09
@github-actions github-actions Bot added the C-internals PRs modifying the internals crate label Apr 15, 2025
@nyonson
nyonson force-pushed the build/direct-minimal-versions branch 2 times, most recently from 05dc80f to 51fd4e8 Compare April 15, 2025 16:13
@nyonson
nyonson marked this pull request as ready for review April 15, 2025 16:41
@nyonson

nyonson commented Apr 15, 2025

Copy link
Copy Markdown
Contributor Author

Tweaked the first patch to bump serde to 1.0.156.

tcharding
tcharding previously approved these changes Apr 16, 2025

@tcharding tcharding left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

ACK 51fd4e8

@tcharding

Copy link
Copy Markdown
Member

I didn't put too many clock cycles into reviewing this because it looks to me like @nyonson has had a good long think about it and it all seems reasonably sane.

@apoelstra

Copy link
Copy Markdown
Member

@nyonson my intention for Cargo-recent.lock is that it is the latest versions of everything that builds with our MSRV.

In practice this is basically impossible to maintain (other than by just letting the file rot) because upgrading it requires noticing and manually trying every update and then trial-and-erroring what combination of upgraded deps it needs and then testing with the MSRV compiler and I'm just never gonna do that.

In theory this also seems like it hasn't worked because everybody assumes that "recent" means "most recent available" rather than "most recent that Andrew bothered testing". So maybe we should change to just always be the output of cargo update, add another weekly cronjob to update it, and stop testing the MSRV compiler with that lockfile. But we should do this in a separate PR with a separate discussion.

Comment thread Cargo-minimal.lock Outdated
[[package]]
name = "proc-macro2"
version = "1.0.63"
version = "1.0.94"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 51fd4e8:

When I run the command locally I get 1.0.95, even with the same nightly version as used by CI. I worry that the -Z direct-minimal-versions flag is unstable somehow.

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.

I think in this case, there was a patch release for the proc-macro2 crate in the time since I generated the lockfile.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Oh, lol, ok.

But then the question becomes -- why does cargo update want to use this version? In general is it going to keep bumping versions of non-direct dependencies like this?

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.

I think this is an example of the crux of the trade off with direct-minimal-versions. It uses the minimal version for any direct dep, which I am pretty sure in a workspace just means anything in the [dependencies] section of each crate's manifest, but for transitive dependencies it uses the default policy for the constraint. Usually "update to highest minor and patch". The idea being that they might not have minimal versions specified correctly, so not testing their constraints. Looks like proc-macro2 is a transitive dep?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Ok, we definitely do not want "update to highest minor and patch". This will result in dependency choices that don't even compile on our MSRV.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks for all the work you put into this! But it seems like this cargo flag is yet another "do something weirdly specifically wrong" flag.

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.

Oof, good point. I'll put this back into draft and see if I can salvage anything from it, but seems like a big hole. I'll run an experiment or two with the new v3 resolver which respects MSRV settings.

Fwiw it looks like all transitive dep versions work with the MSRV which is nice. But agree that it would be a confusing failure when that breaking upgrade arrived.

Comment thread Cargo-minimal.lock Outdated
[[package]]
name = "arrayvec"
version = "0.7.4"
version = "0.7.6"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

In 51fd4e8:

I'm struggling to understand this change. cargo update -Z direct-minimal-versions consistently makes it, but the actual version of arrayvec that we depend on is 0.7.2 (defined by hex-conservative --- this is not even a direct dependency!). I'm not sure why this was at 0.7.4 instead of 0.7.2 and I really don't understand why it's now 0.7.6.

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.

I believe another example of a transitive dep version bump since arrayvec is transitive for this workspace. hex-conservative sets arrayvec = { version = "0.7.2", default-features = false }. I'll double check when using this syntax, but I think this is equivalent to the ^0.7.2 notation, so minor and patch versions will be updated.

I am not sure why the 0.7.4 version was set before, but looks like 0.7.6 is the highest version which fits the constraints.

@nyonson
nyonson marked this pull request as draft April 16, 2025 14:30
@nyonson
nyonson force-pushed the build/direct-minimal-versions branch from 51fd4e8 to 098541f Compare April 16, 2025 16:34
@nyonson

nyonson commented Apr 16, 2025 •

Copy link
Copy Markdown
Contributor Author

@apoelstra feel free to toss this on the back burner, but I made a small tweak in 098541f to address the MSRV leak.

The cargo command to generate Cargo-minimal.toml is getting gnarly, but I think it does what we want now (at least on paper...I did some local tests to verify the basic idea at least): CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback cargo +"$NIGHTLY" update -Z direct-minimal-versions

I added the CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS which enables the MSRV-aware resolver (aka V3). It is yet another flag, but this is the default resolver for the 2024 edition and is already stable, so not super risky. It "fallsback" to the max version of a dependency which still fits the MSRV of the workspace. Now, the big assumption here is that dependencies will correctly add their MSRV in their manifest. This is the risk, but given the relatively small amount of dependencies of the workspace, and the fact that they are currently working, maybe worth considering?

@apoelstra

Copy link
Copy Markdown
Member

I think we should consider the risk. serde and rand have repeatedly violated rules of etiquette like this, and they're around because they're irreplaceable, not because they're any better than the deps we were able to get rid of.

I also don't like the idea that versions in Cargo-minimal.lock would creep upward, even if the upward creep continues to be MSRV compatible. These versions aren't ever supposed to increase unless we change something that requires a newer version.

@nyonson

nyonson commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

I also don't like the idea that versions in Cargo-minimal.lock would creep upward, even if the upward creep continues to be MSRV compatible. These versions aren't ever supposed to increase unless we change something that requires a newer version.

That's my feeling too. I am also hoping that the ecosystem will move towards more MSRV-aware manifests given its the new default, so lowering this new MSRV risk in the long run (:crossed_fingers:). Whereas the upward creep risk will still remain unless checked with something like this patch.

@apoelstra

Copy link
Copy Markdown
Member

Whereas the upward creep risk will still remain unless checked with something like this patch

I'm confused -- this patch seems to introduce a bunch of upward creep, which otherwise wouldn't happen.

@nyonson

nyonson commented Apr 16, 2025

Copy link
Copy Markdown
Contributor Author

I'm confused -- this patch seems to introduce a bunch of upward creep, which otherwise wouldn't happen.

Upward creep on the transitive dependencies, but not the direct dependencies. -Z direct-minimal-versions locks those to their minimums. And while the transitive deps can creep upward, CARGO_RESOLVER_INCOMPATIBLE_RUST_VERSIONS=fallback should cap them at their highest MSRV compatible version.

So the issue this patch is fixing is like how serde and arbitrary, direct dependencies of the workspace, are currently not having their minimums actually exercised in tests because they crept upwards.

@apoelstra

Copy link
Copy Markdown
Member

should cap them at their highest MSRV compatible version

This will constantly increase as the deps release new versions, until finally they lie about their MSRV and our build breaks. I don't want them to change.

nyonson added 2 commits April 16, 2025 15:27
* Standardizing the minimal versions of `serde_json` and `serde_test` in
the workspace. This allows the unstable "direct-minimal-versions" flag
of cargo to be used since it requires one minimal version in a
workspace. Since these are all dev dependencies (including the fuzz
crate) there shouldn't be any change for external consumers.
* Update serde's contraint to the minimum version which the
workspace has been using due to implicit upgrades. Attempts were first
made to see if 1.0.103 could be used, but at the very least, would
require a fair amount of lint settings to be overridden due to the old
code.
* Arbitrary 1.4.0 does not work with the current 1.63.0 MSRV due to some
core::error usage.
Adding the unstable direct-minimal-versions flag insteaf of relying on
just "cargo check" to conservatively upgrade the minimal versions
lockfile.

The issue with "cargo check" by itselft is that any version downgrades
of direct dependencies are not automatically reflected in the lockfile.
They would have to be hand set by developers. The direct-minimal-versions
flag of cargo does this for us, overriding just that aspect of the
conservative resolver of check.
@nyonson
nyonson force-pushed the build/direct-minimal-versions branch from 098541f to 346a472 Compare April 16, 2025 22:30
@nyonson

nyonson commented Apr 17, 2025

Copy link
Copy Markdown
Contributor Author

This will constantly increase as the deps release new versions, until finally they lie about their MSRV and our build breaks. I don't want them to change.

Heh, you have me beat on the ecosystem pessimism (for now), but I get it. Unfortunately I think I have exhausted cargo flag combinations. I momentarily had myself convinced I could combo cargo +nightly check -Z direct-minimal-dependencies so the base is the conservative check upgrade policy (which I still don't know the technical name for, kinda hidden in the docs), but direct deps are always reset to minimums to fix any drift. However, looks like direct-minimal-dependencies only matters when creating a lockfile, not updating one. So essentially has no effect here.

I have arrived at your conclusion that each flag is a little off in its own special way. Maybe there is a more complex approach to analyze the lockfile itself, but not sure if worth it at that point.

@nyonson

nyonson commented Apr 17, 2025 •

Copy link
Copy Markdown
Contributor Author

Have run out of ideas on this one for now, here is a summary of the attempts and why they don't satisfy.

The goal is to (1) enforce that minimal versions of direct dependencies constraints are exercised in CI, but (2) without allowing transitive dependencies to upgrade automatically which introduces breaking risks to the workflow.

  • The current approach uses cargo check's conservative upgrade policy to only update necessary dependencies of an existing lockfile due to changes in the manifest. It only updates to the minimum required version. However, it never performs downgrades, so it is possible for direct minimal versions to not be exercised. (Fails 1)
  • Adding -Z direct-minimal-dependencies to cargo check has no effect, since it only changes behavior when creating a lockfile, not updating. (Fails 1)
  • Using cargo update -Z direct-minimal-dependencies enforces minimum versions of direct dependencies in the generated lockfile, however, it allows all transitive dependencies to upgrade aggressively. These updates can be capped with the new V3 resolver which is MSRV-aware, but then you are hoping your dependencies do not lie about their MSRV. (Fails 2)
  • Using the older cargo update -Z minimal-dependencies version keeps transitive dependencies at their minimums, however it allows their constraints to quietly bump the direct dependency versions. So direct dependency constraints are not exercised, you are not testing what you think you are. Also, it looks like this flag is going to be removed in favor of direct-minimal-dependencies, so not worth investing in. (Fails 1)

@nyonson nyonson closed this Apr 17, 2025
@apoelstra

Copy link
Copy Markdown
Member

Lol that they're going to remove the old broken flag and the new one is just as bad.

The existing lockfiles originally I created manually by downgrading everything by hand, fwiw.

@yancyribbens

Copy link
Copy Markdown
Contributor

I also don't like the idea that versions in Cargo-minimal.lock would creep upward, even if the upward creep continues to be MSRV compatible. These versions aren't ever supposed to increase unless we change something that requires a newer version.

I'm curious about this. I thought the most important thing is that we ensure that this builds for the stated MSRV. I'm missing some piece of information I think since it seems like there is a stricter constraint then just testing for MSRV here.

@apoelstra

Copy link
Copy Markdown
Member

"Does it compile" is table stakes.

The point of Cargo-minimal.lock is that it's minimal.

@yancyribbens

Copy link
Copy Markdown
Contributor

"Does it compile" is table stakes.

Is it not enough to try to compile with CI using the MSRV? If it fails to compile, maybe some crate made a mistake in the MSRV and then it would require intervention to downgrade. Is that why you are interested in keeping the "minimal" version numbers locked?

@Kixunil

Kixunil commented Apr 22, 2025

Copy link
Copy Markdown
Collaborator
  • Using the older cargo update -Z minimal-dependencies version keeps transitive dependencies at their minimums, however it allows their constraints to quietly bump the direct dependency versions. So direct dependency constraints are not exercised, you are not testing what you think you are.

FTR This is effectively correct, because there can only be one version of a dependency with the same major version. Annoyingly, if a dependency is mentioned at all it has to be in the lock file, so we can't have a lock file for each optional dependency. Thus if, e.g. foo dependes on serde = "1.0.42" but our crate works with serde = "1.0.21" we have no way to actually test it so we're forced to bump our serde requirement even if it's too strict. I guess we just have to live with it.

apoelstra added a commit that referenced this pull request Aug 23, 2025
…ency changes

d39a8ac fix: add all optional feature dependencies (Nick Johnson)
328937a fix: build Cargo-minimal.lock with min deps (Nick Johnson)
b75fa0b fix: upgrade deps for min version checks (Nick Johnson)

Pull request description:

  Attempt two to close #4313 
  
  I first attempted to iron out dependency drift in #4330, but wasn't quite able to check all the boxes. I think I might have now though by just running the two cargo flags, `direct-minimal-versions` and `minimal-versions`, one after the other in the update lock file script. Running `direct` first checks for the issue where direct dependencies might be lying. And running `minimal-versions` after that forces all transitive versions down to their minimums, fixing any drift (can see this in the lock file change). We can trust that `minimal-versions` won't bump our own direct deps because we just checked them.
  
  The first commit is ironing out little issues required to use `direct-minimal-versions`. Essentially fixing manifests which are out of date and lying about the required version. The `serde` bump might be a large one, but I was running into an issue where it was pulling in an old `proc-macro2` which required a rust compiler option, `proc_macro_span_shrink`, that is no longer available.
  
  The second commit updates the lock file script to use the new flags and generates the new lock files.
  
  The third commit might be drop-able, but I found it kinda of strange that only the default features where being used to generate the dependency lock files. Now, granted, the rust-bitcoin dependency tree is so lean that it appears enabling all features doesn't add any new dependencies, but still makes me feel better for the future.


ACKs for top commit:
  tcharding:
    ACK d39a8ac
  apoelstra:
    ACK d39a8ac; successfully ran local tests


Tree-SHA512: 3f7f5dde71902ee55e1afd5d95c5d350de95ccbb145350efdaa5b940c3d17aed240f39fc77cb9de46fe98a04fd040c4e87f8d2ba2d602d415ad27720c4a0ff04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-bitcoin PRs modifying the bitcoin crate C-hashes PRs modifying the hashes crate C-internals PRs modifying the internals crate C-primitives C-units PRs modifying the units crate test

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Can Cargo-minimal.lock versions drift upwards on downgrades?

6 participants