-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
Read environment variables through Config
instead of std::env::var(_os)
#11727
Conversation
r? @ehuss (rustbot has picked a reviewer for you, use r? to override) |
…get_env and get_env_os
…e config is available
e34dbe5
to
a148cd4
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for getting this started!
src/cargo/util/config/mod.rs
Outdated
/// Get the value of environment variable `key` through the `Config` snapshot. | ||
/// | ||
/// This can be used similarly to `std::env::var_os`. | ||
pub fn get_env_os(&self, key: impl AsRef<str>) -> Option<String> { | ||
self.env.get(key.as_ref()).cloned() | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might be a little confusing since it is not returning an OsString
, which is what the _os
suffix implies.
The lack of OsString
support also seems like a regression in the cases where var_os
is used. Cargo has fairly poor support for non-UTF-8 paths and environment, but I think it would be good to try to avoid making it worse.
How hard would it be to store OsString
in the map instead of String
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oops yes good point! I'll investigate what else needs to change if Config.env
is a HashMap<OsString, OsString>
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, this turned out to be not too bad to implement (I think). Please see the most recent commit. I mainly had to add/edit a few utility methods on Config
to handle invalid UTF-8 entries in self.env
.
Thanks! I'm pretty sure (though not certain) that there shouldn't be any behavioral changes here. 🤞 @bors r+ |
☀️ Test successful - checks-actions |
Make more reads of environment variables go through the `Config` As discussed in #11588, it's better to make reads of environment variables go through the `Config` instead of calling `std::env::var` or `env::var_os` everywhere. Most of the "easy" places to change this in `src/` were handled in #11727. This PR fixes a few remaining call sites that were missed in #11727, namely: - `LocalFingerprint::find_stale_item` - `util::profile::start` and `Profiler` - `util::rustc::rustc_fingerprint` After doing this, there are a few remaining calls to `env::var` in `src/` but those probably require more discussion to fix.
15 commits in 17b3d0de0897e1c6b8ca347bd39f850bb0a5b9f6..9d5b32f503fc099c4064298465add14d4bce11e6 2023-02-17 19:45:09 +0000 to 2023-02-22 23:04:16 +0000 - refactor(job_queue): docs and move types around (rust-lang/cargo#11758) - Scrub more of the test environment (rust-lang/cargo#11757) - Make more reads of environment variables go through the `Config` (rust-lang/cargo#11754) - Revert "Update curl-sys to use libcurl 7.88.1" (rust-lang/cargo#11755) - use consistent case (rust-lang/cargo#11748) - Switch some tests from `build` to `check` (rust-lang/cargo#11725) - Fix typo in sparse-registry warning message (rust-lang/cargo#11753) - reuse url encoding from `url` crate, don't use separate `percent-encoding` (rust-lang/cargo#11750) - Read environment variables through `Config` instead of `std::env::var(_os)` (rust-lang/cargo#11727) - Update curl-sys to use libcurl 7.88.1 (rust-lang/cargo#11749) - mdman: update pretty_assertions to reduce deps (rust-lang/cargo#11747) - Cleanup tests (rust-lang/cargo#11745) - Enhance help texts of position args (rust-lang/cargo#11740) - Fix typo (rust-lang/cargo#11741) - Update comment about cargo-ok (rust-lang/cargo#11724)
Update cargo 15 commits in 17b3d0de0897e1c6b8ca347bd39f850bb0a5b9f6..9d5b32f503fc099c4064298465add14d4bce11e6 2023-02-17 19:45:09 +0000 to 2023-02-22 23:04:16 +0000 - refactor(job_queue): docs and move types around (rust-lang/cargo#11758) - Scrub more of the test environment (rust-lang/cargo#11757) - Make more reads of environment variables go through the `Config` (rust-lang/cargo#11754) - Revert "Update curl-sys to use libcurl 7.88.1" (rust-lang/cargo#11755) - use consistent case (rust-lang/cargo#11748) - Switch some tests from `build` to `check` (rust-lang/cargo#11725) - Fix typo in sparse-registry warning message (rust-lang/cargo#11753) - reuse url encoding from `url` crate, don't use separate `percent-encoding` (rust-lang/cargo#11750) - Read environment variables through `Config` instead of `std::env::var(_os)` (rust-lang/cargo#11727) - Update curl-sys to use libcurl 7.88.1 (rust-lang/cargo#11749) - mdman: update pretty_assertions to reduce deps (rust-lang/cargo#11747) - Cleanup tests (rust-lang/cargo#11745) - Enhance help texts of position args (rust-lang/cargo#11740) - Fix typo (rust-lang/cargo#11741) - Update comment about cargo-ok (rust-lang/cargo#11724)
What does this PR try to resolve?
Adds two new methods
get_env
andget_env_os
for reading environment variables from theConfig
and uses them to replace the many of the calls tostd::env::var
andvar_os
(see the discussion in #11588).Closes #11588