-
Notifications
You must be signed in to change notification settings - Fork 29
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
Autometrics doesn't compile with Bazel due to CARGO_PKG_REPOSITORY
not defined at compile time
#176
Comments
Hi, thanks for the issue! We talked internally and decided that we'll change the code to instead use fn main() {
AutometricsSettings::builder()
.repo_url("https://github.com/autometrics-dev/autometrics-rs/")
.init();
// ...
} In the meantime while we add a fallback, I suggest as a workaround that you define |
For the record, the reason we thought it was fine was the Cargo book:
That’s why we were kind of expecting the variables to be always accessible if |
Thanks for working on this issue,
I sincerely appreciate your efforts.
For clarification, the cargo book is correct. However Bazel invokes the
Rust compiler directly meaning whatever magic cargo may cook up along the
way is not working when Bazel executes the build.
I try the environment variable as you said and report back probably
tomorrow when I'm back in office.
Again thank you so much for ironing this out because this also resolves any
potential issues with buck2 and all other enterprise grade build systems.
…On Mon, May 13, 2024 at 8:06 PM Gerry Agbobada ***@***.***> wrote:
For the record, the reason we thought it was fine was the Cargo book
<https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-crates>
:
Cargo exposes these environment variables to your crate when it is compiled
That’s why we were kind of expecting the variables to be always accessible
if cargo is used to compile, even for hermetic builds. The more we know
—
Reply to this email directly, view it on GitHub
<#176 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AFYR7XG47772XKQ3MF4BUKTZCCUFXAVCNFSM6AAAAABHQE7VB2VHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCMBXGQYDAMRXGQ>
.
You are receiving this because you authored the thread.Message ID:
***@***.***>
|
Closing this issue as its currently impossible to compile autometrics with Bazel. After having worked around the aforementioned environment variable issue, I was running into However, as a word of warning, whenever you change build environment variables in Bazel, you actually invalidate the cache and trigger a full rebuild plus you have to carry over those variables to test and run targets meaning if your repo is large, you spent the rest of the day watching the compiler.... That is to say, if this ever get touched again, please add a custom constructor and pass in the repo_url as argument, as shown in the example above, to prevent any reliance on env variables as these only cause 99 unnecessary problems at scale. it's a pity because this is an outstanding crate that adds real value. |
I've a larger mono-repo that builds with Bazel and when adding autometrics as dependency,
compile fails with the error below.
Apparently, the issue is caused because the repo_url in the setting file cannot access the environment variable.
Unlike Cargo, Bazel operates in a hermetic environment meaning Bazel cannot access host environment variables so this is actually expected behavior for hermetic builds.
The file in question:
autometrics-rs/autometrics/src/settings.rs
Line 201 in 9bc24e0
I had similar issues in the past and, by experience, relying on settings stored in an env variables that is expected on the host was almost always problematic, so there are roughly three ways to resolve this issue:
Ensure repo url is initialized during construction. The RAII pattern does makes sense here unless the URL somehow isn't invariant.
Modify the recovery strategy. Currently, the implementation tries to read the env variable like so:
The problem is that repo_url isn't initialized, but the env variable is inaccessible either from within Bazel.
Therefore, if the repo_url is invariant, you can store it in a constant and use that as a fallback.
If it's not invariant, for whatever reason, you would need to add a small util that constructs the correct repo_url.
Personally, in my project, I rely on configuration as code and only use one single env variable "ENV" to determine in which environment the system runs in order to determine which configuration to load.
I don't necessarily want to write a patch & fill a PR at this time because, quite frankly, I have a hard time understanding how settings configuration works in autometrics let alone how cargo config works in this context. However, If necessary, I can provide a self-contained code example with some instructions how to run the Bazel build if it helps to get this fixed.
The full error log:
The text was updated successfully, but these errors were encountered: