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

How to specify fallback value for git sha #174

Closed
YJDoc2 opened this issue Mar 28, 2023 · 9 comments
Closed

How to specify fallback value for git sha #174

YJDoc2 opened this issue Mar 28, 2023 · 9 comments

Comments

@YJDoc2
Copy link

YJDoc2 commented Mar 28, 2023

I want to emit "unknown" in place of git sha if while compiling git info is unavailable. In previous versions, I used to add println!("cargo:rustc-env=VERGEN_GIT_SHA_SHORT=unknown"); before the vergen(config) call, so if vergen does not emit, there is a placeholder to fall back to.

In 8.0, the config is removed, and we have to use the builder pattern, which I have used as follows :

EmitBuilder::builder()
    .git_sha(true)
    .emit()

But when git version is not present, this emits VERGEN_IDEMPOTENT_OUTPUT as the value. I saw in the documentation it is mentioned that if we specify certain env vars, vergen will take their values, so I added

std::env::set_var("VERGEN_GIT_SHA", "0xdeadbob");

Before the emit call, but that still outputs the same (VERGEN_IDEMPOTENT_OUTPUT). Also if I'm not mistake this will be an unconditional override.

Is there any way with which we can define a fallback value for a certain param?

Thanks!

@CraZySacX
Copy link
Member

That is the correct method to override the output. Seems like a bug. I'll take a look.

@CraZySacX
Copy link
Member

CraZySacX commented Mar 28, 2023

I wrote a little stand alone test and was able to generate the override successfully (build.rs below):

use anyhow::Result;
use vergen::EmitBuilder;
use std::env;

pub fn main() -> Result<()> {
    env::set_var("VERGEN_GIT_SHA", "my override");
    EmitBuilder::builder().git_sha(true).emit()
}

Here was the output when running cargo build -vvv:

     Running `/home/jozias/projects/rust-lang/orc/target/debug/build/orc-6214949a09c27336/build-script-build`
[orc 0.1.0] cargo:rustc-env=VERGEN_GIT_SHA=my override
[orc 0.1.0] cargo:rerun-if-changed=.git/HEAD
[orc 0.1.0] cargo:rerun-if-changed=.git/refs/heads/master
[orc 0.1.0] cargo:rerun-if-changed=build.rs
[orc 0.1.0] cargo:rerun-if-env-changed=VERGEN_IDEMPOTENT
[orc 0.1.0] cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH
     Running `CARGO=/home/jozias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/bin/cargo CARGO_BIN_NAME=orc CARGO_CRATE_NAME=orc CARGO_MANIFEST_DIR=/home/jozias/projects/rust-lang/orc CARGO_PKG_AUTHORS='' CARGO_PKG_DESCRIPTION='' CARGO_PKG_HOMEPAGE='' CARGO_PKG_LICENSE='' CARGO_PKG_LICENSE_FILE='' CARGO_PKG_NAME=orc CARGO_PKG_README='' CARGO_PKG_REPOSITORY='' CARGO_PKG_RUST_VERSION='' CARGO_PKG_VERSION=0.1.0 CARGO_PKG_VERSION_MAJOR=0 CARGO_PKG_VERSION_MINOR=1 CARGO_PKG_VERSION_PATCH=0 CARGO_PKG_VERSION_PRE='' CARGO_PRIMARY_PACKAGE=1 LD_LIBRARY_PATH='/home/jozias/projects/rust-lang/orc/target/debug/deps:/home/jozias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib:/home/jozias/.rustup/toolchains/nightly-x86_64-unknown-linux-gnu/lib' OUT_DIR=/home/jozias/projects/rust-lang/orc/target/debug/build/orc-549c0e8738dd82b5/out VERGEN_GIT_SHA='my override' rustc --crate-name orc --edition=2021 src/main.rs --error-format=json --json=diagnostic-rendered-ansi,artifacts,future-incompat --diagnostic-width=325 --crate-type bin --emit=dep-info,link -C embed-bitcode=no -C debuginfo=2 -C metadata=563b6a5e977ccb04 -C extra-filename=-563b6a5e977ccb04 --out-dir /home/jozias/projects/rust-lang/orc/target/debug/deps -C incremental=/home/jozias/projects/rust-lang/orc/target/debug/incremental -L dependency=/home/jozias/projects/rust-lang/orc/target/debug/deps`
    Finished dev [unoptimized + debuginfo] target(s) in 4.87s

If possible, could you run cargo clean and cargo build -vvv and post the output here. May help determine what is going on. I've also noticed that the rerun-if-changed=build.rs seems to not be honored. I was forced to run a clean between builds to pick up changes in that file.

@CraZySacX
Copy link
Member

CraZySacX commented Mar 28, 2023

I should also mention that any failures are silently suppressed by default so vergen won't impact the rest of the build adversely. You can use fail_on_error() on the EmitBuilder to allow failures to propagate up to build.rs.

@YJDoc2
Copy link
Author

YJDoc2 commented Mar 29, 2023

This was run after cargo clean, in a directory where I had deleted the .git folder to see what happens when git dir is not found. To note, in the normal case, where git dir is present, it has correctly given the sha.

My code in build.rs is

use anyhow::Result;
use vergen::EmitBuilder;

fn main() -> Result<()> {
    println!("cargo:rustc-env=VERGEN_GIT_SHA=unknown");
    std::env::set_var("VERGEN_GIT_SHA", "0xdeadbob");
    EmitBuilder::builder()
    .git_sha(true)
    .emit()
}

The relevant section of vvv output is :

[youki 0.0.4] cargo:rustc-env=VERGEN_GIT_SHA=unknown
[youki 0.0.4] cargo:rustc-env=VERGEN_GIT_SHA=VERGEN_IDEMPOTENT_OUTPUT
[youki 0.0.4] cargo:warning=not within a suitable 'git' worktree!
[youki 0.0.4] cargo:warning=VERGEN_GIT_SHA set to default
[youki 0.0.4] cargo:rerun-if-changed=build.rs
[youki 0.0.4] cargo:rerun-if-env-changed=VERGEN_IDEMPOTENT
[youki 0.0.4] cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH
warning: not within a suitable 'git' worktree!
warning: VERGEN_GIT_SHA set to default

The o/p after removing my custom fallback (first println) is

   Running `/tmp/youki/target/debug/build/youki-742bbcccb9a2924c/build-script-build`
[youki 0.0.4] cargo:rustc-env=VERGEN_GIT_SHA=VERGEN_IDEMPOTENT_OUTPUT
[youki 0.0.4] cargo:warning=not within a suitable 'git' worktree!
[youki 0.0.4] cargo:warning=VERGEN_GIT_SHA set to default
[youki 0.0.4] cargo:rerun-if-changed=build.rs
[youki 0.0.4] cargo:rerun-if-env-changed=VERGEN_IDEMPOTENT
[youki 0.0.4] cargo:rerun-if-env-changed=SOURCE_DATE_EPOCH
warning: not within a suitable 'git' worktree!
warning: VERGEN_GIT_SHA set to default

@CraZySacX
Copy link
Member

The current logic only applies override values in place of otherwise successful generation. In this particular case, an error is occurring and causing the defaults to be used, completely skipping the override check. I could see where you would still want the overrides even if errors have happened. I'll get that logic added.

@YJDoc2
Copy link
Author

YJDoc2 commented Mar 29, 2023

Hey, thanks for the info.
On that note, would it be possible (with/without the change), to override the value only if missing? The set_env will always override it, but if possible I want the override only if the proper value is missing. Originally I did it using the println! "hack" ; but if the value is always set, that cannot be used now.

@YJDoc2
Copy link
Author

YJDoc2 commented Apr 4, 2023

Hey @CraZySacX Can you take a look at my comment above and let me know if there is any way I can conditionally override the GIT_SHA when it is not available and let it take its proper value otherwise?
Thanks !

@CraZySacX
Copy link
Member

Currently that is not possible. This requires some thought regarding implementation. Any errors are propagated back up to the emitter and overrides/idempotent is used to fill in the variables. I think in your particular case, you could turn on the fail_on_error flag, match on the emitter result, and use custom println as you did before in the Err block. Not elegant, but should work.

use anyhow::Result;
use vergen::EmitBuilder;

pub fn main() -> Result<()> {
    match EmitBuilder::builder().fail_on_error().git_sha(true).emit() {
        Ok(_) => {}
        Err(_e) => {
            // Your custom printlns here
        }
    }
    Ok(())
}

@YJDoc2
Copy link
Author

YJDoc2 commented Apr 6, 2023

Thanks, that works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants