[GH-ISSUE #2348] environment variable CARGO_PKG_VERSION not defined at compile time #970

Open
opened 2026-03-16 01:07:49 +03:00 by kerem · 2 comments
Owner

Originally created by @jagill on GitHub (Aug 7, 2024).
Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2348

Describe the bug
When building with a non-Cargo build tool, CARGO_PKG_VERSION is not set at compile time. But in resolver's lib.rs line 316, the env!("CARGO_PKG_VERSION") assumes it exists and compilation fails.

Hickory should instead use option_env! and handle the None case.

To Reproduce
Attempt to build without Cargo. We use Buck2 (https://buck2.build/).

error: environment variable `CARGO_PKG_VERSION` not defined at compile time
   --> third-party\rust\vendor\hickory-resolver-0.24.1\src\lib.rs:316:5
    |
316 |     env!("CARGO_PKG_VERSION")
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation
   --> ...\1.79.0\library\core\src\macros\mod.rs:1083:5
    |
    = note: in this expansion of `env!`
    |
    = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_PKG_VERSION")` instead

Expected behavior
Crate should build without errors.

System:

  • OS: CentOS
  • Architecture: x86_64
  • Version: 8
  • rustc version: 1.79

Version:
Crate: resolver
Version: 0.24.1

Additional context
The build system, while on CentOS, also builds MacOS architectures.

Originally created by @jagill on GitHub (Aug 7, 2024). Original GitHub issue: https://github.com/hickory-dns/hickory-dns/issues/2348 **Describe the bug** When building with a non-Cargo build tool, `CARGO_PKG_VERSION` is not set at compile time. But in `resolver`'s `lib.rs` line 316, the `env!("CARGO_PKG_VERSION")` assumes it exists and compilation fails. Hickory should instead use `option_env!` and handle the None case. **To Reproduce** Attempt to build without Cargo. We use Buck2 (https://buck2.build/). ``` error: environment variable `CARGO_PKG_VERSION` not defined at compile time --> third-party\rust\vendor\hickory-resolver-0.24.1\src\lib.rs:316:5 | 316 | env!("CARGO_PKG_VERSION") | ^^^^^^^^^^^^^^^^^^^^^^^^^ in this macro invocation --> ...\1.79.0\library\core\src\macros\mod.rs:1083:5 | = note: in this expansion of `env!` | = help: Cargo sets build script variables at run time. Use `std::env::var("CARGO_PKG_VERSION")` instead ``` **Expected behavior** Crate should build without errors. **System:** - OS: CentOS - Architecture: x86_64 - Version: 8 - rustc version: 1.79 **Version:** Crate: resolver Version: 0.24.1 **Additional context** The build system, while on CentOS, also builds MacOS architectures.
Author
Owner

@djc commented on GitHub (Aug 7, 2024):

Why can't you not simply set the environment variable in your Buck2 build environment? IMO it is not unreasonable for Rust code to depend on well-defined environment variables like this.

<!-- gh-comment-id:2274287034 --> @djc commented on GitHub (Aug 7, 2024): Why can't you not simply set the environment variable in your Buck2 build environment? IMO it is not unreasonable for Rust code to depend on well-defined environment variables like this.
Author
Owner

@bluejekyll commented on GitHub (Aug 7, 2024):

This is also very common for CLIs (via clap) to depend on Cargo environment variables for things like description, name, version, or authors.

It would be my opinion that these are properties defined in the Cargo.toml that should be read by any build tool and exposed to make the builds of those tools work correctly, just like would be done with the dependencies lists defined in the Cargo.toml.

<!-- gh-comment-id:2274330358 --> @bluejekyll commented on GitHub (Aug 7, 2024): This is also very common for CLIs (via clap) to depend on Cargo environment variables for things like description, name, version, or authors. It would be my opinion that these are properties defined in the Cargo.toml that should be read by any build tool and exposed to make the builds of those tools work correctly, just like would be done with the dependencies lists defined in the Cargo.toml.
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/hickory-dns#970
No description provided.