Skip to content

Conversation

@ChenRuiwei
Copy link

std::env::set_var is marked as unsafe starting in the 2024 Edition.

https://doc.rust-lang.org/edition-guide/rust-2024/newly-unsafe-functions.html

@orhun
Copy link
Member

orhun commented Jun 6, 2025

Can you also look for other usages in the repo and replace them as well?

@ChenRuiwei ChenRuiwei changed the title Fix set_var in log-with-tracing.md Fix set_var by adding unsafe in Rust 2024 Edition Jun 7, 2025
@ChenRuiwei
Copy link
Author

Grep all usages of set_var and fixed them all. Also fix a missing include error in async-template-counter app.

Comment on lines 101 to 108
std::env::set_var(
"RUST_LOG",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var(LOG_ENV.clone()))
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME"))),
);
unsafe {
std::env::set_var(
"RUST_LOG",
std::env::var("RUST_LOG")
.or_else(|_| std::env::var(LOG_ENV.clone()))
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME"))),
)
};
Copy link
Member

Choose a reason for hiding this comment

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

It's probably better to remove the set_var call instead of making this unsafe, and configuring EnvFilter below properly.

Copy link
Member

@orhun orhun Jun 7, 2025

Choose a reason for hiding this comment

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

I'm okay with either way, we can also add a comment why this is unsafe etc.

Maybe it would be nice to show both ways?

Copy link
Member

@joshka joshka Jun 7, 2025

Choose a reason for hiding this comment

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

Not really - it's there because EnvFilter uses RUST_LOG in the call later down, but it doesn't have to. Envfilter has a way to choose the env variable to use, and it has a way to set a default directive that's used if the env variable is not set. And it has a way to return an error if the variable is not set. I.e. all the things that this code tries to do here are handled by correctly using EnvFilter::builder. The set_var call is unnecessary.

Copy link
Author

Choose a reason for hiding this comment

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

Great insights! Solved in the latest commit.

The usage of [`from_default_env`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.EnvFilter.html#method.from_default_env)
is equivelent to

```rust
use tracing_subscriber::filter::{EnvFilter, LevelFilter};

EnvFilter::builder()
    .with_default_directive(LevelFilter::ERROR.into())
    .from_env_lossy()
```

While in this case the `with_default_directive` will never be reached
because we always set a **vaild** crate level filter string to `RUST_LOG`.

And [`from_env_lossy`](https://docs.rs/tracing-subscriber/latest/tracing_subscriber/filter/struct.Builder.html#method.from_env_lossy)
[uses `parse_lossy` internally](https://docs.rs/tracing-subscriber/latest/src/tracing_subscriber/filter/env/builder.rs.html#168-171)
@ChenRuiwei ChenRuiwei changed the title Fix set_var by adding unsafe in Rust 2024 Edition Remove the usage of set_var Jun 7, 2025
.or_else(|_| std::env::var(LOG_ENV.clone()))
.unwrap_or_else(|_| format!("{}=info", env!("CARGO_CRATE_NAME"))),
);
let filter_string = std::env::var("RUST_LOG")
Copy link
Member

Choose a reason for hiding this comment

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

nit:

Suggested change
let filter_string = std::env::var("RUST_LOG")
let log_filter = std::env::var("RUST_LOG")

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.

3 participants