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 12 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
2 changes: 1 addition & 1 deletion mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ backend = "aqua:BurntSushi/ripgrep"

[tools.shellcheck]
version = "0.10.0"
backend = "aqua:koalaman/shellcheck"
backend = "ubi:koalaman/shellcheck"

[tools.shellcheck.checksums]
"shellcheck-v0.10.0.darwin.aarch64.tar.xz" = "sha256:bbd2f14826328eee7679da7221f2bc3afb011f6a928b848c80c321f6046ddf81"
Expand Down
36 changes: 24 additions & 12 deletions src/cli/settings/set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ 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 parsed_value = if let Some(meta) = SETTINGS_META.get(key) {
match meta.type_ {
SettingsType::Bool => parse_bool(value)?,
SettingsType::Integer => parse_i64(value)?,
Expand All @@ -46,16 +46,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 +68,30 @@ 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()
let new_value: toml_edit::Value = if add {
if let Some(existing_arr) = settings.get(key).and_then(|item| item.as_array()).cloned()
{
let mut arr = existing_arr;
// Dedupe by comparing to the original &str
if !arr.iter().any(|it| it.as_str() == Some(value)) {
arr.push(value);
}
toml_edit::Value::Array(arr)
} else {
parsed_value.clone()
}
_ => value,
} 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