-
Notifications
You must be signed in to change notification settings - Fork 12.9k
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
Future deprecation of env::{set, remove}_var
#92365
Conversation
r? @kennytm (rust-highfive has picked a reviewer for you, use r? to override) |
The job Click to see the possible cause of the failure (guessed by this bot)
|
Hmm…not sure how we can avoid triggering the lint. I'm fairly certain rustc itself needs environment variables. |
@jhpratt I think you'd have to Also, rather than "method is unsound", I'd suggest "method cannot be made thread-safe". |
How about "method is unsound because it cannot be made thread-safe"? I think any message without "unsound" is not ideal. |
@@ -327,6 +327,7 @@ impl Error for VarError { | |||
/// assert_eq!(env::var(key), Ok("VALUE".to_string())); | |||
/// ``` | |||
#[stable(feature = "env", since = "1.0.0")] | |||
#[rustc_deprecated(since = "TBD", reason = "method is unsound")] |
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.
Can you link to the discussion about why it is unsound?
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.
Which discussion is preferred? I'm pretty sure at this point we could assemble a book with the various discussions.
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.
Good point. Maybe open a tracking issue to be refenced here and write a recap there together with all discussion you can find?
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.
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.
Do you want to defer changing the documentation of this function to another PR, or should we do it here? If we're going to do it as part of this PR then I'm happy to suggest changes to the docs.
How about "method is unsound in the presence of threads"? Multi-threaded programs may have periods where no other threads exist. It is also ok post- |
EDIT: This is actually incorrect, see later comment.
|
I don't think we should trigger that lint without giving some guidance on what people should do instead. As-is, the likely thing people will do is |
@RalfJung I presume that would require altering the lint itself? |
Well, it would require not adding the attribute. It might make sense to have some discussion of the issue in the doc comment and explain that a better replacement will come Eventually (TM), but I don't think we should add the attribute at this point. |
I'm well aware of that, but there are other tasks which are not exposed via |
So is this something that's currently wanted? I don't particularly mind either way; it's not like this was a difficult PR to put together. Simple 👍/👎 from people that are relevant I suppose? |
Maybe add an unstable copy of today's |
Okay, I updated my previous comment to note it's incorrect. According to POSIX:
|
Oh, indeed. There may be a lock behind that call that is still locked after the |
…Simulacrum Remove CommandEnv::apply It's not being used and uses unsound set_var and remove_var functions. This is an internal function that isn't exported (even with `process_internals` feature), so this shouldn't break anything. Also see rust-lang#92365. Note that this isn't the only use of those methods in standard library, so that particular pull request will need more changes than just this to work (in particular, `test_capture_env_at_spawn` is using `set_var` and `remove_var`).
If rustc is setting or unsetting environment variables itself, either directly or indirectly via its dependencies, we should attempt to change that before or during the process of this deprecation. If the uses in rustc are too hard to remove then what hope do other users have? |
Right now it's a future deprecation — it's not actually being deprecated in this PR. The lint is allow by default. Though I do agree that there needs to be a replacement before actually deprecating (and I believe there's consensus on that point). |
Yes, but rustc and libstd have |
That's why it's an issue for this PR but not others, generally speaking. Other users don't have worry about builds failing because of a future deprecation unless they've explicitly opted into the lint. |
To explain my 👎 : I think we should not add a |
This seems to be somewhat stalled. I do think we should have at least some plan for what we intend to support as a replacement, though that replacement doesn't have to exist in stable Rust for us to add At the moment, the only proposal I've seen that seems like a viable replacement is to add unsafe functions. Apart from bikeshedding the names of those methods ( Would someone be interested in submitting a PR adding unsafe |
I can do that tonight. Should just amount to calling the same thing internally. |
I think the intention is for Only mutating the environment is unsafe. |
I believe the goal is to only mark env::set_var as unsafe, and to do so in-place (i.e., with a new custom deprecated_safe or similar attribute), based on the latest discussions in libs-api that I recall. My understanding is that we want to leave env::var safe in Rust's standard library, as the retrieval of environment variables is safe in correctly written programs (i.e., those that don't have any setenv/putenv/etc calls in the possibly MT section). I think the next step before actually deprecating these would be to stabilize #93346, which tracks the RUST_BACKTRACE setting for std::panic. That's the only in-std API stable that relies on the environment, I believe. (Modulo env::home_dir and env::temp_dir, but those are not so interesting). |
Yeah, |
@Mark-Simulacrum, is the implementation of a deprecated_safe attribute up for grabs? I suggested something like that on IRLO and I've been looking for an excuse to work on Rust. 😊 I've been following the thread but wasn't sure if that was the direction things would be going. |
I'm not him, but to my knowledge no one has worked on such an attribute in any way. |
Yes, I think so, would be great to get someone to implement it. No guarantees on whether it gets used, of course, but I believe several folks are in favor of using it so chances are good. |
FWIW we already have at least one example of a "deprecated safe" function: https://doc.rust-lang.org/nightly/std/os/unix/process/trait.CommandExt.html#method.before_exec. That one is just deprecated the normal way now. Not sure if it makes sense to also apply the new approach there. Certainly if we plan to make an edition change here where calling them as safe functions becomes a hard error, we should also do that for |
Part of the goal on the 'in-place' deprecation is that there's no nice alternative name (before_exec/pre_exec in the CommandExt case), so we're stuck with either using a nice name like env::set, or something with an awkward extra word -- e.g., set_unsafe_var -- and the in-place deprecation leaves the door open to the nice name being used later down the line for a safe API (but one that targets only some use cases, perhaps, like just affecting Rust code, or Rust code and C code using some new libc interfaces, etc.) |
PR introducing the unsafe |
FYI, I've been discussing work on the potential new #[rustc_deprecated_safe] attribute on Zulip here: https://rust-lang.zulipchat.com/#narrow/stream/131828-t-compiler/topic/deprecated_safe.20attribute |
I have some commits pushed here that (very, VERY roughly) implement a new #[rustc_deprecated_safe] attribute. I'm hesitant to post even a draft PR as the code quality is not at all up to snuff, but it'd be good know if the approach I'm taking is even valid before I make things proper. I've never touched rustc before, so I'm not really sure what I'm doing. :) https://github.com/skippy10110/rust/commits/rustc_deprecated_safe This covers the following places where making set_var unsafe would break existing code, I'm not sure if there are other places that need to be handled: env::set_var("TEST", "test");
let set_var_fn_ptr: fn(OsString, OsString) = env::set_var;
let set_var_fn_impl: Box<dyn Fn(OsString, OsString)> = Box::new(env::set_var);
let mut set_var_fnmut_impl: Box<dyn FnMut(OsString, OsString)> = Box::new(env::set_var);
let set_var_fnonce_impl: Box<dyn FnOnce(OsString, OsString)> = Box::new(env::set_var); P.S. I haven't updated the new THIR unsafety checker yet, I figured I'd wait until I knew if any of this code is even correct first. |
This is sort of in limbo. Basically there are no technical blockers, but it shouldn't land if @rustbot label -S-waiting-on-author +S-blocked |
At some point in the future it is quite likely that
std::env::set_var
andstd::env::remove_var
will be deprecated. There appears to be consensus that this should happen per a poll on IRLO. The only blocker to actually deprecating these methods is agreement on what should replace them. This PR serves two purposes: indicate they will be deprecated in documentation and trigger thedeprecated_in_future
lint.@rustbot label +S-waiting-on-review +T-libs-api