Read environment variables through Config instead of std::env::var(_os)#11727
Read environment variables through Config instead of std::env::var(_os)#11727bors merged 5 commits intorust-lang:masterfrom
Config instead of std::env::var(_os)#11727Conversation
|
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
ehuss
left a comment
There was a problem hiding this comment.
Thanks for getting this started!
| /// 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.
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.
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.
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_envandget_env_osfor reading environment variables from theConfigand uses them to replace the many of the calls tostd::env::varandvar_os(see the discussion in #11588).Closes #11588