Skip to content

fix: duplicate entries when running mise settings add idiomatic_version_file_enable_tools #4925

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 22 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions e2e/cli/test_settings_add
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,6 @@
assert "mise settings add disable_hints a" ""
assert "mise settings add disable_hints b" ""
assert "mise settings get disable_hints" '["a", "b"]'

assert "mise settings add disable_hints a" "" # mise should ignore identical values which have been passed using `add`
assert "mise settings get disable_hints" '["a", "b"]'
50 changes: 38 additions & 12 deletions src/cli/settings/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ impl SettingsSet {
}

pub fn set(mut key: &str, value: &str, add: bool, local: bool) -> Result<()> {
let value = if let Some(meta) = SETTINGS_META.get(key) {
let meta = SETTINGS_META.get(key);

let parsed_value = if let Some(meta) = meta {
match meta.type_ {
SettingsType::Bool => parse_bool(value)?,
SettingsType::Integer => parse_i64(value)?,
Expand All @@ -46,16 +48,19 @@ pub fn set(mut key: &str, value: &str, add: bool, local: bool) -> Result<()> {
config::global_config_path()
};
file::create_dir_all(path.parent().unwrap())?;

let raw = file::read_to_string(&path).unwrap_or_default();
let mut config: DocumentMut = raw.parse()?;

if !config.contains_key("settings") {
config["settings"] = toml_edit::Item::Table(toml_edit::Table::new());
}

if let Some(mut settings) = config["settings"].as_table_mut() {
if let Some((parent_key, child_key)) = key.split_once('.') {
key = child_key;
if let Some((parent, child)) = key.split_once('.') {
key = child;
settings = settings
.entry(parent_key)
.entry(parent)
.or_insert({
let mut t = toml_edit::Table::new();
t.set_implicit(true);
Expand All @@ -65,21 +70,42 @@ pub fn set(mut key: &str, value: &str, add: bool, local: bool) -> Result<()> {
.unwrap();
}

let value = match settings.get(key).map(|c| c.as_array()) {
Some(Some(array)) if add => {
let mut array = array.clone();
array.extend(value.as_array().unwrap().iter().cloned());
array.into()
// If we're in “add” mode and the key already exists as an array
// and already contains our value, just do nothing.
if add {
if let Some(toml_edit::Value::Array(arr)) =
settings.get(key).and_then(|it| it.as_value().cloned())
{
if arr.iter().any(|it| it.as_str() == Some(value)) {
return Ok(());
}
}
_ => value,
}

let new_value: toml_edit::Value = if add {
if let Some(existing_arr) = settings.get(key).and_then(|it| it.as_array()).cloned() {
let mut arr = existing_arr;
// dedupe: only push if it’s not already in the array
if !arr.iter().any(|it| it.as_str() == Some(value)) {
arr.push(value);
}
toml_edit::Value::Array(arr)
} else {
// first time: parsed_value is already an array (ListString/ListPath)
parsed_value.clone()
}
} else {
parsed_value.clone()
};
settings.insert(key, value.into());

settings.insert(key, new_value.into());

// validate
let _: SettingsFile = toml::from_str(&config.to_string())?;

file::write(path, config.to_string())?;
file::write(&path, config.to_string())?;
}

Ok(())
}

Expand Down