Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should Cargo.lock be committed when the crate is both a rust library and an executable?

I 've read https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html

If I understand correctly, when I commit Cargo.lock into my crate (which is both a library and an executable)'s repository, and also, publish it to crates.io, downstream crates will ignore it and build it's own snap, right?

like image 348
Danielhu Avatar asked Jul 12 '20 13:07

Danielhu


People also ask

What is the purpose of Cargo lock?

The purpose of a Cargo. lock is to describe the state of the world at the time of a successful build. It is then used to provide deterministic builds across whatever machine is building the package by ensuring that the exact same dependencies are being compiled.

Should I put Cargo lock in Gitignore?

If you're building a non-end product, such as a rust library that other rust packages will depend on, put Cargo. lock in your . gitignore . If you're building an end product, which are executable like command-line tool or an application, or a system library with crate-type of staticlib or cdylib , check Cargo.

How does Cargo work Rust?

Cargo is the Rust package manager. Cargo downloads your Rust package's dependencies, compiles your packages, makes distributable packages, and uploads them to crates.io, the Rust community's package registry.


1 Answers

Yes, crates that depend on your library will ignore your Cargo.lock. The Cargo FAQ provides more details:

Why do binaries have Cargo.lock in version control, but not libraries?

The purpose of a Cargo.lock is to describe the state of the world at the time of a successful build. It is then used to provide deterministic builds across whatever machine is building the package by ensuring that the exact same dependencies are being compiled.

This property is most desirable from applications and packages which are at the very end of the dependency chain (binaries). As a result, it is recommended that all binaries check in their Cargo.lock.

For libraries the situation is somewhat different. A library is not only used by the library developers, but also any downstream consumers of the library. Users dependent on the library will not inspect the library’s Cargo.lock (even if it exists). This is precisely because a library should not be deterministically recompiled for all users of the library.

If a library ends up being used transitively by several dependencies, it’s likely that just a single copy of the library is desired (based on semver compatibility). If Cargo used all of the dependencies' Cargo.lock files, then multiple copies of the library could be used, and perhaps even a version conflict.

In other words, libraries specify semver requirements for their dependencies but cannot see the full picture. Only end products like binaries have a full picture to decide what versions of dependencies should be used.

like image 78
Till Ulen Avatar answered Sep 18 '22 06:09

Till Ulen