Skip to content
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

Merged
merged 5 commits into from
Feb 21, 2023

Conversation

kylematsuda
Copy link
Contributor

@kylematsuda kylematsuda commented Feb 17, 2023

What does this PR try to resolve?

Adds two new methods get_env and get_env_os for reading environment variables from the Config and uses them to replace the many of the calls to std::env::var and var_os (see the discussion in #11588).

Closes #11588

@rustbot
Copy link
Collaborator

rustbot commented Feb 17, 2023

r? @ehuss

(rustbot has picked a reviewer for you, use r? to override)

@rustbot rustbot added A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-cli Area: Command-line interface, option parsing, etc. A-configuration Area: cargo config files and env vars A-console-output Area: Terminal output, colors, progress bar, etc. A-diagnostics Area: Error and warning messages generated by Cargo itself. A-git Area: anything dealing with git A-interacts-with-crates.io Area: interaction with registries A-layout Area: target output directory layout, naming, and organization A-profiles Area: profiles A-registries Area: registries A-registry-authentication Area: registry authentication and authorization (authn authz) Command-doc Command-fix Command-install Command-new S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 17, 2023
Copy link
Contributor

@ehuss ehuss left a 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!

Comment on lines 781 to 786
/// 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()
}
Copy link
Contributor

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?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

@ehuss
Copy link
Contributor

ehuss commented Feb 21, 2023

Thanks! I'm pretty sure (though not certain) that there shouldn't be any behavioral changes here. 🤞

@bors r+

@bors
Copy link
Contributor

bors commented Feb 21, 2023

📌 Commit b864262 has been approved by ehuss

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Feb 21, 2023
@bors
Copy link
Contributor

bors commented Feb 21, 2023

⌛ Testing commit b864262 with merge 7b98113...

@bors
Copy link
Contributor

bors commented Feb 21, 2023

☀️ Test successful - checks-actions
Approved by: ehuss
Pushing 7b98113 to master...

@bors bors merged commit 7b98113 into rust-lang:master Feb 21, 2023
bors added a commit that referenced this pull request Feb 22, 2023
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.
weihanglo added a commit to weihanglo/rust that referenced this pull request Feb 23, 2023
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)
bors added a commit to rust-lang-ci/rust that referenced this pull request Feb 23, 2023
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)
@ehuss ehuss added this to the 1.69.0 milestone Mar 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-build-execution Area: anything dealing with executing the compiler A-cfg-expr Area: Platform cfg expressions A-cli Area: Command-line interface, option parsing, etc. A-configuration Area: cargo config files and env vars A-console-output Area: Terminal output, colors, progress bar, etc. A-diagnostics Area: Error and warning messages generated by Cargo itself. A-git Area: anything dealing with git A-interacts-with-crates.io Area: interaction with registries A-layout Area: target output directory layout, naming, and organization A-profiles Area: profiles A-registries Area: registries A-registry-authentication Area: registry authentication and authorization (authn authz) Command-doc Command-fix Command-install Command-new S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Avoid directly reading environment variables
4 participants