diff --git a/README.md b/README.md index 71e8da0eba..ae827fd156 100644 --- a/README.md +++ b/README.md @@ -1287,7 +1287,12 @@ you pass `--yes`. Each `--keep-*` flag skips the removal of one category: the managed `uv` package cache, which `--keep-data` controls. By default, the configuration and data directories are both `~/.rocm`, and the -cache directory is `~/.rocm/cache`. `uninstall` removes a category's directory +cache directory is `~/.rocm/cache`. The `ROCM_CLI_CONFIG_DIR`, +`ROCM_CLI_DATA_DIR`, and `ROCM_CLI_CACHE_DIR` environment variables move them; +when only `ROCM_CLI_DATA_DIR` is set, the cache directory moves with it, to +`/cache`. A cache that an earlier version kept at `~/.rocm/cache` +while only `ROCM_CLI_DATA_DIR` was set is no longer used or removed by `rocm +uninstall`; delete it by hand if you don't need it. `uninstall` removes a category's directory unless you keep that category, so on a default installation a flag doesn't protect its files by itself. To keep your settings and data, pass `--keep-config` and `--keep-data` together. Run with `--dry-run` first to confirm what is kept. diff --git a/crates/rocm-core/src/paths.rs b/crates/rocm-core/src/paths.rs index 554dde6d54..15840e56e9 100644 --- a/crates/rocm-core/src/paths.rs +++ b/crates/rocm-core/src/paths.rs @@ -19,6 +19,14 @@ use crate::runtime::{ managed_runtime_data_root, normalize_runtime_path_for_host, }; +/// The `ROCM_CLI_*_DIR` overrides, one field per root, each `None` when unset. +#[derive(Debug, Clone, Default)] +struct PathOverrides { + config: Option, + data: Option, + cache: Option, +} + #[derive(Debug, Clone, Serialize)] pub struct AppPaths { pub config_dir: PathBuf, @@ -30,25 +38,51 @@ impl AppPaths { pub fn discover() -> Result { let data_dir_override = env_path_override("ROCM_CLI_DATA_DIR"); let cache_dir_override = env_path_override("ROCM_CLI_CACHE_DIR"); - let paths = Self { - config_dir: env_path_override("ROCM_CLI_CONFIG_DIR") + let paths = Self::from_overrides(PathOverrides { + config: env_path_override("ROCM_CLI_CONFIG_DIR"), + data: data_dir_override.clone(), + cache: cache_dir_override.clone(), + })?; + Ok(Self::discover_from_paths( + paths, + data_dir_override.is_some(), + cache_dir_override.is_some(), + )) + } + + /// The three roots from their `ROCM_CLI_*_DIR` overrides, before any + /// `setup.therock_venv` managed root is applied. + /// + /// The cache folder follows a data folder moved by `ROCM_CLI_DATA_DIR` to + /// `/cache` — the same place it sits by default, `~/.rocm/cache` + /// under `~/.rocm`, and where [`Self::with_managed_root`] puts it when the + /// data folder moves to a managed root — unless `ROCM_CLI_CACHE_DIR` names + /// one of its own. Falling back to the default instead would leave an + /// install isolated by its config and data folders sharing the default + /// install's cache, and `rocm uninstall` would delete it. + /// + /// Takes the overrides as a named struct so tests can drive it without + /// mutating the process environment, and so the three optional folders + /// cannot be transposed. + fn from_overrides(overrides: PathOverrides) -> Result { + let PathOverrides { + config: config_dir, + data: data_dir, + cache, + } = overrides; + let cache_dir = cache.or_else(|| data_dir.as_deref().map(managed_runtime_cache_dir)); + Ok(Self { + config_dir: config_dir .or_else(default_config_dir) .context("unable to determine config directory for rocm-cli")?, - data_dir: data_dir_override - .clone() + data_dir: data_dir .or_else(default_data_dir) .context("unable to determine data directory for rocm-cli")?, - cache_dir: cache_dir_override - .clone() + cache_dir: cache_dir .or_else(default_cache_dir) .context("unable to determine cache directory for rocm-cli")?, } - .normalize_for_host(); - Ok(Self::discover_from_paths( - paths, - data_dir_override.is_some(), - cache_dir_override.is_some(), - )) + .normalize_for_host()) } fn discover_from_paths( @@ -385,6 +419,90 @@ mod tests { Ok(()) } #[test] + fn cache_dir_follows_an_overridden_data_dir() -> Result<()> { + let (root, _) = temp_app_paths("cache-follows-data"); + let config = root.join("config"); + let data = root.join("data"); + + let paths = AppPaths::from_overrides(PathOverrides { + config: Some(config.clone()), + data: Some(data.clone()), + cache: None, + })?; + assert_eq!(paths.config_dir, normalize_runtime_path_for_host(&config)); + assert_eq!(paths.data_dir, normalize_runtime_path_for_host(&data)); + assert_eq!( + paths.cache_dir, + normalize_runtime_path_for_host(&data.join("cache")), + "an install isolated by ROCM_CLI_DATA_DIR must not share the default cache" + ); + + // An explicit cache folder still wins over the data folder's. + let cache = root.join("elsewhere").join("cache"); + let pinned = AppPaths::from_overrides(PathOverrides { + config: Some(config), + data: Some(data), + cache: Some(cache.clone()), + })?; + assert_eq!(pinned.cache_dir, normalize_runtime_path_for_host(&cache)); + Ok(()) + } + #[test] + fn a_managed_root_does_not_move_a_cache_that_followed_the_data_dir() -> Result<()> { + // The path `discover()` takes with ROCM_CLI_DATA_DIR set and no cache + // override, with a `setup.therock_venv` managed root in the config. + let (root, _) = temp_app_paths("cache-follows-data-managed-root"); + let config = root.join("config"); + let data = root.join("data"); + let managed_runtime = root + .join("managed") + .join("runtimes") + .join("wheel") + .join("release-wheel-gfx942-7-0"); + fs::create_dir_all(&config)?; + fs::write( + config.join("config.json"), + serde_json::to_vec_pretty(&serde_json::json!({ + "setup": { "therock_venv": managed_runtime } + }))?, + )?; + + let paths = AppPaths::from_overrides(PathOverrides { + config: Some(config), + data: Some(data.clone()), + cache: None, + })?; + assert_eq!( + configured_managed_root_from_config(&paths), + Some(managed_runtime), + "the fixture must be a config the managed-root lookup reads" + ); + let discovered = AppPaths::discover_from_paths(paths, true, false); + assert_eq!(discovered.data_dir, normalize_runtime_path_for_host(&data)); + assert_eq!( + discovered.cache_dir, + normalize_runtime_path_for_host(&data.join("cache")) + ); + Ok(()) + } + #[test] + fn cache_dir_keeps_its_default_without_a_data_override() -> Result<()> { + // Only the config folder moved: data and cache both stay at their + // defaults, so the cache is not pulled under the config folder. + let (root, _) = temp_app_paths("cache-default"); + let default_cache = + default_cache_dir().expect("the test host has a home folder for the default cache"); + let paths = AppPaths::from_overrides(PathOverrides { + config: Some(root.join("config")), + ..PathOverrides::default() + })?; + assert_eq!( + paths.cache_dir, + normalize_runtime_path_for_host(&default_cache) + ); + Ok(()) + } + #[test] fn engine_envs_dir_honors_dedicated_root_override() { let (root, paths) = temp_app_paths("engine-envs-root-override"); let override_root = root.join("runtime").join("engines"); diff --git a/tests/e2e-cucumber/features/uninstall.feature b/tests/e2e-cucumber/features/uninstall.feature index 16eb177d15..3e06a03efe 100644 --- a/tests/e2e-cucumber/features/uninstall.feature +++ b/tests/e2e-cucumber/features/uninstall.feature @@ -10,10 +10,10 @@ Feature: Uninstall # and points HOME, the uv cache and the Hugging Face cache at the scenario's # own folder, so an uninstall can never reach the runner's shared caches. # - # Linux-only, and the behaviour they pin is only claimed for Linux: on - # Windows a directory symlink or junction needs `remove_dir` rather than - # `remove_file`, which `rocm uninstall` does not do yet and nothing here - # verifies. + # The link scenarios are Linux-only, and the behaviour they pin is only + # claimed for Linux: on Windows a directory symlink or junction needs + # `remove_dir` rather than `remove_file`, which `rocm uninstall` does not do + # yet and nothing here verifies. @id:uninstall-trailing-slash-link-unlinks-only @requires-os:linux Scenario: uninstall-01 - A linked cache folder written with a trailing slash is unlinked, not emptied @@ -50,3 +50,19 @@ Feature: Uninstall When the user uninstalls the config without confirming Then the uninstall is refused and the user is told to confirm explicitly And the config folder still holds the user's files + + # An install isolated by ROCM_CLI_CONFIG_DIR and ROCM_CLI_DATA_DIR alone, + # with no ROCM_CLI_CACHE_DIR, keeps its cache inside its data folder. It once + # fell back to the default `~/.rocm/cache`, so `rocm storage` reported the + # default install's cache and `rocm uninstall` deleted it. + @id:uninstall-moved-data-folder-keeps-its-own-cache + Scenario: uninstall-05 - With only the config and data folders set, the cache folder is the data folder's own + Given a default install's cache folder in the user's home holds the user's files + And the data folder's own cache folder holds a cache file + When the user asks what ROCm CLI keeps on disk with only the config and data folders set + Then the storage report names the cache folder inside the data folder + When the user uninstalls with only the config and data folders set + Then the uninstall succeeds + And the uninstall does not name the default install's cache folder + And the data folder's own cache folder is gone + And the default install's cache folder still holds the user's files diff --git a/tests/e2e-cucumber/tests/e2e.rs b/tests/e2e-cucumber/tests/e2e.rs index 72b593360d..2d56ac2835 100644 --- a/tests/e2e-cucumber/tests/e2e.rs +++ b/tests/e2e-cucumber/tests/e2e.rs @@ -944,11 +944,28 @@ pub fn run_rocm_with_env( world: &E2eWorld, args: &[&str], envs: &[(&str, &str)], +) -> (String, String, i32) { + run_rocm_with_env_and_unset(world, args, envs, &[]) +} + +/// Like [`run_rocm_with_env`], but first removes each of `unset` from the env. +/// +/// For a scenario about what the CLI resolves when one of the +/// `ROCM_CLI_*_DIR` settings is left out. Everything the scenario keeps set +/// still points inside its isolated root. +pub fn run_rocm_with_env_and_unset( + world: &E2eWorld, + args: &[&str], + envs: &[(&str, &str)], + unset: &[&str], ) -> (String, String, i32) { let binary = rocm_binary(); let mut cmd = std::process::Command::new(&binary); cmd.args(args); world.isolate_cmd(&mut cmd); + for key in unset { + cmd.env_remove(key); + } for (key, value) in envs { cmd.env(key, value); } diff --git a/tests/e2e-cucumber/tests/e2e/uninstall_steps.rs b/tests/e2e-cucumber/tests/e2e/uninstall_steps.rs index 657e6aaa64..13871a4cbe 100644 --- a/tests/e2e-cucumber/tests/e2e/uninstall_steps.rs +++ b/tests/e2e-cucumber/tests/e2e/uninstall_steps.rs @@ -157,6 +157,132 @@ async fn uninstall_config_unconfirmed(world: &mut E2eWorld) { ); } +/// The scenario's own home folder, standing in for the user's. +fn home_dir(world: &E2eWorld) -> PathBuf { + root(world).join("home") +} + +/// The cache folder a default install in [`home_dir`] uses (`~/.rocm/cache`). +fn default_cache(world: &E2eWorld) -> PathBuf { + home_dir(world).join(".rocm").join("cache") +} + +/// Where the cache folder belongs when only the config and data folders are +/// set: inside the scenario's data folder, the one `isolate_env` hands the CLI. +fn data_cache(world: &E2eWorld) -> PathBuf { + world.app_paths().data_dir.join("cache") +} + +/// Run `rocm` with the config and data folders set but no cache folder, as an +/// install isolated by `ROCM_CLI_CONFIG_DIR` and `ROCM_CLI_DATA_DIR` alone is. +/// HOME (and USERPROFILE, which is where Windows finds it) is the scenario's +/// own, so a cache resolved to the default lands in [`default_cache`], not the +/// runner's. +fn run_without_cache_setting(world: &mut E2eWorld, args: &[&str]) { + let root = root(world).to_path_buf(); + let home = home_dir(world); + std::fs::create_dir_all(&home).expect("failed to create isolated HOME"); + let home = home.display().to_string(); + let uv = root.join("uv-cache").display().to_string(); + let hf = root.join("hf-home").display().to_string(); + let envs = [ + ("HOME", home.as_str()), + ("USERPROFILE", home.as_str()), + ("UV_CACHE_DIR", uv.as_str()), + ("HF_HOME", hf.as_str()), + ]; + let (stdout, stderr, rc) = + crate::run_rocm_with_env_and_unset(world, args, &envs, &["ROCM_CLI_CACHE_DIR"]); + world.cli_output = Some(stdout); + world.cli_stderr = Some(stderr); + world.cli_rc = Some(rc); +} + +#[given("a default install's cache folder in the user's home holds the user's files")] +async fn default_cache_holds_user_files(world: &mut E2eWorld) { + let cache = default_cache(world); + std::fs::create_dir_all(&cache).expect("failed to create the default cache folder"); + std::fs::write(cache.join(USER_FILE), "user data").expect("failed to write user file"); +} + +#[given("the data folder's own cache folder holds a cache file")] +async fn data_cache_holds_file(world: &mut E2eWorld) { + let cache = data_cache(world); + std::fs::create_dir_all(&cache).expect("failed to create the data folder's cache folder"); + std::fs::write(cache.join("cached.bin"), "cached").expect("failed to write cache file"); +} + +#[when("the user asks what ROCm CLI keeps on disk with only the config and data folders set")] +async fn storage_without_cache_setting(world: &mut E2eWorld) { + run_without_cache_setting(world, &["storage", "report"]); +} + +#[when("the user uninstalls with only the config and data folders set")] +async fn uninstall_without_cache_setting(world: &mut E2eWorld) { + run_without_cache_setting(world, &["uninstall", "--yes", "--keep-binaries"]); +} + +#[then("the storage report names the cache folder inside the data folder")] +async fn storage_names_data_cache(world: &mut E2eWorld) { + let rc = world.cli_rc.expect("no exit code captured"); + let stdout = output(world); + assert_eq!(rc, 0, "storage report must exit 0:\n{stdout}"); + let row = stdout + .lines() + .find(|line| line.trim_start().starts_with("- ROCm CLI cache folder:")) + .unwrap_or_else(|| panic!("the report must name the cache folder:\n{stdout}")); + let expected = format!("({})", data_cache(world).display()); + assert!( + row.ends_with(&expected), + "the cache folder must be the one inside the data folder {expected}, got `{row}`:\n{stdout}" + ); +} + +#[then("the uninstall does not name the default install's cache folder")] +async fn uninstall_skips_default_cache(world: &mut E2eWorld) { + let stdout = output(world); + let default = default_cache(world).display().to_string(); + assert!( + !stdout.contains(&default), + "an uninstall of an isolated install must not touch {default}:\n{stdout}" + ); + // It removed exactly one cache folder, and that one is the isolated + // install's own; zero removals must not pass. + let expected = format!("removed cache {}", data_cache(world).display()); + let removals: Vec<&str> = stdout + .lines() + .filter(|l| l.starts_with("removed cache ")) + .collect(); + assert_eq!( + removals, + vec![expected.as_str()], + "the uninstall must report removing exactly the data folder's cache:\n{stdout}" + ); +} + +#[then("the data folder's own cache folder is gone")] +async fn data_cache_gone(world: &mut E2eWorld) { + let cache = data_cache(world); + assert!( + !cache.exists(), + "the uninstall left {} behind: +{}", + cache.display(), + output(world) + ); +} + +#[then("the default install's cache folder still holds the user's files")] +async fn default_cache_intact(world: &mut E2eWorld) { + let file = default_cache(world).join(USER_FILE); + assert!( + file.is_file(), + "uninstalling the isolated install deleted {}:\n{}", + file.display(), + output(world) + ); +} + fn output(world: &E2eWorld) -> &str { world .cli_output