Skip to content
Merged
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
102 changes: 102 additions & 0 deletions e2e/cli/test_nonexistent_cwd
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
#!/usr/bin/env bash

set -euo pipefail

# Test that mise handles commands from non-existent directories gracefully
# This test validates that when a directory is deleted while mise is running from it,
# mise shows a warning but continues execution

# Test helper function to run commands from a non-existent directory
run_from_nonexistent() {
local cmd="$1"
local should_succeed="${2:-true}" # Whether the command should succeed despite the warning

# Create a temporary directory
local temp_dir
temp_dir=$(mktemp -d)

# Change to the temporary directory
cd "$temp_dir"

# Remove the directory while we're still in it
rmdir "$temp_dir"

# Now we're in a non-existent directory - run the mise command
local status=0
local output
output=$(MISE_FRIENDLY_ERROR=1 RUST_BACKTRACE=0 bash -c "$cmd 2>&1") || status=$?

# Check for the warning about non-existent directory
if [[ $output == *"Current directory does not exist"* ]] || [[ $output == *"WARNING"* ]]; then
ok "[$cmd] showed warning about non-existent directory"
else
# Some commands might not trigger the warning if they exit early
debug "[$cmd] may not have shown warning, output: ${output:0:100}..."
fi

if [[ $should_succeed == "true" ]]; then
if [[ $status -eq 0 ]]; then
ok "[$cmd] succeeded despite non-existent directory"
else
# Command might fail for other reasons (e.g., missing tools)
debug "[$cmd] failed (status $status), which might be expected"
fi
else
if [[ $status -ne 0 ]]; then
ok "[$cmd] failed as expected"
else
fail "[$cmd] succeeded but was expected to fail"
fi
fi

# Return to a valid directory
cd "$HOME" || cd / || true
}

echo "Testing mise commands from non-existent directories..."

# Test various commands - they should all show a warning but may still work
echo "Test 1: mise --version from non-existent directory"
run_from_nonexistent "mise --version" "true"

echo "Test 2: mise ls from non-existent directory"
run_from_nonexistent "mise ls" "true"

echo "Test 3: mise current from non-existent directory"
run_from_nonexistent "mise current" "true"

echo "Test 4: mise env from non-existent directory"
run_from_nonexistent "mise env" "true"

echo "Test 5: mise doctor from non-existent directory"
run_from_nonexistent "mise doctor" "true"

echo "Test 6: mise plugins from non-existent directory"
run_from_nonexistent "mise plugins" "true"

# Commands that need to write to cwd might fail
echo "Test 7: mise use from non-existent directory"
run_from_nonexistent "mise use node@20" "false"

echo "Test 8: mise install from non-existent directory"
run_from_nonexistent "mise install" "true" # Should work for global tools

# Test that the warning appears consistently
echo "Test 9: Verify warning message format"
temp_dir=$(mktemp -d)
cd "$temp_dir"
rmdir "$temp_dir"

output=$(mise --version 2>&1) || true
if [[ $output == *"WARNING"* ]] && [[ $output == *"Current directory does not exist"* ]]; then
ok "Warning message has proper format"
elif [[ $output == *"current directory"* ]]; then
ok "Warning message mentions directory issue"
else
debug "Warning might not be shown for --version: ${output:0:100}..."
fi

cd "$HOME" || cd / || true

echo ""
echo "All non-existent directory tests passed!"
19 changes: 19 additions & 0 deletions mise.lock
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ backend = "core:bun"
checksum = "blake3:bf32acffd6e77803ba760caf8d2909389da01c95613b81fe155fe87e2a8f8004"
size = 39256266

[tools.bun.platforms.macos-arm64]
checksum = "blake3:ec41528ae2badd9c5d41cefe2c463a307c46d9b5f0ccf157d6f406890340833e"
size = 22178114

[[tools.cargo-binstall]]
version = "1.15.5"
backend = "aqua:cargo-bins/cargo-binstall"
Expand All @@ -43,6 +47,11 @@ checksum = "blake3:1a5fa23fb172db04713b8acd854227ad5df08480dcc0a2fbd71f4fd93c069
size = 6790102
url = "https://github.com/cargo-bins/cargo-binstall/releases/download/v1.15.5/cargo-binstall-x86_64-unknown-linux-musl.tgz"

[tools.cargo-binstall.platforms.macos-arm64]
checksum = "blake3:4eff2c4ed16a8864b20b55fbe855821e60dfb633c04e40ab67679a0639f08b2f"
size = 6004395
url = "https://github.com/cargo-bins/cargo-binstall/releases/download/v1.15.5/cargo-binstall-aarch64-apple-darwin.zip"

[[tools."cargo:cargo-edit"]]
version = "0.13.7"
backend = "cargo:cargo-edit"
Expand Down Expand Up @@ -76,6 +85,11 @@ checksum = "sha256:ea5c65f99425d6cfbb5c4b5de5dac035f14d09131c1a0ea7c7fc32eab3936
size = 155329864
url = "https://github.com/sigstore/cosign/releases/download/v2.6.0/cosign-linux-amd64"

[tools.cosign.platforms.macos-arm64]
checksum = "sha256:dea5b83b8b375b99ac803c7bdb1f798963dbeb47789ceb72153202e7f20e8d07"
size = 154611890
url = "https://github.com/sigstore/cosign/releases/download/v2.6.0/cosign-darwin-arm64"

[[tools.gh]]
version = "2.62.0"
backend = "aqua:cli/cli"
Expand All @@ -99,6 +113,11 @@ checksum = "blake3:2e8d3a27beabb40355f7deb89aa02599769a31513af1df88b6f2509f23274
size = 6751013
url = "https://github.com/jdx/hk/releases/download/v1.12.1/hk-x86_64-unknown-linux-gnu.tar.gz"

[tools.hk.platforms.macos-arm64]
checksum = "blake3:1d4153f34bc93216980171a07b0e5441d4e99776506f95ca9c3d63603a9d044c"
size = 5854360
url = "https://github.com/jdx/hk/releases/download/v1.12.1/hk-aarch64-apple-darwin.tar.gz"

[[tools.jq]]
version = "1.8.1"
backend = "aqua:jqlang/jq"
Expand Down
15 changes: 15 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -334,6 +334,7 @@ impl Cli {
tool_stub::short_circuit_stub(&args[2..]).await?;
}
measure!("logger", { logger::init() });
check_working_directory();
measure!("handle_shim", { shims::handle_shim().await })?;
ctrlc::init();
let print_version = version::print_version_if_requested(args)?;
Expand Down Expand Up @@ -449,3 +450,17 @@ static AFTER_LONG_HELP: &str = color_print::cstr!(
$ <bold>mise settings color=0</bold> Disable color by modifying global config file
"#
);

/// Check if the current working directory exists and warn if not
fn check_working_directory() {
if std::env::current_dir().is_err() {
// Try to get the directory path from PWD env var, which might still contain the old path
let dir_path = std::env::var("PWD")
.or_else(|_| std::env::var("OLDPWD"))
.unwrap_or_else(|_| "(unknown)".to_string());
warn!(
"Current directory does not exist or is not accessible: {}",
dir_path
);
}
}
2 changes: 1 addition & 1 deletion src/config/config_file/config_root.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub fn config_root(path: &Path) -> PathBuf {
let path = path
.absolutize()
.map(|p| p.to_path_buf())
.unwrap_or(path.to_path_buf());
.unwrap_or_else(|_| path.to_path_buf());
if let Some(cached) = CONFIG_ROOT_CACHE.lock().unwrap().get(&path).cloned() {
return cached;
}
Expand Down
Loading