-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Hint that a new Rust release may be available #4869
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,11 +4,14 @@ use std::fmt::Display; | |
| use std::fs; | ||
| use std::io::{BufRead, Write}; | ||
| use std::path::Path; | ||
| use std::str::FromStr; | ||
| use std::sync::LazyLock; | ||
| use std::time::{SystemTime, UNIX_EPOCH}; | ||
| use std::{cmp, env}; | ||
|
|
||
| use anstyle::Style; | ||
| use anyhow::{Context, Result, anyhow}; | ||
| use chrono::{DateTime, NaiveDate}; | ||
| use clap_cargo::style::{CONTEXT, ERROR, UPDATE_ADDED, UPDATE_UNCHANGED, UPDATE_UPGRADED}; | ||
| use futures_util::future::join_all; | ||
| use git_testament::{git_testament, render_testament}; | ||
|
|
@@ -17,15 +20,18 @@ use tracing_subscriber::{EnvFilter, Registry, reload::Handle}; | |
|
|
||
| use crate::{ | ||
| config::Cfg, | ||
| dist::{DistOptions, TargetTuple, ToolchainDesc}, | ||
| dist::{DistOptions, PartialToolchainDesc, TargetTuple, ToolchainDesc}, | ||
| errors::RustupError, | ||
| install::{InstallMethod, UpdateStatus}, | ||
| process::Process, | ||
| toolchain::{LocalToolchainName, Toolchain, ToolchainName}, | ||
| toolchain::{DistributableToolchain, LocalToolchainName, Toolchain, ToolchainName}, | ||
| utils::{self, ExitCode}, | ||
| }; | ||
|
|
||
| pub(crate) const WARN_COMPLETE_PROFILE: &str = "downloading with complete profile isn't recommended unless you are a developer of the rust language"; | ||
| const RELEASE_CYCLE_DAYS: i64 = 42; | ||
| const NOTIFY_INTERVAL_SECS: u64 = 24 * 60 * 60; | ||
| const FALLBACK_RELEASE_DATE: &str = "2026-04-17"; | ||
|
|
||
| pub(crate) fn confirm(question: &str, default: bool, process: &Process) -> Result<bool> { | ||
| write!(process.stdout().lock(), "{question} ")?; | ||
|
|
@@ -569,3 +575,61 @@ pub(super) fn update_console_filter( | |
| .expect("error reloading `EnvFilter` for console_logger"); | ||
| } | ||
| } | ||
|
|
||
| /// Notifies a user with a hint whenever a new Rust release is available. | ||
| /// This is only shown at max once per day and only if not in proxy mode. | ||
| pub(crate) fn notify_release(cfg: &Cfg<'_>) -> Result<()> { | ||
| if cfg.process.var("RUSTUP_RELEASE_HINT").ok().as_deref() == Some("0") { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let time_now = SystemTime::now() | ||
| .duration_since(UNIX_EPOCH) | ||
| .unwrap_or_default() | ||
| .as_secs(); | ||
|
|
||
| // Limit notifications to at most once per day. | ||
| // This is checked before loading the manifest to avoid unnecessary disk I/O. | ||
| let last_notified = cfg | ||
| .settings_file | ||
| .with(|s| Ok(s.last_release_notified_secs.unwrap_or(0)))?; | ||
| if time_now.saturating_sub(last_notified) < NOTIFY_INTERVAL_SECS { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| let default_host = cfg.default_host_tuple()?; | ||
| let stable_desc = PartialToolchainDesc::from_str("stable")?.resolve(&default_host)?; | ||
| let distributable = DistributableToolchain::new(cfg, stable_desc) | ||
| .map_err(|e| anyhow!("stable toolchain unavailable: {e}"))?; | ||
| let release_date_str = match distributable.get_manifestation() { | ||
| Ok(manifestation) => match manifestation.load_manifest() { | ||
| Ok(Some(manifest)) => manifest.date, | ||
| Ok(None) | Err(_) => FALLBACK_RELEASE_DATE.to_owned(), | ||
| }, | ||
| Err(_) => FALLBACK_RELEASE_DATE.to_owned(), | ||
| }; | ||
|
|
||
| let today = DateTime::from_timestamp(time_now as i64, 0) | ||
| .unwrap_or_default() | ||
| .date_naive(); | ||
|
|
||
| let release_date = NaiveDate::parse_from_str(&release_date_str, "%Y-%m-%d") | ||
| .map_err(|e| anyhow!("could not parse release date '{}': {e}", release_date_str))?; | ||
|
|
||
| // Skip the hint if fewer than 6 weeks have passed since the last known release. | ||
| if (today - release_date).num_days() < RELEASE_CYCLE_DAYS { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| cfg.settings_file.with_mut(|s| { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I still think it might be harmful to write this in cc @Cloud0310 for potential interaction/collaboration regarding XDG.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed. Happy to collaborate with @Cloud0310 on this!
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FranciscoTGouveia I don't think it should be in the For the moment I'll say just write it as a new file under
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I have been working on a patch and wanted to ask whether it would be fine to add a new
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @FranciscoTGouveia Sound good to me :) |
||
| s.last_release_notified_secs = Some(time_now); | ||
| Ok(()) | ||
| })?; | ||
|
|
||
| writeln!( | ||
| cfg.process.stderr().lock(), | ||
| "hint: a new stable Rust release is available. Run `rustup update stable` to install it." | ||
| )?; | ||
|
|
||
| Ok(()) | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.
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.
For top-down style, these should be at the bottom of the module (current exceptions notwithstanding -- want to fix them up in a separate commit?).
View changes since the review