1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
mod gc;
mod init;
mod install;
mod list;
mod r#override;
mod set;
mod show;
mod uninstall;
mod update;
use std::{ffi::OsString, path::PathBuf};
use anyhow::{Context, anyhow, bail};
use clap::{ArgAction, Args, Parser, Subcommand};
pub use self::{
gc::gc,
init::{init, setup_midenup},
install::install,
list::list,
r#override::r#override,
set::set,
show::ShowCommand,
uninstall::uninstall,
update::update,
};
use crate::{channel, config, manifest, options};
pub const MIDENUP_MANIFEST_URI_ENV: &str = "MIDENUP_MANIFEST_URI";
#[derive(Debug, Parser)]
#[command(
name = "midenup",
author,
about = "The Miden toolchain installer",
long_about = None,
multicall(true),
disable_version_flag(true)
)]
pub struct Midenup {
#[command(subcommand)]
behavior: Behavior,
}
/// What set of behavior the CLI should exhibit
#[derive(Debug, Subcommand)]
// Boxing here would mean boxing a clap parse root that is constructed exactly once per process,
// trading a real ergonomic cost for no measurable benefit.
#[allow(clippy::large_enum_variant)]
enum Behavior {
/// The Miden toolchain installer
Midenup {
#[command(flatten)]
config: GlobalArgs,
#[command(subcommand)]
command: Option<Commands>,
},
/// Invoke components of the current Miden toolchain
#[command(external_subcommand)]
Miden(Vec<OsString>),
}
/// Global configuration options for `midenup`
#[derive(Debug, Args)]
struct GlobalArgs {
/// The location of the Miden toolchain root
#[arg(long, hide(true), value_name = "DIR", env = "MIDENUP_HOME")]
pub midenup_home: Option<PathBuf>,
#[arg(long, hide(true), value_name = "DIR", env = "CARGO_HOME")]
pub cargo_home: Option<PathBuf>,
/// The URI from which we should load the global toolchain manifest
#[arg(
long,
hide(true),
value_name = "FILE",
env = MIDENUP_MANIFEST_URI_ENV,
default_value = manifest::VersionedManifest::PUBLISHED_MANIFEST_URI
)]
pub manifest_uri: String,
/// Determines wether the components are installed in debug mode. Useful for
/// debugging and faster installations. This flag is only avaialble to
/// `midenup`, not `miden`.
#[arg(env = "MIDENUP_DEBUG_MODE", action = ArgAction::Set, default_value = "false", hide = true)]
pub debug: bool,
/// Display verbose output, mainly used during install.
#[arg(short, long, action, default_value_t = false)]
pub verbose: bool,
/// Displays `midenup`'s version information.
#[arg(short = 'V', long, action, default_value_t = false)]
pub version: bool,
}
/// All the available Midenup Commands
#[derive(Debug, Subcommand)]
enum Commands {
/// Bootstrap the `midenup` environment.
///
/// This initializes the `MIDEN_HOME` directory layout and configuration.
Init,
/// Install a Miden toolchain
Install {
/// The channel or version to install, e.g. `stable` or `0.15.0`
#[arg(required(true), value_name = "CHANNEL", value_parser)]
channel: channel::UserChannel,
#[clap(flatten)]
options: options::InstallationOptions,
},
/// Reclaim disk space from toolchain installations nothing refers to any more.
///
/// Every change to an installed channel publishes a new copy and leaves the previous one in
/// place, because another process may still be running out of it. This removes those.
Gc,
/// List all available toolchains
List,
/// Uninstall a Miden toolchain
Uninstall {
/// The channel or version to install, e.g. `stable` or `0.15.0`
#[arg(required(true), value_name = "CHANNEL", value_parser)]
channel: channel::UserChannel,
/// Also delete this channel's mutable data (`var/<channel>`), such as the client's
/// database. Without this flag it is kept, and you are told where it lives.
#[arg(long, action, default_value_t = false)]
purge: bool,
},
/// Show information about the local midenup environment.
#[command(subcommand)]
Show(ShowCommand),
/// Sets the current active miden toolchain for the current project.
/// This creates a miden-toolchain.toml file in the present working directory.
Set {
/// The channel or version to set, e.g. `stable` or `0.15.0`
#[arg(required(true), value_name = "CHANNEL", value_parser)]
channel: channel::UserChannel,
},
/// Sets the system's default toolchain.
///
/// Unlike `rustup`, midenup does *not* have a notion of directory
/// overrides. Instead, the `midenup set` command can be used to configure a
/// directory-specific toolchain.
Override {
/// The channel or version to set, e.g. `stable` or `0.15.0`
#[arg(required(true), value_name = "CHANNEL", value_parser)]
channel: channel::UserChannel,
},
/// Update your installed Miden toolchains.
Update {
/// `midenup update`'s behavior differs depending on the specified [CHANNEL]
/// - If left blank, then midenup will check for updates in all the downloaded toolchains.
/// - If [CHANNEL] is a version, updates that toolchain against the channel upstream now
/// publishes under that name.
/// - If [CHANNEL] is a network (mainnet, testnet, devnet), follows the network to whatever
/// channel it now names: installing it if needed, carrying your component selection
/// across, and moving your data under var/ with it.
#[clap(verbatim_doc_comment)]
#[arg(value_name = "CHANNEL", value_parser)]
channel: Option<channel::UserChannel>,
#[clap(flatten)]
options: options::UpdateOptions,
},
}
impl Commands {
/// Whether this subcommand writes to `$MIDENUP_HOME`.
///
/// Only mutating operations take the advisory lock (spec section 9.9). Making `list` or `show`
/// wait behind a long install would be a regression in a tool people run to find out *why*
/// something is taking so long.
fn is_mutating(&self) -> bool {
match self {
Self::Init
| Self::Install { .. }
| Self::Uninstall { .. }
| Self::Update { .. }
| Self::Gc => true,
// Writes `toolchains/default`.
Self::Override { .. } => true,
// Writes `miden-toolchain.toml` in the working directory, not `$MIDENUP_HOME`.
Self::Set { .. } => false,
Self::List | Self::Show(_) => false,
}
}
/// Execute the requested subcommand
pub fn execute(
&self,
config: &config::Config,
state: &mut crate::state::LocalState,
) -> anyhow::Result<()> {
match &self {
Self::Init => {
init(config)?;
Ok(())
},
Self::Gc => gc(config, state),
Self::List => list(config, state),
Self::Install { channel, options } => {
let manifest = config.upstream_manifest()?;
let Some(channel) = manifest.get_channel(channel) else {
// Which names exist is manifest data now, so a typo has to be answerable with
// what was actually declared rather than "doesn't exist or is unavailable".
match channel {
channel::UserChannel::Named(name) => bail!(
"unknown channel '{name}'; known networks are {}",
manifest.network_names().collect::<Vec<_>>().join(", ")
),
channel::UserChannel::Version(version) => {
bail!("there is no toolchain {version} in the channel manifest")
},
}
};
install(config, channel, state, options)
},
// Deliberately not resolved against upstream: a channel that has been withdrawn is
// exactly one a user needs to be able to uninstall (spec section 12.3).
Self::Uninstall { channel, purge } => uninstall(config, channel, state, *purge),
Self::Update { channel, options } => update(config, channel.as_ref(), state, options),
Self::Show(cmd) => cmd.execute(config, state),
Self::Set { channel } => set(config, channel),
Self::Override { channel } => r#override(config, state, channel),
}
}
}
impl Midenup {
/// Get the effective configuration for the current session
pub fn config(&self) -> anyhow::Result<config::Config> {
let working_directory =
std::env::current_dir().context("unable to read current directory")?;
match &self.behavior {
Behavior::Miden(_) => {
// Always respect XDG dirs if set
let midenup_home = std::env::var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.map(|dir| dir.join("midenup"))
.or_else(|| dirs::data_dir().map(|dir| dir.join("midenup")))
// If for whatever reason, we can't access the data dir, we fall
// back to .local/share
.or_else(|| {
dirs::home_dir()
.map(|home| home.join(".local").join("share"))
})
.ok_or_else(||
anyhow!("Failed to set midenup directory.\
Consider setting a value for XDG_DATA_HOME in your shell's profile"
)
)?;
let cargo_home = std::env::var_os("CARGO_HOME")
.map(PathBuf::from)
.or_else(|| dirs::home_dir().map(|home| home.join(".cargo")))
.ok_or_else(|| {
anyhow!(
"$CARGO_HOME and $HOME are unset, but at least one must be set in \
your shell's profile"
)
})?;
let manifest_uri = std::env::var(MIDENUP_MANIFEST_URI_ENV)
.unwrap_or(manifest::VersionedManifest::PUBLISHED_MANIFEST_URI.to_string());
// Before the upstream fetch below: an unreachable upstream must not be able to
// prevent a local migration (spec section 12.2).
crate::migrate_v1::migrate_if_needed(&midenup_home)?;
crate::migrate_networks::migrate_if_needed(&midenup_home)
.context("failed to migrate the toolchains directory to the network layout")?;
config::Config::init(
working_directory,
midenup_home,
cargo_home,
manifest_uri,
false,
)
},
Behavior::Midenup { config, .. } => {
let midenup_home = config
.midenup_home
.clone()
.or_else(|| {
// Always respect XDG dirs if set
std::env::var_os("XDG_DATA_HOME")
.map(PathBuf::from)
.map(|dir| dir.join("midenup"))
})
.or_else(|| dirs::data_dir().map(|dir| dir.join("midenup")))
// If for whatever reason, we can't access the data dir, we fall
// back to .local/share
.or_else(|| {
dirs::home_dir()
.map(|home| home.join(".local").join("share"))
})
.ok_or_else(||
anyhow!("Failed to set midenup directory.\
Consider setting a value for XDG_DATA_HOME in your shell's profile"
)
)?;
let cargo_home = config
.cargo_home
.clone()
.or_else(|| std::env::var_os("CARGO_HOME").map(PathBuf::from))
.or_else(|| dirs::home_dir().map(|home| home.join(".cargo")))
.ok_or_else(|| {
anyhow!(
"$CARGO_HOME and $HOME are unset, but at least one must be set in \
your shell's profile"
)
})?;
// See above: migration precedes any upstream fetch.
crate::migrate_v1::migrate_if_needed(&midenup_home)?;
crate::migrate_networks::migrate_if_needed(&midenup_home)
.context("failed to migrate the toolchains directory to the network layout")?;
config::Config::init(
working_directory,
midenup_home,
cargo_home,
&config.manifest_uri,
config.debug,
)
},
}
}
/// Execute this session with the provided configuration.
pub fn execute(&self, config: &config::Config) -> anyhow::Result<()> {
let mut state = config.local_state()?;
self.execute_with_state(config, &mut state)
}
/// Execute this session with the provided configuration and local manifest
pub fn execute_with_state(
&self,
config: &config::Config,
state: &mut crate::state::LocalState,
) -> anyhow::Result<()> {
use crate::miden_wrapper;
// Migration first, before recovery and before anything reads local state. `config()`
// already ran this ahead of the upstream fetch on the CLI path; it is idempotent and costs
// one `stat`, and running it here too means a caller that built its own `Config` -- every
// in-process test -- gets the same startup sequence rather than a subtly different one.
if let crate::migrate_v1::MigrationOutcome::Migrated { channels } =
crate::migrate_v1::migrate_if_needed(&config.midenup_home)?
{
report_migration(&channels);
*state = config.local_state()?;
}
crate::migrate_networks::migrate_if_needed(&config.midenup_home)
.context("failed to migrate the toolchains directory to the network layout")?;
recover(config, state)?;
match &self.behavior {
Behavior::Miden(argv) => {
// No lock: dispatch is read-only until it discovers the toolchain is missing, and
// it takes the lock itself at that point (`ensure_current_is_installed`).
miden_wrapper::miden_wrapper(argv, config, state)
.with_context(|| format!("failed to execute '{}'", get_full_command(argv)))?;
},
Behavior::Midenup { config: global_args, command: subcommand } => {
if global_args.version {
println!("{}", miden_wrapper::display_version(config));
} else if let Some(subcommand) = subcommand {
let _lock = if subcommand.is_mutating() {
let lock = crate::lock::acquire(&config.midenup_home)?;
// Whoever held the lock may have changed what is installed, so nothing may
// be planned against the state read before waiting for it.
*state = config.local_state()?;
Some(lock)
} else {
None
};
subcommand.execute(config, state)?;
} else {
bail!("no subcommand provided. Run `midenup --help` for usage information.")
}
},
}
// After execution we check if need to update the midenup/opt symlink
// This is done *after* execution because some commands change what the active toolchain
// (update, set) and some remove the directory entirely (uninstall)
config.update_opt_symlinks()?;
Ok(())
}
}
fn report_migration(channels: &[semver::Version]) {
use colored::Colorize;
println!(
"{}: migrated {} installed toolchain(s) to the new local state format",
"info".white().bold(),
channels.len()
);
for channel in channels {
println!(
"- {channel} will be reinstalled the next time it is used, so that midenup knows \
exactly what it owns"
);
}
}
/// Completes or discards whatever the previous run left behind, before anything else happens.
///
/// A new operation must never be planned against a half-published `MIDENUP_HOME`, so this runs
/// ahead of every command, including `miden` dispatch.
///
/// Divergence -- a state record whose publication is not on disk -- is *reported* here rather than
/// being fatal. It is a genuine error (spec section 14.3) and is never guessed at or silently
/// repaired, but the remediation it names is itself a midenup command: making it fatal at startup
/// would leave the user with a diagnostic they cannot act on. The operation that actually needs
/// the missing files fails on its own terms.
fn recover(config: &config::Config, state: &mut crate::state::LocalState) -> anyhow::Result<()> {
use colored::Colorize;
// Recovery mutates, so it takes the lock -- but only when there is something to recover.
// Taking it unconditionally would put every `miden` invocation behind it, and the read-only
// dispatch path is required to stay lock-free.
let _lock = match crate::publish::journal::read(&config.midenup_home)? {
Some(_) => {
let lock = crate::lock::acquire(&config.midenup_home)?;
// Whoever held the lock may already have completed this recovery.
*state = config.local_state()?;
Some(lock)
},
None => None,
};
match crate::publish::journal::recover(&config.midenup_home, state) {
Ok(None) => {},
Ok(Some(operation)) => {
println!("{}: recovered an interrupted {operation}", "info".white().bold());
},
Err(err @ crate::publish::PublishError::DivergentState { .. }) => {
eprintln!("{}: {err}", "warning".yellow().bold());
},
Err(err) => return Err(err.into()),
}
Ok(())
}
fn get_full_command(argv: &[OsString]) -> String {
use core::fmt::Write;
let mut out = String::with_capacity(256);
for (i, arg) in argv.iter().enumerate() {
if i > 0 {
out.push(' ');
}
write!(&mut out, "{}", arg.display()).unwrap();
}
out
}