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
use std::{path::PathBuf, sync::Arc};
use crate::cli::args::ToolArg;
use crate::cli::prune::prune;
use crate::config::config_file::ConfigFile;
use crate::config::{Config, config_file};
use crate::file::display_path;
use crate::{config, env};
use eyre::Result;
use itertools::Itertools;
use path_absolutize::Absolutize;
/// Removes installed tool versions from mise.toml
///
/// By default, this will use the `mise.toml` file that has the tool defined.
/// If multiple config files exist (e.g., both `mise.toml` and `mise.local.toml`),
/// the lowest precedence file (`mise.toml`) will be used.
/// See https://mise.en.dev/configuration.html#target-file-for-write-operations
///
/// In the following order:
/// - If `--global` is set, it will use the global config file.
/// - If `--path` is set, it will use the config file at the given path.
/// - If `--env` is set, it will use `mise.<env>.toml`.
/// - If [`MISE_DEFAULT_CONFIG_FILENAME`](https://mise.en.dev/configuration.html#mise_default_config_filename) is set, it will use that instead.
/// - If `MISE_OVERRIDE_CONFIG_FILENAMES` is set, it will the first from that list.
/// - Otherwise just "mise.toml" or global config if cwd is home directory.
///
/// Use [`MISE_GLOBAL_CONFIG_FILE`](https://mise.en.dev/configuration.html#mise_global_config_file) to choose a different global config path.
///
/// Will also prune the installed version if no other configurations are using it.
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, visible_aliases = ["rm", "remove"], after_long_help = AFTER_LONG_HELP)]
pub struct Unuse {
/// Tool(s) to remove
#[clap(value_name = "INSTALLED_TOOL@VERSION", required = true)]
installed_tool: Vec<ToolArg>,
/// Create/modify an environment-specific config file like .mise.<env>.toml
#[clap(long, short, overrides_with_all = & ["global", "path"])]
env: Option<String>,
/// Use the global config file (`~/.config/mise/config.toml`) instead of the local one
#[clap(short, long, overrides_with_all = & ["path", "env"])]
global: bool,
/// Specify a path to a config file or directory
///
/// If a directory is specified, it will look for a config file in that directory following
/// the rules above.
#[clap(short, long, overrides_with_all = & ["global", "env"], value_hint = clap::ValueHint::FilePath)]
path: Option<PathBuf>,
/// Do not also prune the installed version
#[clap(long)]
no_prune: bool,
}
impl Unuse {
pub async fn run(self) -> Result<()> {
let config = Config::get().await?;
let cf = self.get_config_file(&config).await?;
let tools = cf.to_tool_request_set()?.tools;
let mut removed: Vec<&ToolArg> = vec![];
for ta in &self.installed_tool {
if let Some(tool_requests) = tools.get(ta.ba.as_ref()) {
let should_remove = if let Some(v) = &ta.version {
tool_requests.iter().any(|tv| &tv.version() == v)
} else {
true
};
// TODO: this won't work properly for unusing a specific version in of multiple in a config
if should_remove {
removed.push(ta);
cf.remove_tool(&ta.ba)?;
}
}
}
if removed.is_empty() {
debug!("no tools to remove");
} else {
cf.save()?;
let removals = removed.iter().join(", ");
info!("removed: {removals} from {}", display_path(cf.get_path()));
}
if !self.no_prune {
prune(
&config,
self.installed_tool
.iter()
.map(|ta| ta.ba.as_ref())
.collect(),
false,
)
.await?;
let config = Config::reset().await?;
let ts = config.get_toolset().await?;
config::rebuild_shims_and_runtime_symlinks(
&config,
ts,
&[],
crate::lockfile::LockfileUpdateMode::Normal,
)
.await?;
}
Ok(())
}
async fn get_config_file(&self, config: &Config) -> Result<Arc<dyn ConfigFile>> {
let cwd = env::current_dir()?;
let path = if self.global {
config::global_config_path()
} else if let Some(p) = &self.path {
let from_dir = config::config_file_from_dir(p).absolutize()?.to_path_buf();
if from_dir.starts_with(&cwd) {
from_dir
} else {
p.clone()
}
} else if let Some(env) = &self.env {
let p = cwd.join(format!(".mise.{env}.toml"));
if p.exists() {
p
} else {
cwd.join(format!("mise.{env}.toml"))
}
} else if env::in_home_dir() {
config::global_config_path()
} else {
for cf in config.config_files.values() {
if cf
.to_tool_request_set()?
.tools
.keys()
.any(|ba| self.installed_tool.iter().any(|ta| &ta.ba == ba))
{
return config_file::parse(cf.get_path()).await;
}
}
config::local_toml_config_path()
};
config_file::parse_or_init(&path).await
}
}
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
# will uninstall specific version
$ <bold>mise unuse node@18.0.0</bold>
# will uninstall specific version from global config
$ <bold>mise unuse -g node@18.0.0</bold>
# will uninstall specific version from .mise.local.toml
$ <bold>mise unuse --env local node@20</bold>
# will uninstall specific version from .mise.staging.toml
$ <bold>mise unuse --env staging node@20</bold>
"#
);