Skip to content

Commit

Permalink
fix: respect lockfile on mise install
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Nov 4, 2024
1 parent 5b1d306 commit 577aefd
Show file tree
Hide file tree
Showing 13 changed files with 84 additions and 32 deletions.
14 changes: 14 additions & 0 deletions e2e/lockfile/test_lockfile_install
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#!/usr/bin/env bash

export MISE_LOCKFILE=1
export MISE_EXPERIMENTAL=1

mise use tiny@1
cat <<EOF > .mise.lock
[tools]
tiny = "1.0.0"
EOF
rm -rf "$MISE_DATA_DIR/installs/tiny"
mise install
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"
2 changes: 1 addition & 1 deletion src/cli/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Exec {
force: false,
jobs: self.jobs,
raw: self.raw,
latest_versions: false,
resolve_options: Default::default(),
};
ts.install_arg_versions(&config, &opts)?;
ts.notify_if_versions_missing();
Expand Down
9 changes: 7 additions & 2 deletions src/cli/install.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use crate::cli::args::{BackendArg, ToolArg};
use crate::config::Config;
use crate::lockfile;
use crate::toolset::{InstallOptions, ToolRequest, ToolSource, ToolVersion, Toolset};
use crate::toolset::{
InstallOptions, ResolveOptions, ToolRequest, ToolSource, ToolVersion, Toolset,
};
use crate::ui::multi_progress_report::MultiProgressReport;
use eyre::{Result, WrapErr};
use itertools::Itertools;
Expand Down Expand Up @@ -75,7 +77,10 @@ impl Install {
force: self.force,
jobs: self.jobs,
raw: self.raw,
latest_versions: true,
resolve_options: ResolveOptions {
use_locked_version: true,
latest_versions: true,
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/cli/shell.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl Shell {
force: false,
jobs: self.jobs,
raw: self.raw,
latest_versions: false,
resolve_options: Default::default(),
};
ts.install_arg_versions(&config, &opts)?;
ts.notify_if_versions_missing();
Expand Down
5 changes: 4 additions & 1 deletion src/cli/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,10 @@ impl Uninstall {
})
.collect::<Result<Vec<_>>>()?;
if let Some(tvr) = &a.tvr {
tvs.push((tool.clone(), tvr.resolve(tool.as_ref(), false)?));
tvs.push((
tool.clone(),
tvr.resolve(tool.as_ref(), &Default::default())?,
));
}
if tvs.is_empty() {
warn!("no versions found for {}", style(&tool).blue().for_stderr());
Expand Down
7 changes: 5 additions & 2 deletions src/cli/upgrade.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::cli::args::ToolArg;
use crate::config::{config_file, Config};
use crate::file::display_path;
use crate::toolset::{InstallOptions, OutdatedInfo, ToolVersion, ToolsetBuilder};
use crate::toolset::{InstallOptions, OutdatedInfo, ResolveOptions, ToolVersion, ToolsetBuilder};
use crate::ui::multi_progress_report::MultiProgressReport;
use crate::ui::progress_report::SingleReport;
use crate::{lockfile, runtime_symlinks, shims, ui};
Expand Down Expand Up @@ -141,7 +141,10 @@ impl Upgrade {
force: false,
jobs: self.jobs,
raw: self.raw,
latest_versions: true,
resolve_options: ResolveOptions {
use_locked_version: false,
latest_versions: true,
},
};
let new_versions = outdated.iter().map(|o| o.tool_request.clone()).collect();
let versions = ts.install_versions(config, new_versions, &mpr, &opts)?;
Expand Down
2 changes: 1 addition & 1 deletion src/cli/use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl Use {
force: self.force,
jobs: self.jobs,
raw: self.raw,
latest_versions: false,
resolve_options: Default::default(),
},
)?;

Expand Down
2 changes: 1 addition & 1 deletion src/cli/where.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ impl Where {

let ba = tvr.backend();
let backend = backend::get(ba);
let tv = tvr.resolve(backend.as_ref(), false)?;
let tv = tvr.resolve(backend.as_ref(), &Default::default())?;

if backend.is_version_installed(&tv, true) {
miseprintln!("{}", tv.install_path().to_string_lossy());
Expand Down
2 changes: 1 addition & 1 deletion src/config/config_file/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ impl dyn ConfigFile {
.map(|tvr| {
if pin {
let plugin = backend::get(&fa);
let tv = tvr.resolve(plugin.as_ref(), false)?;
let tv = tvr.resolve(plugin.as_ref(), &Default::default())?;
Ok(tv.version)
} else {
Ok(tvr.version())
Expand Down
10 changes: 5 additions & 5 deletions src/toolset/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use tabled::Tabled;
pub use tool_request::ToolRequest;
pub use tool_request_set::{ToolRequestSet, ToolRequestSetBuilder};
pub use tool_source::ToolSource;
pub use tool_version::ToolVersion;
pub use tool_version::{ResolveOptions, ToolVersion};
pub use tool_version_list::ToolVersionList;
use versions::{Version, Versioning};
use xx::regex;
Expand Down Expand Up @@ -58,7 +58,7 @@ pub struct InstallOptions {
pub force: bool,
pub jobs: Option<usize>,
pub raw: bool,
pub latest_versions: bool,
pub resolve_options: ResolveOptions,
}

impl InstallOptions {
Expand Down Expand Up @@ -118,7 +118,7 @@ impl Toolset {
.iter_mut()
.collect::<Vec<_>>()
.par_iter_mut()
.map(|(_, v)| v.resolve(false))
.map(|(_, v)| v.resolve(&Default::default()))
.filter(|r| r.is_err())
.map(|r| r.unwrap_err())
.collect::<Vec<_>>();
Expand Down Expand Up @@ -225,7 +225,7 @@ impl Toolset {
sleep(Duration::from_millis(100));
}
}
let tv = tr.resolve(t.as_ref(), opts.latest_versions)?;
let tv = tr.resolve(t.as_ref(), &opts.resolve_options)?;
let ctx = InstallContext {
ts,
pr: mpr.add(&tv.style()),
Expand Down Expand Up @@ -284,7 +284,7 @@ impl Toolset {
Some((p, tv)) => Ok((p.clone(), tv.clone())),
None => {
let tv = ToolRequest::new(p.fa().clone(), &v, ToolSource::Unknown)?
.resolve(p.as_ref(), false)
.resolve(p.as_ref(), &Default::default())
.unwrap();
Ok((p.clone(), tv))
}
Expand Down
5 changes: 3 additions & 2 deletions src/toolset/tool_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use xx::file;
use crate::backend::Backend;
use crate::cli::args::BackendArg;
use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::tool_version::ResolveOptions;
use crate::toolset::{ToolSource, ToolVersion, ToolVersionOptions};
use crate::{backend, lockfile};

Expand Down Expand Up @@ -225,8 +226,8 @@ impl ToolRequest {
Ok(None)
}

pub fn resolve(&self, plugin: &dyn Backend, latest_versions: bool) -> Result<ToolVersion> {
ToolVersion::resolve(plugin, self.clone(), latest_versions)
pub fn resolve(&self, plugin: &dyn Backend, opts: &ResolveOptions) -> Result<ToolVersion> {
ToolVersion::resolve(plugin, self.clone(), opts)
}
}

Expand Down
41 changes: 30 additions & 11 deletions src/toolset/tool_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,9 @@ impl ToolVersion {
pub fn resolve(
backend: &dyn Backend,
request: ToolRequest,
latest_versions: bool,
opts: &ResolveOptions,
) -> Result<Self> {
if !latest_versions {
if opts.use_locked_version {
if let Some(v) = request.lockfile_resolve()? {
let tv = Self::new(backend, request.clone(), v);
return Ok(tv);
Expand All @@ -53,12 +53,12 @@ impl ToolVersion {
}
let tv = match request.clone() {
ToolRequest::Version { version: v, .. } => {
Self::resolve_version(backend, request, latest_versions, &v)?
Self::resolve_version(backend, request, &v, opts)?
}
ToolRequest::Prefix { prefix, .. } => Self::resolve_prefix(backend, request, &prefix)?,
ToolRequest::Sub {
sub, orig_version, ..
} => Self::resolve_sub(backend, request, latest_versions, &sub, &orig_version)?,
} => Self::resolve_sub(backend, request, &sub, &orig_version, opts)?,
_ => {
let version = request.version();
Self::new(backend, request, version)
Expand Down Expand Up @@ -101,7 +101,11 @@ impl ToolVersion {
self.backend.downloads_path.join(self.tv_pathname())
}
pub fn latest_version(&self, tool: &dyn Backend) -> Result<String> {
let tv = self.request.resolve(tool, true)?;
let opts = ResolveOptions {
latest_versions: true,
use_locked_version: false,
};
let tv = self.request.resolve(tool, &opts)?;
// map cargo backend specific prefixes to ref
let version = match tv.request.version().split_once(':') {
Some((_ref_type @ ("tag" | "branch" | "rev"), r)) => {
Expand Down Expand Up @@ -132,8 +136,8 @@ impl ToolVersion {
fn resolve_version(
backend: &dyn Backend,
request: ToolRequest,
latest_versions: bool,
v: &str,
opts: &ResolveOptions,
) -> Result<ToolVersion> {
let config = Config::get();
let v = config.resolve_alias(backend, v)?;
Expand All @@ -155,7 +159,7 @@ impl ToolVersion {
}
Some((part, v)) if part.starts_with("sub-") => {
let sub = part.split_once('-').unwrap().1;
return Self::resolve_sub(backend, request, latest_versions, sub, v);
return Self::resolve_sub(backend, request, sub, v, opts);
}
_ => (),
}
Expand All @@ -169,7 +173,7 @@ impl ToolVersion {
}

if v == "latest" {
if !latest_versions {
if !opts.latest_versions {
if let Some(v) = backend.latest_installed_version(None)? {
return build(v);
}
Expand All @@ -178,7 +182,7 @@ impl ToolVersion {
return build(v);
}
}
if !latest_versions {
if !opts.latest_versions {
let matches = backend.list_installed_versions_matching(&v)?;
if matches.contains(&v) {
return build(v);
Expand All @@ -198,16 +202,16 @@ impl ToolVersion {
fn resolve_sub(
tool: &dyn Backend,
request: ToolRequest,
latest_versions: bool,
sub: &str,
v: &str,
opts: &ResolveOptions,
) -> Result<Self> {
let v = match v {
"latest" => tool.latest_version(None)?.unwrap(),
_ => Config::get().resolve_alias(tool, v)?,
};
let v = tool_request::version_sub(&v, sub);
Self::resolve_version(tool, request, latest_versions, &v)
Self::resolve_version(tool, request, &v, opts)
}

fn resolve_prefix(tool: &dyn Backend, request: ToolRequest, prefix: &str) -> Result<Self> {
Expand Down Expand Up @@ -281,3 +285,18 @@ impl Hash for ToolVersion {
self.version.hash(state);
}
}

#[derive(Debug, Clone)]
pub struct ResolveOptions {
pub latest_versions: bool,
pub use_locked_version: bool,
}

impl Default for ResolveOptions {
fn default() -> Self {
Self {
latest_versions: false,
use_locked_version: true,
}
}
}
15 changes: 11 additions & 4 deletions src/toolset/tool_version_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use crate::backend;
use crate::cli::args::BackendArg;
use crate::errors::Error;
use crate::toolset::tool_request::ToolRequest;
use crate::toolset::tool_version::ResolveOptions;
use crate::toolset::{ToolSource, ToolVersion};

/// represents several versions of a tool for a particular plugin
Expand All @@ -22,11 +23,11 @@ impl ToolVersionList {
source,
}
}
pub fn resolve(&mut self, latest_versions: bool) -> eyre::Result<()> {
pub fn resolve(&mut self, opts: &ResolveOptions) -> eyre::Result<()> {
self.versions.clear();
let plugin = backend::get(&self.backend);
for tvr in &mut self.requests {
match tvr.resolve(plugin.as_ref(), latest_versions) {
match tvr.resolve(plugin.as_ref(), opts) {
Ok(v) => self.versions.push(v),
Err(err) => {
return Err(Error::FailedToResolveVersion {
Expand Down Expand Up @@ -58,7 +59,10 @@ mod tests {
let mut tvl = ToolVersionList::new(fa.clone(), ToolSource::Argument);

Check warning on line 59 in src/toolset/tool_version_list.rs

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

Diff in /home/runner/work/mise/mise/src/toolset/tool_version_list.rs
tvl.requests
.push(ToolRequest::new(fa, "latest", ToolSource::Argument).unwrap());
tvl.resolve(true).unwrap();
tvl.resolve(&ResolveOptions{
latest_versions: true,
use_locked_version: false,
}).unwrap();
assert_eq!(tvl.versions.len(), 1);
}

Expand All @@ -72,7 +76,10 @@ mod tests {
let mut tvl = ToolVersionList::new(fa.clone(), ToolSource::Argument);

Check warning on line 76 in src/toolset/tool_version_list.rs

View workflow job for this annotation

GitHub Actions / unit (ubuntu-latest)

Diff in /home/runner/work/mise/mise/src/toolset/tool_version_list.rs
tvl.requests
.push(ToolRequest::new(fa, "latest", ToolSource::Argument).unwrap());
let _ = tvl.resolve(true);
let _ = tvl.resolve(&ResolveOptions{
latest_versions: true,
use_locked_version: false,
});
assert_eq!(tvl.versions.len(), 0);
}
}

0 comments on commit 577aefd

Please sign in to comment.