Expand description
Obtain the exact Cargo.lock used by the workspace that consumes a reusable
external crate.
For a build script belonging to a crate in a workspace,
project-root is sufficient: it
finds the workspace root, and the workspace’s Cargo.lock follows from that
path. This does not work for a crate developed separately and reused by
multiple projects. Cargo builds its crates.io dependency from its cache,
outside the consuming workspace, so searching upward would inspect the
wrong directory.
get-cargo-lock addresses that boundary. The published crate deliberately
does not search: get_cargo_lock panics with setup instructions until the
workspace owner activates a workspace-local proxy.
§Use from an external crate
Add this crate as a build dependency and call get_cargo_lock from the
crate’s build.rs:
[build-dependencies]
get-cargo-lock = "0.1"let lockfile = get_cargo_lock::get_cargo_lock();
println!("cargo:rerun-if-changed={}", lockfile.display());
// Use lockfile for reflection or a nested Cargo invocation.§Configure the consuming workspace
The workspace owner performs this setup once:
cargo install get-cargo-lock
cargo get-cargo-lock install .
cargo checkThe installer creates a get-cargo-lock package in the workspace, adds it
to workspace.members, and adds this override:
[patch.crates-io]
get-cargo-lock = { path = "get-cargo-lock" }Cargo then compiles the injected package in place of the crates.io
dependency. It exposes the same get_cargo_lock API, but its build script
can find the workspace’s Cargo.lock because the package is inside that
workspace. The installer enables this behavior by removing a blocking file;
it does not modify copied Rust sources. Re-running the command safely
refreshes a proxy created by this tool.
The command accepts a workspace directory, its Cargo.toml, or a standalone
package. A standalone package is converted to a workspace containing itself
and the proxy. The installer refuses to overwrite an unrecognized
get-cargo-lock directory.
Functions§
- get_
cargo_ lock - Returns the consuming workspace’s
Cargo.lock.