Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added logging to crate_universe checksum validation #2657

Merged
merged 3 commits into from
May 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.

Loading