Skip to content

Commit

Permalink
fix: add backend to lockfile
Browse files Browse the repository at this point in the history
Fixes #3339
  • Loading branch information
jdx committed Dec 14, 2024
1 parent 9cf3c1c commit ef5f0c7
Show file tree
Hide file tree
Showing 5 changed files with 81 additions and 8 deletions.
8 changes: 6 additions & 2 deletions e2e/cli/test_use_latest
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,11 @@ touch mise.lock
assert "mise use dummy@1 tiny@1"
assert "cat mise.lock" '[tools.dummy]
version = "1.0.0"
backend = "asdf:dummy"
[tools.tiny]
version = "1.0.0"'
version = "1.0.0"
backend = "asdf:tiny"'
assert "mise tool dummy --requested" "1"
assert "mise tool tiny --requested" "1"
assert "mise tool dummy --active" "1.0.0"
Expand All @@ -29,9 +31,11 @@ assert "mise tool tiny --active" "1.0.0"
assert "mise use dummy@latest"
assert "cat mise.lock" '[tools.dummy]
version = "2.0.0"
backend = "asdf:dummy"
[tools.tiny]
version = "1.0.0"'
version = "1.0.0"
backend = "asdf:tiny"'
assert "mise tool dummy --requested" "latest"
assert "mise tool tiny --requested" "1"
assert "mise tool dummy --active" "2.0.0"
Expand Down
16 changes: 16 additions & 0 deletions e2e/lockfile/test_lockfile_backend
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env bash

export MISE_LOCKFILE=1
export MISE_EXPERIMENTAL=1

assert "mise tool gh --backend" "aqua:cli/cli"
cat <<EOF >mise.toml
[tools.gh]
version = "1.0.0"
EOF
cat <<EOF >mise.lock
[tools.gh]
version = "1.0.0"
backend = "ubi:cli/cli[exe=gh]"
EOF
assert "mise tool gh --backend" "ubi:cli/cli[exe=gh]"
16 changes: 11 additions & 5 deletions e2e/lockfile/test_lockfile_use
Original file line number Diff line number Diff line change
Expand Up @@ -12,33 +12,39 @@ assert "mise where tiny" "$MISE_DATA_DIR/installs/tiny/1.0.0"
assert "mise ls tiny --json --current | jq -r '.[0].requested_version'" "1"
assert "mise ls tiny --json --current | jq -r '.[0].version'" "1.0.0"
assert "cat mise.lock" '[tools.tiny]
version = "1.0.0"'
version = "1.0.0"
backend = "asdf:tiny"'

assert "mise use tiny@1"
assert "cat mise.lock" '[tools.tiny]
version = "1.0.0"'
version = "1.0.0"
backend = "asdf:tiny"'
assert "mise ls tiny --json --current | jq -r '.[0].requested_version'" "1"
assert "mise ls tiny --json --current | jq -r '.[0].version'" "1.0.0"

assert "mise up tiny"
assert "cat mise.lock" '[tools.tiny]
version = "1.1.0"'
version = "1.1.0"
backend = "asdf:tiny"'
assert "mise ls tiny --json --current | jq -r '.[0].requested_version'" "1"
assert "mise ls tiny --json --current | jq -r '.[0].version'" "1.1.0"

assert "mise up tiny --bump"
assert "cat mise.lock" '[tools.tiny]
version = "3.1.0"'
version = "3.1.0"
backend = "asdf:tiny"'
assert "mise ls tiny --json --current | jq -r '.[0].requested_version'" "3"
assert "mise ls tiny --json --current | jq -r '.[0].version'" "3.1.0"

echo 'tools.tiny = "1.0.0"' >mise.lock
assert "mise use tiny@1 tiny@2"
assert "cat mise.lock" '[[tools.tiny]]
version = "1.0.0"
backend = "asdf:tiny"
[[tools.tiny]]
version = "2.1.0"'
version = "2.1.0"
backend = "asdf:tiny"'
assert "mise uninstall --all tiny"
assert "mise install tiny"
assert "mise ls tiny --json --current | jq -r '.[0].requested_version'" "1"
Expand Down
7 changes: 6 additions & 1 deletion src/cli/args/backend_arg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::plugins::PluginType;
use crate::registry::REGISTRY;
use crate::toolset::install_state::InstallStateTool;
use crate::toolset::{install_state, parse_tool_options, ToolVersionOptions};
use crate::{backend, config, dirs, registry};
use crate::{backend, config, dirs, lockfile, registry};
use contracts::requires;
use eyre::{bail, Result};
use heck::ToKebabCase;
Expand Down Expand Up @@ -139,6 +139,11 @@ impl BackendArg {
deprecated!("config_plugins", "[plugins] section of mise.toml is deprecated. Use [alias] instead. https://mise.jdx.dev/dev-tools/aliases.html");
return format!("asdf:{url}");
}
if let Some(lt) = lockfile::get_locked_version(None, short, "").unwrap_or_default() {
if let Some(backend) = lt.backend {
return backend;
}
}
}
if let Some(full) = &self.full {
full.clone()
Expand Down
42 changes: 42 additions & 0 deletions src/lockfile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde_derive::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::path::{Path, PathBuf};
use std::sync::Mutex;
use toml_edit::DocumentMut;

#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
Expand All @@ -21,6 +22,7 @@ pub struct Lockfile {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockfileTool {
pub version: String,
pub backend: Option<String>,
#[serde(skip_serializing_if = "BTreeMap::is_empty")]
pub checksums: BTreeMap<String, String>,
}
Expand Down Expand Up @@ -69,6 +71,7 @@ impl Lockfile {
let mut lockfile = toml::Table::new();
lockfile.insert("tools".to_string(), tools.into());
let content = toml::to_string_pretty(&toml::Value::Table(lockfile))?;
let content = format(content.parse()?);
file::write(path, content)?;
}
Ok(())
Expand Down Expand Up @@ -241,6 +244,7 @@ impl TryFrom<toml::Value> for LockfileTool {
let tool = match value {
toml::Value::String(v) => LockfileTool {
version: v,
backend: Default::default(),
checksums: Default::default(),
},
toml::Value::Table(mut t) => {
Expand All @@ -257,6 +261,11 @@ impl TryFrom<toml::Value> for LockfileTool {
.map(|v| v.try_into())
.transpose()?
.unwrap_or_default(),
backend: t
.remove("backend")
.map(|v| v.try_into())
.transpose()?
.unwrap_or_default(),
checksums,
}
}
Expand All @@ -270,6 +279,9 @@ impl LockfileTool {
fn into_toml_value(self) -> toml::Value {
let mut table = toml::Table::new();
table.insert("version".to_string(), self.version.into());
if let Some(backend) = self.backend {
table.insert("backend".to_string(), backend.into());
}
if !self.checksums.is_empty() {
table.insert("checksums".to_string(), self.checksums.into());
}
Expand All @@ -283,8 +295,38 @@ impl From<ToolVersionList> for Vec<LockfileTool> {
.iter()
.map(|tv| LockfileTool {
version: tv.version.clone(),
backend: Some(tv.ba().full()),
checksums: tv.checksums.clone(),
})
.collect()
}
}

fn format(mut doc: DocumentMut) -> String {
if let Some(tools) = doc.get_mut("tools") {
for (_k, v) in tools.as_table_mut().unwrap().iter_mut() {
match v {
toml_edit::Item::ArrayOfTables(art) => {
for t in art.iter_mut() {
t.sort_values_by(|a, _, b, _| {
if a == "version" {
return std::cmp::Ordering::Less;
}
a.to_string().cmp(&b.to_string())
});
}
}
toml_edit::Item::Table(t) => {
t.sort_values_by(|a, _, b, _| {
if a == "version" {
return std::cmp::Ordering::Less;
}
a.to_string().cmp(&b.to_string())
});
}
_ => {}
}
}
}
doc.to_string()
}

0 comments on commit ef5f0c7

Please sign in to comment.