Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
43 changes: 43 additions & 0 deletions e2e/cli/test_install_incomplete_survives_cache_clear
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#!/usr/bin/env bash

# A version whose install never finished stays incomplete after the cache is
# cleared, so it is neither listed nor selected over the complete one.
plugin="$MISE_DATA_DIR/plugins/halfdone"
mkdir -p "$plugin/bin"
cat >"$plugin/bin/list-all" <<'SH'
#!/usr/bin/env bash
echo 1.0.0 1.1.0
SH
# 1.1.0 writes a working binary, then fails: an install that died partway.
cat >"$plugin/bin/install" <<'SH'
#!/usr/bin/env bash
mkdir -p "$ASDF_INSTALL_PATH/bin"
printf '#!/usr/bin/env bash\necho "halfdone %s"\n' "$ASDF_INSTALL_VERSION" >"$ASDF_INSTALL_PATH/bin/halfdone"
chmod +x "$ASDF_INSTALL_PATH/bin/halfdone"
[ "$ASDF_INSTALL_VERSION" != "1.1.0" ]
SH
chmod +x "$plugin/bin/list-all" "$plugin/bin/install"

cat >mise.toml <<'TOML'
[tools]
halfdone = "1"
TOML

mise install halfdone@1.0.0
assert "mise x -- halfdone" "halfdone 1.0.0"

# Keep the half-done install directory, as `always_keep_install` does.
assert_fail "MISE_ALWAYS_KEEP_INSTALL=1 mise install halfdone@1.1.0"
assert_directory_exists "$MISE_DATA_DIR/installs/halfdone/1.1.0"
assert_not_contains "mise ls --installed halfdone" "1.1.0"
assert "mise x -- halfdone" "halfdone 1.0.0"

mise cache clear
assert_directory_exists "$MISE_STATE_DIR/incomplete-installs/halfdone/1.1.0"
assert_not_contains "mise ls --installed halfdone" "1.1.0"
assert "mise x -- halfdone" "halfdone 1.0.0"

# Uninstalling the half-done version clears its marker with it.
mise uninstall halfdone@1.1.0
assert_directory_not_exists "$MISE_DATA_DIR/installs/halfdone/1.1.0"
assert_directory_not_exists "$MISE_STATE_DIR/incomplete-installs/halfdone/1.1.0"
3 changes: 2 additions & 1 deletion src/backend/http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::runtime_symlinks::is_runtime_symlink;
use crate::toolset::ToolRequest;
use crate::toolset::ToolVersion;
use crate::toolset::ToolVersionOptions;
use crate::toolset::install_state;
use crate::ui::progress_report::SingleReport;
use crate::{dirs, env, file, hash};
use async_trait::async_trait;
Expand Down Expand Up @@ -1474,7 +1475,7 @@ impl Backend for HttpBackend {
_ => {
let install_path = Self::lookup_install_path(tv);
install_path.exists()
&& !self.incomplete_file_path(tv).exists()
&& !install_state::is_incomplete(tv.ba(), &tv.state_key())
&& (!check_symlink || !is_runtime_symlink(&install_path))
}
}
Expand Down
27 changes: 11 additions & 16 deletions src/backend/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2977,7 +2977,7 @@ pub trait Backend: Debug + Send + Sync {
) -> bool {
let check_path = |install_path: &Path, check_symlink: bool| {
let is_installed = install_path.exists();
let is_not_incomplete = !self.incomplete_file_path(tv).exists();
let is_not_incomplete = !install_state::is_incomplete(tv.ba(), &tv.state_key());
let is_valid_symlink = !check_symlink || !is_runtime_symlink(install_path);
let is_healthy = is_installed && self.is_install_path_healthy(install_path);
// An identity-layout installation is complete once its receipt is there;
Expand Down Expand Up @@ -3613,7 +3613,7 @@ pub trait Backend: Debug + Send + Sync {
// version is a pre-release must not keep winning, and neither
// may one left pointing into an interrupted install.
if (!filter || !self.is_backend_prerelease(&version))
&& !install_state::incomplete_file_path(self.ba(), &version).exists()
&& !install_state::is_incomplete(self.ba(), &version)
{
return Ok(Some(version));
}
Expand All @@ -3623,7 +3623,7 @@ pub trait Backend: Debug + Send + Sync {
.into_iter()
.filter(|v| !v.starts_with('.'))
.filter(|v| !is_runtime_symlink(&installs_path.join(v)))
.filter(|v| !install_state::incomplete_file_path(self.ba(), v).exists())
.filter(|v| !install_state::is_incomplete(self.ba(), v))
.filter(|v| v != "latest")
.sorted_by_cached_key(|v| (Versioning::new(v), v.to_string()))
.collect_vec();
Expand Down Expand Up @@ -3735,6 +3735,7 @@ pub trait Backend: Debug + Send + Sync {
remove_all_with_progress(self.ba().installs_path(), pr)?;
remove_all_with_progress(self.ba().cache_path(), pr)?;
remove_all_with_progress(self.ba().downloads_path(), pr)?;
install_state::clear_incomplete_markers(self.ba())?;
Ok(())
}
fn get_aliases(&self) -> eyre::Result<BTreeMap<String, String>> {
Expand Down Expand Up @@ -4545,6 +4546,8 @@ pub trait Backend: Debug + Send + Sync {
pr.set_message(format!("remove {}", would_remove.join(", ")));
}
} else {
// The marker is outside the cache dir removed above.
install_state::clear_incomplete_marker(tv.ba(), &tv.state_key())?;
self.cleanup_empty_installs_dir();
}
Ok(())
Expand Down Expand Up @@ -4644,14 +4647,10 @@ pub trait Backend: Debug + Send + Sync {
file::create_dir_all(tv.install_path())?;
file::create_dir_all(tv.download_path())?;
file::create_dir_all(tv.cache_path())?;
// `file::create` rather than `File::create`: it names the path in the error. The three
// calls above can return Ok without having created anything -- `std::fs::create_dir_all`
// does that for a path Windows refuses, such as one ending in `nul` -- and the first thing
// to notice is this write, three calls away from the cause. Unwrapped it was a bare
// `The system cannot find the path specified. (os error 3)` naming neither the file nor
// the operation.
file::create(&self.incomplete_file_path(tv))?;
Ok(())
// The three calls above can return Ok without having created anything for a version
// Windows refuses as a path; the marker sits in a directory named for the same version,
// so its write is what reports it, naming the path.
install_state::mark_incomplete(tv.ba(), &tv.state_key())
}
fn cleanup_install_dirs_on_error(&self, tv: &ToolVersion) {
if !Settings::get().always_keep_install {
Expand Down Expand Up @@ -4710,14 +4709,10 @@ pub trait Backend: Debug + Send + Sync {
.await
&& self.verify_repaired_install(ctx, tv).await.is_ok();
if !usable {
let _ = file::create(&self.incomplete_file_path(tv));
let _ = install_state::mark_incomplete(tv.ba(), &tv.state_key());
}
}

fn incomplete_file_path(&self, tv: &ToolVersion) -> PathBuf {
install_state::incomplete_file_path(tv.ba(), &tv.state_key())
}

async fn path_env_for_cmd(&self, config: &Arc<Config>, tv: &ToolVersion) -> Result<OsString> {
let path = self
.list_bin_paths(config, tv)
Expand Down
2 changes: 1 addition & 1 deletion src/backend/packslip.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2475,7 +2475,7 @@ mod tests {
"original binary"
);
assert!(
!install_state::incomplete_file_path(&ba, &tv.state_key()).exists(),
!install_state::is_incomplete(&ba, &tv.state_key()),
"the generic marker is cleared after the payload succeeds"
);
assert!(crate::packslip::skills_incomplete_path(&install_path).is_file());
Expand Down
18 changes: 7 additions & 11 deletions src/runtime_symlinks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ fn is_install_incomplete(backend: &Arc<dyn Backend>, installs_dir: &Path, v: &st
let key = crate::install_layout::resolver::link_target(&installs_dir.join(v))
.and_then(|install| install.file_name().map(|n| n.to_string_lossy().to_string()))
.unwrap_or_else(|| v.to_string());
install_state::incomplete_file_path(backend.ba(), &key).exists()
install_state::is_incomplete(backend.ba(), &key)
}

/// Real install directories a rebuild must never replace with a selector
Expand Down Expand Up @@ -542,24 +542,20 @@ mod tests {
))
}

/// Removes the markers [`interrupted_install`] wrote when the test ends.
struct InterruptedInstall(PathBuf);
/// Removes the marker [`interrupted_install`] wrote when the test ends.
struct InterruptedInstall(Arc<crate::args::BackendArg>, String);

impl Drop for InterruptedInstall {
fn drop(&mut self) {
let _ = fs::remove_dir_all(&self.0);
let _ = install_state::clear_incomplete_marker(&self.0, &self.1);
}
}

/// Leaves version `v` of `backend` the way an interrupted install does:
/// the incomplete marker is still in the cache.
/// the incomplete marker is still in place.
fn interrupted_install(backend: &Arc<dyn Backend>, v: &str) -> Result<InterruptedInstall> {
let marker = install_state::incomplete_file_path(backend.ba(), v);
fs::create_dir_all(marker.parent().unwrap())?;
fs::write(&marker, "")?;
Ok(InterruptedInstall(
marker.parent().unwrap().parent().unwrap().to_path_buf(),
))
install_state::mark_incomplete(backend.ba(), v)?;
Ok(InterruptedInstall(backend.ba().clone(), v.to_string()))
}

#[test]
Expand Down
126 changes: 110 additions & 16 deletions src/toolset/install_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ fn scan_versions(dir: &Path, tool_dir_name: &str) -> Result<Vec<String>> {
.file_name()
.map(|n| n.to_string_lossy().to_string())
.unwrap_or_else(|| name.clone());
if !incomplete_marker(tool_dir_name, &key).exists() {
if !is_incomplete_in(tool_dir_name, &key) {
versions.push(name);
}
continue;
Expand All @@ -306,7 +306,7 @@ fn scan_versions(dir: &Path, tool_dir_name: &str) -> Result<Vec<String>> {
} else if !file_type.is_dir() {
continue;
}
if incomplete_marker(tool_dir_name, &name).exists() {
if is_incomplete_in(tool_dir_name, &name) {
continue;
}
versions.push(name);
Expand Down Expand Up @@ -1087,27 +1087,79 @@ fn strip_url_credentials(value: &toml::Value) -> toml::Value {
toml::Value::String(url.to_string())
}

/// Marks `v` of `ba` as mid-install; also the identity of its install lock.
/// Keyed by the tool's cache dir, which install, uninstall and link share
/// across local, shared and system install paths.
pub(crate) fn incomplete_file_path(ba: &BackendArg, v: &str) -> PathBuf {
ba.cache_path().join(v).join("incomplete")
/// Marks `v` of the tool whose directory is `tool_dir_name` as mid-install.
/// It lives in the state dir rather than the cache, so `mise cache clear` and
/// cache pruning cannot turn an install that never finished into a complete
/// one. Keyed by the tool's directory name, which install, uninstall and link
/// share across local, shared and system install paths.
///
/// The version is a directory rather than the marker's own name: on Windows a
/// file named for a reserved device (`nul`) opens that device, while a
/// directory of that name makes the marker write fail and name the path.
fn incomplete_marker(tool_dir_name: impl AsRef<Path>, v: &str) -> PathBuf {
incomplete_markers_dir(tool_dir_name)
.join(v)
.join("incomplete")
}

fn incomplete_marker(tool_dir_name: impl AsRef<Path>, v: &str) -> PathBuf {
fn incomplete_markers_dir(tool_dir_name: impl AsRef<Path>) -> PathBuf {
dirs::STATE.join("incomplete-installs").join(tool_dir_name)
}

/// Where mise 2026.10.3 and earlier marked an install incomplete. A marker an
/// older mise left there still counts until 2027.1, so an install it never
/// finished is not taken for complete after an upgrade.
fn legacy_incomplete_marker(tool_dir_name: impl AsRef<Path>, v: &str) -> PathBuf {
dirs::CACHE.join(tool_dir_name).join(v).join("incomplete")
}

/// `cache/<tool dir>` and the incomplete markers share the tool's directory name.
fn marker_dir_name(ba: &BackendArg) -> &std::ffi::OsStr {
ba.cache_path()
.file_name()
.expect("a tool's cache path names its directory")
}

fn is_incomplete_in(tool_dir_name: impl AsRef<Path>, v: &str) -> bool {
let tool_dir_name = tool_dir_name.as_ref();
incomplete_marker(tool_dir_name, v).exists()
|| legacy_incomplete_marker(tool_dir_name, v).exists()
}

/// Whether an install of `v` of `ba` started and never finished.
pub(crate) fn is_incomplete(ba: &BackendArg, v: &str) -> bool {
is_incomplete_in(marker_dir_name(ba), v)
}

/// Marks `v` of `ba` as mid-install until [`clear_incomplete_marker`].
pub(crate) fn mark_incomplete(ba: &BackendArg, v: &str) -> Result<()> {
let path = incomplete_marker(marker_dir_name(ba), v);
if let Some(parent) = path.parent() {
file::create_dir_all(parent)?;
}
Comment thread
jdx marked this conversation as resolved.
Resolved
// `file::create` rather than `File::create`: it names the path in the error,
// and `create_dir_all` can return Ok without having created anything --
// `std::fs::create_dir_all` does that for a path Windows refuses, such as
// one ending in `nul`. Unwrapped it was a bare `The system cannot find the
// path specified. (os error 3)` naming neither the file nor the operation.
file::create(&path)?;
Ok(())
}

/// The lock is keyed by where the incomplete marker used to live, which is
/// what older mise processes still lock: moving the marker must not let an old
/// and a new mise install the same version at once.
fn tool_version_lock(ba: &BackendArg, v: &str) -> LockFile {
LockFile::new(&incomplete_file_path(ba, v)).with_pid()
LockFile::new(&ba.cache_path().join(v).join("incomplete")).with_pid()
}

/// Acquires the transaction lock for one logical tool version.
///
/// The incomplete marker is shared by local, shared, and system install paths,
/// so install, uninstall, and link use this logical identity while mutating the
/// marker and install path. The marker path is only the lock identity; the
/// lock itself remains a separate stable file under the lockfiles cache.
/// marker and install path. The lock's identity is the marker's former cache
/// path (see `tool_version_lock`); the lock itself remains a separate stable
/// file under the lockfiles cache.
pub fn lock_tool_version(ba: &BackendArg, v: &str) -> Result<fslock::LockFile> {
lock_tool_version_with_notice(ba, v, &|_| {})
}
Expand All @@ -1130,10 +1182,25 @@ pub(crate) fn lock_tool_version_with_notice(
}

pub fn clear_incomplete_marker(ba: &BackendArg, v: &str) -> Result<()> {
let incomplete_path = incomplete_file_path(ba, v);
match file::remove_file(&incomplete_path) {
let tool_dir_name = marker_dir_name(ba);
let marker = incomplete_marker(tool_dir_name, v);
remove_marker(&marker)?;
if let Some(dir) = marker.parent() {
// Only empty; anything else in it is not ours to remove.
let _ = std::fs::remove_dir(dir);
}
remove_marker(&legacy_incomplete_marker(tool_dir_name, v))
}

/// Drops every incomplete marker of `ba`, for a purge that removes all its versions.
pub(crate) fn clear_incomplete_markers(ba: &BackendArg) -> Result<()> {
file::remove_all(incomplete_markers_dir(marker_dir_name(ba)))
}

fn remove_marker(path: &Path) -> Result<()> {
match file::remove_file(path) {
std::result::Result::Ok(()) => {
if let Some(parent) = incomplete_path.parent()
if let Some(parent) = path.parent()
&& let Err(err) = file::sync_dir(parent)
{
debug!("error syncing incomplete marker parent: {:?}", err);
Expand Down Expand Up @@ -1224,8 +1291,10 @@ pub(crate) fn reset_tools() {
#[cfg(test)]
mod tests {
use super::{
InstallStateTool, incomplete_marker, lock_tool_version, merge_plugin_tools,
normalize_version_for_sort, read_tool_manifest_from, scan_versions, tool_version_lock,
InstallStateTool, clear_incomplete_marker, incomplete_marker, is_incomplete,
legacy_incomplete_marker, lock_tool_version, mark_incomplete, marker_dir_name,
merge_plugin_tools, normalize_version_for_sort, read_tool_manifest_from, scan_versions,
tool_version_lock,
};
use crate::args::BackendArg;
use crate::plugins::PluginType;
Expand Down Expand Up @@ -1593,6 +1662,31 @@ explicit_backend = true
assert_eq!(versions, ["1.0.0"]);
}

/// Clearing the cache must not make an unfinished install look complete,
/// and a marker an older mise left in the cache still counts.
#[test]
fn incomplete_markers_live_outside_the_cache_and_honor_legacy_ones() {
let ba = BackendArg::from(format!("marker_test_{}", std::process::id()).as_str());
let tool_dir_name = marker_dir_name(&ba).to_owned();
let marker = incomplete_marker(&tool_dir_name, "1.0.0");
assert!(!marker.starts_with(*crate::dirs::CACHE));

mark_incomplete(&ba, "1.0.0").unwrap();
let _ = std::fs::remove_dir_all(ba.cache_path());
assert!(is_incomplete(&ba, "1.0.0"));
clear_incomplete_marker(&ba, "1.0.0").unwrap();
assert!(!is_incomplete(&ba, "1.0.0"));
assert!(!marker.parent().unwrap().exists());

let legacy = legacy_incomplete_marker(&tool_dir_name, "2.0.0");
std::fs::create_dir_all(legacy.parent().unwrap()).unwrap();
std::fs::write(&legacy, "").unwrap();
assert!(is_incomplete(&ba, "2.0.0"));
clear_incomplete_marker(&ba, "2.0.0").unwrap();
assert!(!is_incomplete(&ba, "2.0.0"));
let _ = std::fs::remove_dir_all(ba.cache_path());
}

/// Also pins that `DirEntry::file_type()` reports a Windows junction as a symlink, which is
/// what the scan keys on. `make_symlink` writes a junction there, so if that were not so,
/// `broken` would be dropped below — and the *live* junction would never have been listed
Expand Down
4 changes: 2 additions & 2 deletions src/toolset/tool_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -610,11 +610,11 @@ impl ToolVersion {
}

/// Whether an install into `path` was interrupted: the incomplete marker for
/// it is still in the cache.
/// it is still in place.
pub(crate) fn is_incomplete_at(&self, path: &Path) -> bool {
let key = crate::install_layout::resolver::dir_name_of(path)
.unwrap_or_else(|| self.tv_pathname());
install_state::incomplete_file_path(self.ba(), &key).exists()
install_state::is_incomplete(self.ba(), &key)
}

pub(crate) fn aube_install_identity(&self) -> Option<String> {
Expand Down
Loading