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

Update to vergen 5, add branch, commit time, and build target to the panic metadata, automatically update app version from crate version #2029

Merged
merged 8 commits into from
Apr 19, 2021
3 changes: 1 addition & 2 deletions .github/PULL_REQUEST_TEMPLATE/release-checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ what's changed.

- [ ] Increment the crates that have new commits since the last version update
- [ ] Increment any crates that depend on crates that have changed
- [ ] Use the `zebrad` crate version in the `zebrad` app code and `zebra-network` user agent
- [ ] Use the `zebrad` crate version in the `zebra-network` user agent string
- [ ] Use the latest git tag in `README.md`

### How to Increment Versions
Expand Down Expand Up @@ -57,7 +57,6 @@ Increment the first version component in this list, and reset the other componen
Once you know which versions you want to increment, you can find them in the:
- [ ] zebra* `Cargo.toml`s
- [ ] tower-* `Cargo.toml`s
- [ ] `zebrad` app code: https://github.com/ZcashFoundation/zebra/blob/main/zebrad/src/components/tracing/component.rs
- [ ] `zebra-network` protocol user agent: https://github.com/ZcashFoundation/zebra/blob/main/zebra-network/src/constants.rs
- [ ] `README.md`
- [ ] `Cargo.lock`: automatically generated by `cargo build`
Expand Down
109 changes: 103 additions & 6 deletions Cargo.lock

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

16 changes: 11 additions & 5 deletions zebra-chain/src/parameters/network.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::fmt;
use std::{convert::From, fmt};

#[cfg(any(test, feature = "proptest-impl"))]
use proptest_derive::Arbitrary;
Expand All @@ -13,12 +13,18 @@ pub enum Network {
Testnet,
}

impl From<&Network> for &'static str {
fn from(network: &Network) -> &'static str {
match network {
Network::Mainnet => "Mainnet",
Network::Testnet => "Testnet",
}
}
}

impl fmt::Display for Network {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Network::Mainnet => f.write_str("Mainnet"),
Network::Testnet => f.write_str("Testnet"),
}
f.write_str(self.into())
}
}

Expand Down
2 changes: 1 addition & 1 deletion zebrad/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ sentry = { version = "0.21.0", default-features = false, features = ["backtrace"
sentry-tracing = { git = "https://github.com/kellpossible/sentry-tracing.git", rev = "f1a4a4a16b5ff1022ae60be779eb3fb928ce9b0f" }

[build-dependencies]
vergen = "3.2.0"
vergen = { version = "5.1.1", default-features = false, features = ["cargo", "git"] }

[dev-dependencies]
abscissa_core = { version = "0.5", features = ["testing"] }
Expand Down
108 changes: 7 additions & 101 deletions zebrad/build.rs
Original file line number Diff line number Diff line change
@@ -1,107 +1,13 @@
#![allow(clippy::try_err)]

use std::{env, fs, fs::File, io::Read, path::PathBuf};

use vergen::{generate_cargo_keys, ConstantsFlags};
use vergen::{vergen, Config, ShaKind};

fn main() {
let mut flags = ConstantsFlags::empty();
flags.toggle(ConstantsFlags::SHA_SHORT);

// We want to use REBUILD_ON_HEAD_CHANGE here, but vergen assumes that the
// git directory is in the crate directory, and Zebra uses a workspace.
// See rustyhorde/vergen#15 and rustyhorde/vergen#21 for details.
let result = generate_rebuild_key();
if let Err(err) = result {
eprintln!("Error generating 'cargo:rerun-if-changed': {:?}", err);
}

// Generate the 'cargo:' key output
generate_cargo_keys(flags).expect("Unable to generate the cargo keys!");
}

/// Generate the `cargo:` rebuild keys output
///
/// The keys that can be generated include:
/// * `cargo:rustc-rerun-if-changed=<git dir>/HEAD`
/// * `cargo:rustc-rerun-if-changed=<file git HEAD points to>`
fn generate_rebuild_key() -> Result<(), Box<dyn std::error::Error>> {
// Look for .git and ../.git
// We should really use the `git2` crate here, see rustyhorde/vergen#15
let mut git_dir_or_file = env::current_dir()?.join(".git");
let mut metadata = fs::metadata(&git_dir_or_file);
// git searches all the ancestors of the current directory, but Zebra's
// crates are direct children of the workspace directory, so we only
// need to look at the parent directory
if metadata.is_err() {
git_dir_or_file = env::current_dir()?
.parent()
.ok_or("finding crate's parent directory")?
.to_path_buf()
.join(".git");
metadata = fs::metadata(&git_dir_or_file);
}

// Modified from vergen's REBUILD_ON_HEAD_CHANGE implementation:
// https://github.com/rustyhorde/vergen/blob/master/src/output/envvar.rs#L46
if let Ok(metadata) = metadata {
if metadata.is_dir() {
// Echo the HEAD path
let git_head_path = git_dir_or_file.join("HEAD");
println!("cargo:rerun-if-changed={}", git_head_path.display());

// Determine where HEAD points and echo that path also.
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
eprintln!("HEAD contents: {}", git_head_contents);
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();

if ref_vec.len() == 2 {
let current_head_file = ref_vec[1].trim();
let git_refs_path = git_dir_or_file.join(current_head_file);
println!("cargo:rerun-if-changed={}", git_refs_path.display());
} else {
eprintln!("You are most likely in a detached HEAD state");
}
} else if metadata.is_file() {
// We are in a worktree, so find out where the actual worktrees/<name>/HEAD file is.
let mut git_file = File::open(&git_dir_or_file)?;
let mut git_contents = String::new();
let _ = git_file.read_to_string(&mut git_contents)?;
let dir_vec: Vec<&str> = git_contents.split(": ").collect();
eprintln!(".git contents: {}", git_contents);
let git_path = dir_vec[1].trim();

// Echo the HEAD path
let git_head_path = PathBuf::from(git_path).join("HEAD");
println!("cargo:rerun-if-changed={}", git_head_path.display());

// Find out what the full path to the .git dir is.
let mut actual_git_dir = PathBuf::from(git_path);
actual_git_dir.pop();
actual_git_dir.pop();
let mut config = Config::default();

// Determine where HEAD points and echo that path also.
let mut f = File::open(&git_head_path)?;
let mut git_head_contents = String::new();
let _ = f.read_to_string(&mut git_head_contents)?;
eprintln!("HEAD contents: {}", git_head_contents);
let ref_vec: Vec<&str> = git_head_contents.split(": ").collect();
*config.cargo_mut().features_mut() = false;
*config.cargo_mut().profile_mut() = false;

if ref_vec.len() == 2 {
let current_head_file = ref_vec[1].trim();
let git_refs_path = actual_git_dir.join(current_head_file);
println!("cargo:rerun-if-changed={}", git_refs_path.display());
} else {
eprintln!("You are most likely in a detached HEAD state");
}
} else {
Err("Invalid .git format (Not a directory or a file)")?;
};
} else {
Err(".git directory or file not found in crate dir or parent dir")?;
};
*config.git_mut().semver_mut() = false;
*config.git_mut().sha_kind_mut() = ShaKind::Short;

Ok(())
vergen(config).expect("Unable to generate the cargo keys!");
}
17 changes: 11 additions & 6 deletions zebrad/src/application.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ impl ZebradApp {
}

pub fn git_commit() -> &'static str {
const GIT_COMMIT_VERGEN: &str = env!("VERGEN_SHA_SHORT");
const GIT_COMMIT_VERGEN: &str = env!("VERGEN_GIT_SHA_SHORT");
const GIT_COMMIT_GCLOUD: Option<&str> = option_env!("SHORT_SHA");

GIT_COMMIT_GCLOUD.unwrap_or(GIT_COMMIT_VERGEN)
Expand Down Expand Up @@ -155,17 +155,22 @@ impl Application for ZebradApp {
};

// collect the common metadata for the issue URL and panic report
let network = config.network.network.to_string();
let panic_metadata = vec![
("version", env!("CARGO_PKG_VERSION").to_string()),
("git commit", Self::git_commit().to_string()),
("Zcash network", network),
// git
("version", env!("CARGO_PKG_VERSION")),
("branch", env!("VERGEN_GIT_BRANCH")),
("git commit", Self::git_commit()),
("commit timestamp", env!("VERGEN_GIT_COMMIT_TIMESTAMP")),
// build
("target triple", env!("VERGEN_CARGO_TARGET_TRIPLE")),
// config
("Zcash network", (&config.network.network).into()),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks I've been waiting for these for a while.

];

let mut builder = color_eyre::config::HookBuilder::default();
let mut metadata_section = "Metadata:".to_string();
for (k, v) in panic_metadata {
builder = builder.add_issue_metadata(k, v.clone());
builder = builder.add_issue_metadata(k, v);
metadata_section.push_str(&format!("\n{}: {}", k, v));
}

Expand Down
2 changes: 1 addition & 1 deletion zebrad/src/components/tracing/component.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ impl<A: abscissa_core::Application> Component<A> for Tracing {
}

fn version(&self) -> abscissa_core::Version {
abscissa_core::Version::parse("1.0.0-alpha.6").unwrap()
abscissa_core::Version::parse(env!("CARGO_PKG_VERSION")).unwrap()
}

fn before_shutdown(&self, _kind: Shutdown) -> Result<(), FrameworkError> {
Expand Down