Share Cargo.lock if both main.rs and lib.rs exist?

Hello,

I've made it a habit to have my Rust projects include both a main.rs and a lib.rs. Most of the functionality would be attached to lib.rs, while main.rs mostly just exposes some of that functionality through a command line interface.

The main question: should Cargo.lock be listed in .gitignore?

The documentation says that Cargo.lock should be shared (i.e. committed to the repository, i.e. not listed in .gitignore) if it is a binary crate (i.e. has main.rs), but not if it is a library crate (i.e. has lib.rs).

What if you have both? In that case, two crates will be created: one binary crate based on main.rs and one library based on lib.rs. Will Cargo.lock affect the library crate? More precisely, if the library crate from project A will be used by project B, will Cargo.lock from project A be taken into consideration when building project B?

I'm hoping the answer is no. In that case, I'd assume Cargo.lock only affects the binary crate and should be shared (i.e. not listed in .gitignore).

Either way, I'd suggest to update the documentation to clarify this.

Thanks!

Best, Oliver

According to the link you added, no, Cargo.lock from crate A does not have any impact on dependency resolution when building crate B:

Users dependent on the library will not inspect the library’s Cargo.lock (even if it exists).

So I'd add Cargo.lock to version control.

Ah, thanks. Yes, of course. Looks like I did not read the documentation carefully enough.

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.