Skip to content

Commit c7e202f

Browse files
committed
feat: differentiate live and cached toolchain resolution in dump-state
1 parent a4acc2c commit c7e202f

File tree

3 files changed

+30
-16
lines changed

3 files changed

+30
-16
lines changed

src/elan-cli/json_dump.rs

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use elan::{lookup_unresolved_toolchain_desc, resolve_toolchain_desc, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain, UnresolvedToolchainDesc};
1+
use elan::{lookup_unresolved_toolchain_desc, resolve_toolchain_desc_ext, utils::{self, fetch_latest_release_tag}, Cfg, Toolchain, UnresolvedToolchainDesc};
22
use std::{io, path::PathBuf};
33

44
use serde_derive::Serialize;
@@ -22,12 +22,20 @@ struct InstalledToolchain {
2222
path: PathBuf,
2323
}
2424

25+
#[derive(Serialize)]
26+
struct ToolchainResolution {
27+
/// On network error, will always be `Err` even if `elan` commands would fall back to the latest
28+
/// local toolchain if any
29+
live: Result<String>,
30+
/// The latest local toolchain if any independently of network availability
31+
cached: Option<String>,
32+
}
33+
2534
#[derive(Serialize)]
2635
struct DefaultToolchain {
2736
/// Not necessarily resolved name as given to `elan default`, e.g. `stable`
2837
unresolved: UnresolvedToolchainDesc,
29-
/// Fully resolved name; `Err` if `unresolved` needed to be resolved but there was a network error
30-
resolved: Result<String>,
38+
resolved: ToolchainResolution,
3139
}
3240

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

5058
#[derive(Serialize)]
@@ -53,6 +61,12 @@ pub struct StateDump {
5361
toolchains: Toolchains,
5462
}
5563

64+
fn mk_toolchain_resolution(cfg: &Cfg, unresolved: &UnresolvedToolchainDesc, no_net: bool) -> ToolchainResolution {
65+
let live = resolve_toolchain_desc_ext(cfg, unresolved, no_net, false).map(|t| t.to_string()).map_err(|e| e.to_string());
66+
let cached = resolve_toolchain_desc_ext(cfg, unresolved, true, true).map(|t| t.to_string()).map_err(|e| e.to_string()).ok();
67+
ToolchainResolution { live, cached }
68+
}
69+
5670
impl StateDump {
5771
pub fn new(cfg: &Cfg, no_net: bool) -> crate::Result<StateDump> {
5872
let newest = fetch_latest_release_tag("leanprover/elan", no_net);
@@ -76,9 +90,7 @@ impl StateDump {
7690
}).collect(),
7791
default: default.as_ref().map(|default| DefaultToolchain {
7892
unresolved: default.clone(),
79-
resolved: resolve_toolchain_desc(cfg, &default, no_net)
80-
.map(|t| t.to_string())
81-
.map_err(|e| e.to_string()),
93+
resolved: mk_toolchain_resolution(cfg, default, no_net),
8294
}),
8395
active_override: active_override.as_ref().map(|(desc, reason)| Override {
8496
unresolved: desc.clone(),
@@ -87,9 +99,7 @@ impl StateDump {
8799
resolved_active: active_override
88100
.map(|p| p.0)
89101
.or(default)
90-
.map(|t| resolve_toolchain_desc(cfg, &t, no_net)
91-
.map(|tc| tc.to_string())
92-
.map_err(|e| e.to_string()))
102+
.map(|t| mk_toolchain_resolution(cfg, &t, no_net))
93103
},
94104
})
95105
}

src/elan/config.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ impl Cfg {
235235
path: &Path,
236236
) -> Result<Option<(Toolchain, Option<OverrideReason>)>> {
237237
if let Some((toolchain, reason)) = self.find_override(path)? {
238-
let toolchain = resolve_toolchain_desc(&self, &toolchain, false)?;
238+
let toolchain = resolve_toolchain_desc(&self, &toolchain)?;
239239
match self.get_toolchain(&toolchain, false) {
240240
Ok(toolchain) => {
241241
if toolchain.exists() {

src/elan/toolchain.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,19 +92,19 @@ fn find_latest_local_toolchain(cfg: &Cfg, channel: &str) -> Option<ToolchainDesc
9292
toolchains.into_iter().last()
9393
}
9494

95-
pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc, no_net: bool) -> Result<ToolchainDesc> {
95+
pub fn resolve_toolchain_desc_ext(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc, no_net: bool, use_cache: bool) -> Result<ToolchainDesc> {
9696
if let ToolchainDesc::Remote { ref origin, ref release, from_channel: Some(ref channel) } = unresolved_tc.0 {
9797
if release == "lean-toolchain" {
9898
let toolchain_url = format!(
9999
"https://raw.githubusercontent.com/{}/HEAD/lean-toolchain",
100100
origin
101101
);
102-
return resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, fetch_url(&toolchain_url)?.trim())?, no_net);
102+
return resolve_toolchain_desc_ext(cfg, &lookup_unresolved_toolchain_desc(cfg, fetch_url(&toolchain_url)?.trim())?, no_net, use_cache);
103103
} else if release == "stable" || release == "beta" || release == "nightly" {
104104
match utils::fetch_latest_release_tag(origin, no_net) {
105105
Ok(release) => Ok(ToolchainDesc::Remote { origin: origin.clone(), release, from_channel: Some(channel.clone()) }),
106106
Err(e) => {
107-
if let Some(tc) = find_latest_local_toolchain(cfg, &release) {
107+
if let (true, Some(tc)) = (use_cache, find_latest_local_toolchain(cfg, &release)) {
108108
(cfg.notify_handler)(Notification::UsingExistingRelease(&tc));
109109
Ok(tc)
110110
} else {
@@ -120,8 +120,12 @@ pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc
120120
}
121121
}
122122

123+
pub fn resolve_toolchain_desc(cfg: &Cfg, unresolved_tc: &UnresolvedToolchainDesc) -> Result<ToolchainDesc> {
124+
resolve_toolchain_desc_ext(cfg, unresolved_tc, false, true)
125+
}
126+
123127
pub fn lookup_toolchain_desc(cfg: &Cfg, name: &str) -> Result<ToolchainDesc> {
124-
resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, name)?, false)
128+
resolve_toolchain_desc(cfg, &lookup_unresolved_toolchain_desc(cfg, name)?)
125129
}
126130

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

137141
pub fn read_toolchain_desc_from_file(cfg: &Cfg, toolchain_file: &Path) -> Result<ToolchainDesc> {
138-
resolve_toolchain_desc(cfg, &read_unresolved_toolchain_desc_from_file(cfg, toolchain_file)?, false)
142+
resolve_toolchain_desc(cfg, &read_unresolved_toolchain_desc_from_file(cfg, toolchain_file)?)
139143
}
140144

141145
impl<'a> Toolchain<'a> {

0 commit comments

Comments
 (0)