Skip to content

Commit

Permalink
Added logging to crate_universe checksum validation (#2657)
Browse files Browse the repository at this point in the history
When `CARGO_BAZEL_DEBUG=1` is enabled, the following log lines will
print when determining repins.
```
Query 2024-05-16T15:17:26.592139Z DEBUG cargo_bazel::lockfile: cargo-bazel version hash: ef009193262ad048e8890de3f6c4c66f8f26584396e9b6f544894b337ab951ce
Query 2024-05-16T15:17:26.592402Z DEBUG cargo_bazel::lockfile: lockfile context hash: a89c68580b8ac127d1c3d3a700c3c895d107d0826a84c6594c0b4d6c55459af8
Query 2024-05-16T15:17:26.592417Z DEBUG cargo_bazel::lockfile: workspace config hash: baf129ecaaf01eda9020a45991fe804ce7def5e3dc600e8f4f9b99c814ab5635
Query 2024-05-16T15:17:26.592453Z DEBUG cargo_bazel::lockfile: splicing manifest hash: dceb142aa8d5be9cec3bf5d6318150b17c874a7ac84ca3fd9a608b3df279435c
Query 2024-05-16T15:17:26.592456Z DEBUG cargo_bazel::lockfile: Cargo version hash: 573f7bac3e83c5bd86e2fc369f5ecdafc3db984a9641f9f72b6bf68ab060f9d6
Query 2024-05-16T15:17:26.592458Z DEBUG cargo_bazel::lockfile: Rustc version hash: 25b2309140d57d18083da87e1597ef66785a693f2c12dbd7c6912728a2fbce1c
Query 2024-05-16T15:17:26.592461Z DEBUG cargo_bazel::lockfile: Digest hash: 81997a51e91981729d56b9118839163206d491451a10bf9611c6037d1ae2f665
Error: Digests do not match: Current Digest("4cdd2a75d814f49c49d7c02588accb2bc780d3cb1c0f718a83d7edffe4bbebe0") != Expected Digest("81997a51e91981729d56b9118839163206d491451a10bf9611c6037d1ae2f665")
```
  • Loading branch information
UebelAndre committed May 16, 2024
1 parent 1566bad commit 4023d94
Show file tree
Hide file tree
Showing 24 changed files with 70 additions and 44 deletions.
2 changes: 1 addition & 1 deletion crate_universe/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion crate_universe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ exclude = ["test_data"]

[package]
name = "cargo-bazel"
version = "0.13.0"
version = "0.14.0"
authors = ["Andre Brisco - andre.brisco@protonmail.com"]
categories = ["development-tools"]
description = "A collection of tools which use Cargo to generate build targets for Bazel"
Expand Down
5 changes: 3 additions & 2 deletions crate_universe/private/common_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,14 @@ STDERR ------------------------------------------------------------------------
{stderr}
"""

def execute(repository_ctx, args, env = {}):
def execute(repository_ctx, args, env = {}, allow_fail = False):
"""A heler macro for executing some arguments and displaying nicely formatted errors
Args:
repository_ctx (repository_ctx): The rule's context object.
args (list): A list of strings which act as `argv` for execution.
env (dict, optional): Environment variables to set in the execution environment.
allow_fail (bool, optional): Allow the process to fail.
Returns:
struct: The results of `repository_ctx.execute`
Expand All @@ -50,7 +51,7 @@ def execute(repository_ctx, args, env = {}):
quiet = quiet,
)

if result.return_code:
if result.return_code and not allow_fail:
fail(_EXECUTE_ERROR_MESSAGE.format(
args = args,
exit_code = result.return_code,
Expand Down
1 change: 1 addition & 0 deletions crate_universe/private/generate_utils.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,7 @@ def determine_repin(repository_ctx, generator, lockfile_path, config, splicing_m
repository_ctx = repository_ctx,
args = args,
env = env,
allow_fail = True,
)

# If it was determined repinning should occur but there was no
Expand Down
17 changes: 6 additions & 11 deletions crate_universe/src/cli/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use std::fs;
use std::path::PathBuf;

use anyhow::Result;
use anyhow::{bail, Result};
use clap::Parser;

use crate::config::Config;
Expand Down Expand Up @@ -42,19 +42,19 @@ pub fn query(opt: QueryOptions) -> Result<()> {
// Read the lockfile
let content = match fs::read_to_string(&opt.lockfile) {
Ok(c) => c,
Err(_) => return announce_repin("Unable to read lockfile"),
Err(_) => bail!("Unable to read lockfile"),
};

// Deserialize it so we can easily compare it with
let lockfile: Context = match serde_json::from_str(&content) {
Ok(ctx) => ctx,
Err(_) => return announce_repin("Could not load lockfile"),
Err(_) => bail!("Could not load lockfile"),
};

// Check to see if a digest has been set
let digest = match &lockfile.checksum {
Some(d) => d.clone(),
None => return announce_repin("No digest provided in lockfile"),
None => bail!("No digest provided in lockfile"),
};

// Load the config file
Expand All @@ -70,16 +70,11 @@ pub fn query(opt: QueryOptions) -> Result<()> {
&Cargo::new(opt.cargo),
&opt.rustc,
)?;

if digest != expected {
return announce_repin(&format!("Digests do not match: {digest:?} != {expected:?}"));
bail!("Digests do not match: Current {digest:?} != Expected {expected:?}");
}

// There is no need to repin
Ok(())
}

fn announce_repin(reason: &str) -> Result<()> {
eprintln!("{reason}");
println!("repin");
Ok(())
}
51 changes: 40 additions & 11 deletions crate_universe/src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ impl Digest {
})
}

/// A helper for generating a hash and logging it's contents.
fn compute_single_hash(data: &str, id: &str) -> String {
let mut hasher = Sha256::new();
hasher.update(data.as_bytes());
hasher.update(b"\0");
let hash = hasher.finalize().encode_hex::<String>();
tracing::debug!("{} hash: {}", id, hash);
hash
}

fn compute(
context: &Context,
config: &Config,
Expand All @@ -108,25 +118,44 @@ impl Digest {

let mut hasher = Sha256::new();

hasher.update(cargo_bazel_version.as_bytes());
hasher.update(Digest::compute_single_hash(
cargo_bazel_version,
"cargo-bazel version",
));
hasher.update(b"\0");

hasher.update(serde_json::to_string(context).unwrap().as_bytes());
// The lockfile context (typically `cargo-bazel-lock.json`).
hasher.update(Digest::compute_single_hash(
&serde_json::to_string(context).unwrap(),
"lockfile context",
));
hasher.update(b"\0");

hasher.update(serde_json::to_string(config).unwrap().as_bytes());
// This content is generated by various attributes in Bazel rules and written to a file behind the scenes.
hasher.update(Digest::compute_single_hash(
&serde_json::to_string(config).unwrap(),
"workspace config",
));
hasher.update(b"\0");

hasher.update(serde_json::to_string(splicing_metadata).unwrap().as_bytes());
// Data collected about Cargo manifests and configs that feed into dependency generation. This file
// is also generated by Bazel behind the scenes based on user inputs.
hasher.update(Digest::compute_single_hash(
&serde_json::to_string(splicing_metadata).unwrap(),
"splicing manifest",
));
hasher.update(b"\0");

hasher.update(cargo_version.as_bytes());
hasher.update(Digest::compute_single_hash(cargo_version, "Cargo version"));
hasher.update(b"\0");

hasher.update(rustc_version.as_bytes());
hasher.update(Digest::compute_single_hash(rustc_version, "Rustc version"));
hasher.update(b"\0");

Self(hasher.finalize().encode_hex::<String>())
let hash = hasher.finalize().encode_hex::<String>();
tracing::debug!("Digest hash: {}", hash);

Self(hash)
}

pub(crate) fn bin_version(binary: &Path) -> Result<String> {
Expand Down Expand Up @@ -212,7 +241,7 @@ mod test {
);

assert_eq!(
Digest("83ad667352ca5a7cb3cc60f171a65f3bf264c7c97c6d91113d4798ca1dfb8d48".to_owned()),
Digest("7f8d38b770a838797e24635a9030d4194210ff331f1a5b59c753f23fd197b5d8".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -257,7 +286,7 @@ mod test {
);

assert_eq!(
Digest("40a5ede6a47639166062fffab74e5dbe229b1d2508bcf70d8dfeba04b4f4ac9a".to_owned()),
Digest("4148d6b336e574a67417f77fb4727e9c1b014a0b5ab90771f57285f96bca0fee".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -288,7 +317,7 @@ mod test {
);

assert_eq!(
Digest("0d217eec46c3d5a00b142db59994eaa226b630418f70e6b54e8ecc66f7d549da".to_owned()),
Digest("e81dba9d36276baa8d491373fe09ef38e71e68c12f70e45b7c260ba2c48a87f5".to_owned()),
digest,
);
}
Expand Down Expand Up @@ -337,7 +366,7 @@ mod test {
);

assert_eq!(
Digest("9e3d58a48b375bec2cf8c783d92f5d8db67306046e652ee376f30c362c882f56".to_owned()),
Digest("f1b8ca07d35905bbd8bba79137ca7a02414b4ef01f28c459b78d1807ac3a8191".to_owned()),
digest,
);
}
Expand Down
2 changes: 1 addition & 1 deletion crate_universe/version.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
""" Version info for the `cargo-bazel` repository """

VERSION = "0.13.0"
VERSION = "0.14.0"
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "8eebb15991d786d92c33fada8f4d1ffa4066e458853e3e9057788d5dfc923296",
"checksum": "81f62de6f649e0f4b4ba6bef3b10e732e395b45134f33b001ae0d5f8109e7494",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "ef0996d8d24c870ad4d6781696663b4ce32158b5a11f5c5e6f2040f647408cbf",
"checksum": "ed78b5a6ffbed80c354bfe097ab85aaef09b242fd0759fed09db2ffdbd6d3768",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "dd50f7aa27d4f281b8f283beb8b44a332ad8c11731f68250159e38622d89e6c3",
"checksum": "e358b727b6eaba65cda94827b0980ee467846ac1b0201c049bc92b7d5d19cdba",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "69b55cc95efd1af3026173f4d04a6de7862fcf666203f853cd2042615c7d6192",
"checksum": "c8be56b12c4f1e37f413fd2e975f9d35f34d8465f2b1af1f4a661bd777c0aed5",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "cedf00d05772baf88268e3a53147b38b4a0060135b4893a5bab134f08176265a",
"checksum": "dbfb871acaddb8d60e157a70b9b16dbf8ad1e78dd12536461ff4c94340fbdbc7",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "34723df8f98be06e6c8443df29c797c67c3bf65c204842b8ebd5a813963842a2",
"checksum": "c111189559c951979977616f3e3d76f0bacc782b9d8f5dce1a0b7290eb2756ee",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"checksum": "689e7c097350dd53a519f2b2510bd0a5e1a87381beb4c245b82e40f1b4cd4600",
"checksum": "b2145b6622f242823810990c65a4624f4249f08fd8d9816c036fd85b12be9449",
"crates": {
"direct-cargo-bazel-deps 0.0.1": {
"name": "direct-cargo-bazel-deps",
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion examples/crate_universe/using_cxx/cargo-bazel-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion test/no_std/cargo-bazel-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 4023d94

Please sign in to comment.