Skip to content

Conversation

@dpaoliello
Copy link
Contributor

@dpaoliello dpaoliello commented Aug 27, 2025

First issue:

Currently cargo_config_env assumes that cargo config always serializes each entry in the env table to an object, but it is valid to use just a string for the value in the env table and this is converted to a string in the JSON:

> type .\.cargo\config.toml
[env]
IS_OBJECT = { value = "Hello" }
NOT_OBJECT = "World!"
> cargo +nightly -Z unstable-options config get --format json
{"env":{"IS_OBJECT":{"value":"Hello"},"NOT_OBJECT":"World!"}}

The fix for this is to handle JSON strings as well as objects.

Second issue:

Currently the only values available to the env! macro are those set in the env table of the Cargo configuration file (plus a few other hard coded values). This makes it difficult to simulate having environment variables set (e.g., to match a CI system) without either modifying the current project's configuration (which is usually checked-in) or the user's configuration (which is shared between projects).

This change makes the values set in the cargo.extraEnv configuration option available via the env! macro when loading a project via Cargo manifests. This provides a straightforward way for users to set custom values for env! and for those values to be consistent between language services and cargo check.

Note that this feature respects the force option in the env table, so it will only overwrite the values from extraEnv if force is set to true.

Fixes #13004
Partial fix for #17593 (changes env! but not include!)

@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 27, 2025
@dpaoliello dpaoliello marked this pull request as draft August 28, 2025 18:06
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 28, 2025
@dpaoliello dpaoliello changed the title Allow env vars set in cargo.extraEnv to be resolved by the env! macro Improvements for resolving the value of the env! macro Aug 28, 2025
@dpaoliello dpaoliello marked this pull request as ready for review August 28, 2025 18:33
@rustbot rustbot added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Aug 28, 2025
Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Just one nit.

Thanks!

extra_env: &FxHashMap<String, Option<String>>,
) -> Env {
let mut env = Env::default();
for (key, value) in extra_env {
Copy link
Contributor

Choose a reason for hiding this comment

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

Env implements Extend, please use that.

)| {
let env = env.clone().into_iter().collect();
let mut env = env.clone().into_iter().collect::<Env>();
for (key, value) in extra_env.iter() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Here again use Extend.

Copy link
Contributor

@ChayimFriedman2 ChayimFriedman2 left a comment

Choose a reason for hiding this comment

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

Thanks!

@ChayimFriedman2 ChayimFriedman2 added this pull request to the merge queue Oct 21, 2025
Merged via the queue into rust-lang:master with commit 40cd34d Oct 21, 2025
15 checks passed
@rustbot rustbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Oct 21, 2025
@dpaoliello dpaoliello deleted the extraenv branch October 21, 2025 18:43
@lnicola lnicola changed the title Improvements for resolving the value of the env! macro fix: improve handling of the env! macro Oct 24, 2025
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

Successfully merging this pull request may close these issues.

How to pass values to env! macro in VS Code

3 participants