get-cargo-lock 0.1.0

Expose the consuming Cargo workspace's lockfile to external build scripts
Documentation

get-cargo-lock

get-cargo-lock lets a reusable external crate's build script obtain the exact Cargo.lock used by the workspace that consumes it.

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 library deliberately does not search: get_cargo_lock::get_cargo_lock() panics with setup instructions until the consumer activates a workspace-local proxy.

Use from an external crate

  1. Add the library as a build dependency:

    [build-dependencies]
    get-cargo-lock = "0.1"
    
  2. Call it from build.rs:

    fn main() {
        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 check

The install command creates a small 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 consequently compiles the injected package in place of the crates.io dependency. Its get_cargo_lock::get_cargo_lock() is the same API, but its build script can find the workspace's Cargo.lock because the package is inside that workspace. The installer activates that behavior by removing the blocking file; it does not modify the copied Rust sources. Re-running the command is safe and refreshes a proxy previously 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 existing unrecognized get-cargo-lock directory.

License

Licensed under either Apache-2.0 or MIT, at your option.