Skip to content

Commit

Permalink
config: added rwlock around plugins map (#1143)
Browse files Browse the repository at this point in the history
* config: added rwlock around plugins map

* Commit from GitHub Actions (rtx)

* reduce write locking

---------

Co-authored-by: rtx[bot] <[email protected]>
  • Loading branch information
jdx and mise-en-dev authored Dec 12, 2023
1 parent b0e668f commit 360491c
Show file tree
Hide file tree
Showing 42 changed files with 192 additions and 232 deletions.
4 changes: 2 additions & 2 deletions src/cli/asdf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,15 @@ impl Asdf {
}
}

fn list_versions(mut config: Config, out: &mut Output, args: &Vec<String>) -> Result<()> {
fn list_versions(config: Config, out: &mut Output, args: &Vec<String>) -> Result<()> {
if args[2] == "all" {
let mut new_args: Vec<String> = vec!["rtx".into(), "ls-remote".into()];
if args.len() >= 3 {
new_args.push(args[3].clone());
}
return Cli::new().run(config, &new_args, out);
}
let ts = ToolsetBuilder::new().build(&mut config)?;
let ts = ToolsetBuilder::new().build(&config)?;
let mut versions = ts.list_installed_versions(&config)?;
let plugin = match args.len() {
3 => Some(&args[2]),
Expand Down
4 changes: 2 additions & 2 deletions src/cli/bin_paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ use crate::toolset::ToolsetBuilder;
pub struct BinPaths {}

impl BinPaths {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;
for p in ts.list_paths(&config) {
rtxprintln!(out, "{}", p.display());
}
Expand Down
14 changes: 6 additions & 8 deletions src/cli/current.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,16 @@ pub struct Current {
}

impl Current {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;
match &self.plugin {
Some(plugin_name) => {
let plugin_name = unalias_plugin(plugin_name);
match config.plugins.get(plugin_name) {
Some(plugin) => self.one(&config, ts, out, plugin.clone()),
None => {
warn!("Plugin {} is not installed", plugin_name);
Ok(())
}
let plugin = config.get_or_create_plugin(plugin_name);
if !plugin.is_installed() {
bail!("Plugin {} is not installed", plugin_name);
}
self.one(&config, ts, out, plugin.clone())
}
None => self.all(&config, ts, out),
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/direnv/envrc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ use crate::{dirs, env};
pub struct Envrc {}

impl Envrc {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;

let envrc_path = env::RTX_TMP_DIR
.join("direnv")
Expand Down
4 changes: 2 additions & 2 deletions src/cli/direnv/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ struct DirenvWatches {
}

impl DirenvExec {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;

let mut cmd = env_cmd();

Expand Down
10 changes: 5 additions & 5 deletions src/cli/doctor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ use crate::{duration, env};
pub struct Doctor {}

impl Doctor {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;
rtxprintln!(out, "{}", rtx_version());
rtxprintln!(out, "{}", build_info());
rtxprintln!(out, "{}", shell());
Expand All @@ -45,7 +45,7 @@ impl Doctor {
);

let mut checks = Vec::new();
for plugin in config.plugins.values() {
for plugin in config.list_plugins() {
if !plugin.is_installed() {
checks.push(format!("plugin {} is not installed", &plugin.name()));
continue;
Expand Down Expand Up @@ -123,8 +123,8 @@ fn render_config_files(config: &Config) -> String {
fn render_plugins(config: &Config) -> String {
let mut s = style("plugins:\n").bold().to_string();
let plugins = config
.plugins
.values()
.list_plugins()
.into_iter()
.filter(|p| p.is_installed())
.collect::<Vec<_>>();
let max_plugin_name_len = plugins.iter().map(|p| p.name().len()).max().unwrap_or(0) + 2;
Expand Down
8 changes: 3 additions & 5 deletions src/cli/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,9 @@ pub struct Env {
}

impl Env {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new()
.with_args(&self.tool)
.build(&mut config)?;
ts.install_arg_versions(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new().with_args(&self.tool).build(&config)?;
ts.install_arg_versions(&config)?;

if self.json {
self.output_json(config, out, ts)
Expand Down
8 changes: 3 additions & 5 deletions src/cli/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,9 @@ pub struct Exec {
}

impl Exec {
pub fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new()
.with_args(&self.tool)
.build(&mut config)?;
ts.install_arg_versions(&mut config)?;
pub fn run(self, config: Config, _out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new().with_args(&self.tool).build(&config)?;
ts.install_arg_versions(&config)?;

let (program, args) = parse_command(&env::SHELL, &self.command, &self.c);
let env = ts.env_with_path(&config);
Expand Down
21 changes: 7 additions & 14 deletions src/cli/external.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,19 @@
use clap::{ArgMatches, Command};
use color_eyre::eyre::Result;
use itertools::Itertools;
use rayon::prelude::*;

use crate::config::Config;

pub fn commands(config: &Config) -> Vec<Command> {
config
.plugins
.values()
.collect_vec()
.list_plugins()
.into_par_iter()
.flat_map(|p| match p.external_commands() {
Ok(commands) => commands,
Err(e) => {
warn!(
"failed to load external commands for plugin {}: {:#}",
p.name(),
e
);
.flat_map(|p| {
p.external_commands().unwrap_or_else(|e| {
let p = p.name();
warn!("failed to load external commands for plugin {p}: {e:#}");
vec![]
}
})
})
.collect()
}
Expand All @@ -36,7 +29,7 @@ pub fn execute(
.find(|c| c.get_name() == plugin)
{
if let Some((subcommand, matches)) = args.subcommand() {
let plugin = config.plugins.get(&plugin.to_string()).unwrap();
let plugin = config.get_or_create_plugin(plugin);
let args: Vec<String> = matches
.get_raw("args")
.unwrap_or_default()
Expand Down
4 changes: 2 additions & 2 deletions src/cli/hook_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ pub struct HookEnv {
}

impl HookEnv {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let ts = ToolsetBuilder::new().build(&config)?;
let shell = get_shell(self.shell).expect("no shell provided, use `--shell=zsh`");
out.stdout.write(hook_env::clear_old_env(&*shell));
let mut env = ts.env(&config);
Expand Down
16 changes: 8 additions & 8 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,23 +42,23 @@ impl Install {

Ok(())
}
fn install_runtimes(&self, mut config: Config, runtimes: &[ToolArg]) -> Result<()> {
fn install_runtimes(&self, config: Config, runtimes: &[ToolArg]) -> Result<()> {
let mpr = MultiProgressReport::new(&config.settings);
let mut ts = ToolsetBuilder::new()
.with_latest_versions()
.build(&mut config)?;
let tool_versions = self.get_requested_tool_versions(&mut config, &ts, runtimes, &mpr)?;
.build(&config)?;
let tool_versions = self.get_requested_tool_versions(&config, &ts, runtimes, &mpr)?;
if tool_versions.is_empty() {
warn!("no runtimes to install");
warn!("specify a version with `rtx install <PLUGIN>@<VERSION>`");
return Ok(());
}
ts.install_versions(&mut config, tool_versions, &mpr, self.force)
ts.install_versions(&config, tool_versions, &mpr, self.force)
}

fn get_requested_tool_versions(
&self,
config: &mut Config,
config: &Config,
ts: &Toolset,
runtimes: &[ToolArg],
mpr: &MultiProgressReport,
Expand Down Expand Up @@ -102,10 +102,10 @@ impl Install {
Ok(tool_versions)
}

fn install_missing_runtimes(&self, mut config: Config) -> Result<()> {
fn install_missing_runtimes(&self, config: Config) -> Result<()> {
let mut ts = ToolsetBuilder::new()
.with_latest_versions()
.build(&mut config)?;
.build(&config)?;
let versions = ts
.list_missing_versions(&config)
.into_iter()
Expand All @@ -116,7 +116,7 @@ impl Install {
return Ok(());
}
let mpr = MultiProgressReport::new(&config.settings);
ts.install_versions(&mut config, versions, &mpr, self.force)?;
ts.install_versions(&config, versions, &mpr, self.force)?;
Ok(())
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/cli/latest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub struct Latest {
}

impl Latest {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let mut prefix = match self.tool.tvr {
None => self.asdf_version,
Some(ToolVersionRequest::Version(_, version)) => Some(version),
Expand All @@ -37,7 +37,7 @@ impl Latest {
};

let plugin = config.get_or_create_plugin(&self.tool.plugin);
plugin.ensure_installed(&mut config, None, false)?;
plugin.ensure_installed(&config, None, false)?;
if let Some(v) = prefix {
prefix = Some(config.resolve_alias(plugin.name(), &v)?);
}
Expand Down
2 changes: 1 addition & 1 deletion src/cli/link.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ pub struct Link {
}

impl Link {
pub fn run(self, mut config: Config, _out: &mut Output) -> Result<()> {
pub fn run(self, config: Config, _out: &mut Output) -> Result<()> {
let version = match self.tool.tvr {
Some(ref tvr) => tvr.version(),
None => {
Expand Down
4 changes: 2 additions & 2 deletions src/cli/local.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ pub fn get_parent_path() -> Result<PathBuf> {

#[allow(clippy::too_many_arguments)]
pub fn local(
mut config: Config,
config: Config,
out: &mut Output,
path: &Path,
runtime: Vec<ToolArg>,
Expand Down Expand Up @@ -128,7 +128,7 @@ pub fn local(
return Ok(());
}
let pin = pin || (config.settings.asdf_compat && !fuzzy);
cf.add_runtimes(&mut config, &runtimes, pin)?;
cf.add_runtimes(&config, &runtimes, pin)?;
let tools = runtimes.iter().map(|r| r.to_string()).join(" ");
rtxprintln!(
out,
Expand Down
15 changes: 9 additions & 6 deletions src/cli/ls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ pub struct Ls {
}

impl Ls {
pub fn run(mut self, mut config: Config, out: &mut Output) -> Result<()> {
pub fn run(mut self, config: Config, out: &mut Output) -> Result<()> {
self.plugin = self
.plugin
.clone()
.or(self.plugin_flag.clone())
.map(|p| PluginName::from(unalias_plugin(&p)));
self.verify_plugin(&config)?;

let mut runtimes = self.get_runtime_list(&mut config)?;
let mut runtimes = self.get_runtime_list(&config)?;
if self.current || self.global {
// TODO: global is a little weird: it will show global versions as the active ones even if
// they're overridden locally
Expand All @@ -94,8 +94,8 @@ impl Ls {
fn verify_plugin(&self, config: &Config) -> Result<()> {
match &self.plugin {
Some(plugin_name) => {
let plugin = config.plugins.get(plugin_name);
if plugin.is_none() || !plugin.unwrap().is_installed() {
let plugin = config.get_or_create_plugin(plugin_name);
if !plugin.is_installed() {
return Err(PluginNotInstalled(plugin_name.clone()))?;
}
}
Expand Down Expand Up @@ -209,12 +209,11 @@ impl Ls {
Ok(())
}

fn get_runtime_list(&self, config: &mut Config) -> Result<Vec<RuntimeRow>> {
fn get_runtime_list(&self, config: &Config) -> Result<Vec<RuntimeRow>> {
let mut tsb = ToolsetBuilder::new().with_global_only(self.global);

if let Some(plugin) = &self.plugin {
tsb = tsb.with_tools(&[plugin]);
config.plugins.retain(|p, _| p == plugin);
}
let ts = tsb.build(config)?;
let mut versions: HashMap<(String, String), (Arc<dyn Plugin>, ToolVersion)> = ts
Expand All @@ -233,6 +232,10 @@ impl Ls {

let rvs: Vec<RuntimeRow> = versions
.into_iter()
.filter(|((plugin_name, _), _)| match &self.plugin {
Some(p) => p.eq(plugin_name),
None => true,
})
.sorted_by_cached_key(|((plugin_name, version), _)| {
(
plugin_name.clone(),
Expand Down
11 changes: 5 additions & 6 deletions src/cli/ls_remote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ pub struct LsRemote {
}

impl LsRemote {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
if let Some(plugin) = self.get_plugin(&mut config)? {
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
if let Some(plugin) = self.get_plugin(&config)? {
self.run_single(config, out, plugin)
} else {
self.run_all(config, out)
Expand Down Expand Up @@ -68,9 +68,8 @@ impl LsRemote {

fn run_all(self, config: Config, out: &mut Output) -> Result<()> {
let versions = config
.plugins
.values()
.par_bridge()
.list_plugins()
.into_par_iter()
.map(|p| {
let versions = p.list_remote_versions(&config.settings)?;
Ok((p, versions))
Expand All @@ -87,7 +86,7 @@ impl LsRemote {
Ok(())
}

fn get_plugin(&self, config: &mut Config) -> Result<Option<Arc<dyn Plugin>>> {
fn get_plugin(&self, config: &Config) -> Result<Option<Arc<dyn Plugin>>> {
match &self.plugin {
Some(tool_arg) => {
let plugin = config.get_or_create_plugin(&tool_arg.plugin);
Expand Down
6 changes: 2 additions & 4 deletions src/cli/outdated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,8 @@ pub struct Outdated {
}

impl Outdated {
pub fn run(self, mut config: Config, out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new()
.with_args(&self.tool)
.build(&mut config)?;
pub fn run(self, config: Config, out: &mut Output) -> Result<()> {
let mut ts = ToolsetBuilder::new().with_args(&self.tool).build(&config)?;
let tool_set = self
.tool
.iter()
Expand Down
Loading

0 comments on commit 360491c

Please sign in to comment.