Skip to content

Commit

Permalink
feat: differentiate live and cached toolchain resolution in dump-state
Browse files Browse the repository at this point in the history
  • Loading branch information
Kha committed Dec 10, 2024
1 parent a4acc2c commit c7e202f
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 16 deletions.
30 changes: 20 additions & 10 deletions src/elan-cli/json_dump.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use elan::{lookup_unresolved_toolchain_desc, resolve_toolchain_desc, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain, UnresolvedToolchainDesc};
use elan::{lookup_unresolved_toolchain_desc, resolve_toolchain_desc_ext, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain, UnresolvedToolchainDesc};
use std::{io, path::PathBuf};

use serde_derive::Serialize;
Expand All @@ -22,12 +22,20 @@ struct InstalledToolchain {
path: PathBuf,
}

#[derive(Serialize)]
struct ToolchainResolution {
/// On network error, will always be `Err` even if `elan` commands would fall back to the latest
/// local toolchain if any
live: Result<String>,
/// The latest local toolchain if any independently of network availability
cached: Option<String>,
}

#[derive(Serialize)]
struct DefaultToolchain {
/// Not necessarily resolved name as given to `elan default`, e.g. `stable`
unresolved: UnresolvedToolchainDesc,
/// Fully resolved name; `Err` if `unresolved` needed to be resolved but there was a network error
resolved: Result<String>,
resolved: ToolchainResolution,
}

#[derive(Serialize)]
Expand All @@ -44,7 +52,7 @@ struct Toolchains {
/// `None` if no override for current directory configured, in which case `default` if any is used
active_override: Option<Override>,
/// Toolchain, if any, ultimately chosen based on `default` and `active_override`
resolved_active: Option<Result<String>>,
resolved_active: Option<ToolchainResolution>,
}

#[derive(Serialize)]
Expand All @@ -53,6 +61,12 @@ pub struct StateDump {
toolchains: Toolchains,
}

fn mk_toolchain_resolution(cfg: &Cfg, unresolved: &UnresolvedToolchainDesc, no_net: bool) -> ToolchainResolution {
let live = resolve_toolchain_desc_ext(cfg, unresolved, no_net, false).map(|t| t.to_string()).map_err(|e| e.to_string());
let cached = resolve_toolchain_desc_ext(cfg, unresolved, true, true).map(|t| t.to_string()).map_err(|e| e.to_string()).ok();
ToolchainResolution { live, cached }
}

impl StateDump {
pub fn new(cfg: &Cfg, no_net: bool) -> crate::Result<StateDump> {
let newest = fetch_latest_release_tag("leanprover/elan", no_net);
Expand All @@ -76,9 +90,7 @@ impl StateDump {
}).collect(),
default: default.as_ref().map(|default| DefaultToolchain {
unresolved: default.clone(),
resolved: resolve_toolchain_desc(cfg, &default, no_net)
.map(|t| t.to_string())
.map_err(|e| e.to_string()),
resolved: mk_toolchain_resolution(cfg, default, no_net),
}),
active_override: active_override.as_ref().map(|(desc, reason)| Override {
unresolved: desc.clone(),
Expand All @@ -87,9 +99,7 @@ impl StateDump {
resolved_active: active_override
.map(|p| p.0)
.or(default)
.map(|t| resolve_toolchain_desc(cfg, &t, no_net)
.map(|tc| tc.to_string())
.map_err(|e| e.to_string()))
.map(|t| mk_toolchain_resolution(cfg, &t, no_net))
},
})
}
Expand Down
2 changes: 1 addition & 1 deletion src/elan/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ impl Cfg {
path: &Path,
) -> Result<Option<(Toolchain, Option<OverrideReason>)>> {
if let Some((toolchain, reason)) = self.find_override(path)? {
let toolchain = resolve_toolchain_desc(&self, &toolchain, false)?;
let toolchain = resolve_toolchain_desc(&self, &toolchain)?;
match self.get_toolchain(&toolchain, false) {
Ok(toolchain) => {
if toolchain.exists() {
Expand Down
14 changes: 9 additions & 5 deletions src/elan/toolchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,19 +92,19 @@ fn find_latest_local_toolchain(cfg: &Cfg, channel: &str) -> Option<ToolchainDesc
toolchains.into_iter().last()
}

pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc, no_net: bool) -> Result<ToolchainDesc> {
pub fn resolve_toolchain_desc_ext(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc, no_net: bool, use_cache: bool) -> Result<ToolchainDesc> {
if let ToolchainDesc::Remote { ref origin, ref release, from_channel: Some(ref channel) } = unresolved_tc.0 {
if release == "lean-toolchain" {
let toolchain_url = format!(
"https://raw.githubusercontent.com/{}/HEAD/lean-toolchain",
origin
);
return resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, fetch_url(&toolchain_url)?.trim())?, no_net);
return resolve_toolchain_desc_ext(cfg, &lookup_unresolved_toolchain_desc(cfg, fetch_url(&toolchain_url)?.trim())?, no_net, use_cache);
} else if release == "stable" || release == "beta" || release == "nightly" {
match utils::fetch_latest_release_tag(origin, no_net) {
Ok(release) => Ok(ToolchainDesc::Remote { origin: origin.clone(), release, from_channel: Some(channel.clone()) }),
Err(e) => {
if let Some(tc) = find_latest_local_toolchain(cfg, &release) {
if let (true, Some(tc)) = (use_cache, find_latest_local_toolchain(cfg, &release)) {
(cfg.notify_handler)(Notification::UsingExistingRelease(&tc));
Ok(tc)
} else {
Expand All @@ -120,8 +120,12 @@ pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc
}
}

pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc) -> Result<ToolchainDesc> {
resolve_toolchain_desc_ext(cfg, unresolved_tc, false, true)
}

pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, name)?, false)
resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, name)?)
}

pub fn read_unresolved_toolchain_desc_from_file(cfg: &Cfg, toolchain_file: &Path) -> Result<UnresolvedToolchainDesc> {
Expand All @@ -135,7 +139,7 @@ pub fn read_unresolved_toolchain_desc_from_file(cfg: &Cfg, toolchain_file: &Path
}

pub fn read_toolchain_desc_from_file(cfg: &Cfg, toolchain_file: &Path) -> Result<ToolchainDesc> {
resolve_toolchain_desc(cfg, &read_unresolved_toolchain_desc_from_file(cfg, toolchain_file)?, false)
resolve_toolchain_desc(cfg, &read_unresolved_toolchain_desc_from_file(cfg, toolchain_file)?)
}

impl<'a> Toolchain<'a> {
Expand Down

0 comments on commit c7e202f

Please sign in to comment.