Cargo.lock 到底锁了什么?什么时候该提交?--locked 有什么用?篡改它会怎样?这篇用一个最小项目把锁文件的内容、校验、可复现性和提交约定逐个验证。
Cargo.lock(完整内容):
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 4
[[package]]
name = "itoa"
version = "1.0.18"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f42a60cbdf9a97f5d2305f08a87dc4e09308d1276d28c869c684d7777685682"
[[package]]
name = "u152"
version = "0.1.0"
dependencies = [
"itoa",
]依赖版本的解析规则见Cargo.toml 依赖解析。
Cargo.toml 加了依赖但锁文件没更新,cargo build --locked:
Updating crates.io index
error: cannot update the lock file <实验目录>/proj/u152/Cargo.lock because --locked was passed to prevent this
help: to generate the lock file without accessing the network, remove the --locked flag and use --offline instead.
不加 --locked 时 cargo 自动更新锁文件:退出码 0;锁文件里的包数:3 把 itoa 的 checksum 前 8 位改成 deadbeef 后构建:
error: checksum for `itoa v1.0.18` changed between lock files
this could be indicative of a few possible errors:
* the lock file is corrupt
* a replacement source in use (e.g., a mirror) returned a different checksum
* the source itself may be corrupt in one way or another
unable to verify that `itoa v1.0.18` is the same as when the lockfile was generated cargo build --locked 退出码 0
cargo tree --locked:
u152 v0.1.0 (<实验目录>/proj/u152)
├── itoa v1.0.5
└── semver v1.0.28在工作区里统一依赖见Workspace 依赖管理。
cargo new --bin:.gitignore 内容 ['/target'](是否忽略 Cargo.lock: False) cargo new --lib:.gitignore 内容 ['/target'](是否忽略 Cargo.lock: False)