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
8 changes: 5 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1393,9 +1393,11 @@ rocm uninstall [--yes] [--dry-run]
`rocm uninstall` removes four categories of files that ROCm CLI manages on this
computer: its binaries, configuration, data, and caches. It lists what it will
do and asks `[y/N]` (default: no) before doing it. Use `--dry-run` to only list,
and `--yes` to skip the confirmation. Outside an interactive terminal, when there
is something to remove, it refuses with exit code 1 and removes nothing unless
you pass `--yes`. Each `--keep-*` flag skips the removal of one category:
and `--yes` to skip the confirmation. It does not create the folders it reports
on: with `--keep-binaries`, on a computer where ROCm CLI never ran, it says there
is nothing to remove. Outside an interactive terminal, when there is something
to remove, it refuses with exit code 1 and removes nothing unless you pass
`--yes`. Each `--keep-*` flag skips the removal of one category:

- `--keep-binaries` keeps the installed ROCm CLI binaries.
- `--keep-config` keeps your saved settings.
Expand Down
39 changes: 39 additions & 0 deletions apps/rocm/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ Commands marked [preview] work but are outside the Tech Preview's supported\n\
scope: treat them as unfinished and do not depend on their behaviour yet.\n\n\
Run `rocm <command> --help` for details and examples on any command."
)]
// No global flags, no subcommand aliases and no abbreviations: the logger gate in
// `logging::is_enabled_for` reads the subcommand as `argv[1]` and relies on that
// (guarded by `uninstall_stays_the_first_argument_the_logger_gate_reads`).
pub(crate) struct Cli {
#[command(subcommand)]
pub(crate) command: Option<Command>,
Expand Down Expand Up @@ -2093,6 +2096,42 @@ mod tests {
assert_eq!(error.kind(), clap::error::ErrorKind::ValueValidation);
}

/// `logging::is_enabled_for` decides that `rocm uninstall` skips the client
/// log by looking at the first raw argument. That is only right while the
/// subcommand is always `argv[1]`: a global flag in front of it, an alias
/// for `uninstall`, or an abbreviated name would start the logger again and
/// a dry run would list a `logs` folder it had just made. The e2e scenario
/// `uninstall-06` proves the behaviour; this fails earlier, naming the cause.
#[test]
fn uninstall_stays_the_first_argument_the_logger_gate_reads() {
let command = Cli::command();
let global_args: Vec<_> = command
.get_arguments()
.map(|arg| arg.get_id().as_str())
.filter(|id| !matches!(*id, "help" | "version"))
.collect();
assert!(
global_args.is_empty(),
"a top-level argument {global_args:?} can come before the subcommand, so \
logging::is_enabled_for no longer sees `uninstall` as the first argument"
);
let aliased: Vec<_> = command
.get_subcommands()
.filter(|sub| sub.get_all_aliases().any(|alias| alias == "uninstall"))
.map(clap::Command::get_name)
.collect();
assert!(
aliased.is_empty(),
"{aliased:?} answers to `uninstall`, which logging::is_enabled_for does not check"
);
let uninstall = command
.find_subcommand("uninstall")
.expect("uninstall subcommand exists");
assert_eq!(uninstall.get_all_aliases().count(), 0);
Cli::try_parse_from(["rocm", "uninstal"])
.expect_err("an abbreviated subcommand name must not reach uninstall");
}

#[test]
fn update_rejects_zero_timeout_secs() {
let error = Cli::try_parse_from(["rocm", "update", "--json", "--timeout-secs", "0"])
Expand Down
31 changes: 31 additions & 0 deletions apps/rocm/src/logging.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ const MAX_RETAINED_LOGS: usize = 7;
/// else (dependency noise stays out of the file).
const DEFAULT_FILTER: &str = "warn,rocm=info,rocm_dash_tui=info,rocm_dash_daemon=info";

/// Whether this invocation opens the client log file at all.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking: the skip decision relies on the subcommand being argv[1]. That holds today (no global flags, no uninstall alias), but a future global flag would silently re-enable the logger, and only the e2e scenario would catch it. A comment next to the clap definition, or deriving this from the parsed command, would make that coupling harder to miss.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in 990cc08. The coupling is now guarded by a unit test that fails, and names the cause, when the top-level command gains an argument (a global flag that could come before the subcommand), when any subcommand answers to uninstall as an alias, or when an abbreviated name such as uninstal reaches it. I checked that it goes red for a global flag and for an alias on uninstall. The Cli definition also carries a comment pointing at logging::is_enabled_for and the test. I kept the argv[1] check itself rather than deriving the decision from the parsed command, because the logger has to be started or skipped before parsing.

///
/// `rocm uninstall` does not: it inspects and deletes the folders the logger
/// would create, so starting the logger first made a dry run list a `logs`
/// folder it had just made. In a direct invocation the subcommand is the first
/// argument, because `rocm` has no global flags and does not abbreviate
/// subcommand names; an uninstall reached from `rocm chat` or a freeform
/// request runs under a logger that was already started.
pub fn is_enabled_for(raw_args: &[String]) -> bool {
raw_args.first().is_none_or(|arg| arg != "uninstall")
}

/// Initialize the process-wide `tracing` subscriber, writing to a daily-
/// rotated file under `paths.client_log_dir()`.
///
Expand Down Expand Up @@ -167,6 +179,25 @@ mod tests {
)
}

fn args(list: &[&str]) -> Vec<String> {
list.iter().map(|arg| (*arg).to_string()).collect()
}

#[test]
fn uninstall_is_the_only_invocation_that_skips_the_log_file() {
// The scenario `uninstall-06` proves the behavior (a dry run creates and
// lists nothing); this pins which invocations that applies to.
assert!(!is_enabled_for(&args(&["uninstall"])));
assert!(!is_enabled_for(&args(&["uninstall", "--dry-run"])));
assert!(is_enabled_for(&args(&[])));
assert!(is_enabled_for(&args(&["examine"])));
assert!(is_enabled_for(&args(&["--help"])));
// `runtimes uninstall` removes one runtime, not the folders the logger
// writes to, and a longer word is not the subcommand.
assert!(is_enabled_for(&args(&["runtimes", "uninstall", "x"])));
assert!(is_enabled_for(&args(&["uninstall-all"])));
}

#[test]
fn temp_paths_gives_each_call_its_own_root_even_for_one_label() {
crate::test_support::assert_each_call_gets_its_own_root(|| temp_paths("same-label").0);
Expand Down
7 changes: 4 additions & 3 deletions apps/rocm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,14 +330,15 @@ fn run() -> Result<()> {
// non-blocking file writer, so an early drop would silently truncate the
// log. A failed/missing `AppPaths::discover()` degrades to no logging
// rather than a startup failure.
let _log_guard = AppPaths::discover()
.ok()
let raw_args: Vec<String> = std::env::args().skip(1).collect();
let _log_guard = logging::is_enabled_for(&raw_args)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Non-blocking, related: maybe_migrate_legacy_dashboard_config() just below still runs for uninstall --dry-run and writes config.json when a legacy dashboard TOML exists, so a dry run can still have a side effect. This predates the PR and is outside the #791 repro, but it is worth a mention in the scope notes or a follow-up.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed that this is a real remaining side effect, and I'm leaving it out of this change on purpose: maybe_migrate_legacy_dashboard_config() predates this PR, runs for every command, and writes config.json from a legacy dashboard file rather than creating the folders the #791 repro is about, so fixing it here would widen the change beyond the reported behaviour. I added it to the scope notes in the PR body, so it is on record as a known gap and not an oversight. The simplest fix is to skip the migration for uninstall --dry-run by the same argv gate the logger uses, with its own scenario.

.then(AppPaths::discover)
.and_then(Result::ok)
.and_then(|paths| logging::init(&paths));

maybe_migrate_legacy_dashboard_config();
maybe_notice_legacy_uv_cache();

let raw_args: Vec<String> = std::env::args().skip(1).collect();
if raw_args.is_empty() {
return launch_default();
}
Expand Down
5 changes: 5 additions & 0 deletions docs/manual-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1071,6 +1071,11 @@ Expected result:
`PATH change left in place` and names it, and the `PATH` change stays.
- With `rocm uninstall --keep-binaries`, the review says the `PATH` change
stays too, and it does.
- On a home folder ROCm CLI never touched, the dry run says
`Nothing managed by rocm-cli would be removed.` and leaves the home folder
empty (scenario `uninstall-06`): run
`HOME=$(mktemp -d) rocm uninstall --keep-binaries --dry-run`, then `ls -A`
on that folder.
- Without `--yes` and without a terminal, the command prints the same review,
then refuses with `uninstall requires --yes outside an interactive terminal`
and exit code 1, and removes nothing. On Linux or WSL, check it with no
Expand Down
5 changes: 4 additions & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -953,7 +953,10 @@ symlinks.
The `uninstall-NN` E2E scenarios (`features/uninstall.feature`) check the same
behaviour through the built binary, confined to the scenario's own folder,
including a folder that cannot be checked stopping the uninstall before
anything is removed:
anything is removed, and a dry run on a machine ROCm CLI never touched
(`uninstall-06`) reporting nothing to remove and creating no folder. The
`uninstall` command does not start the file logger, which would otherwise create
the data folder's `logs` folder before the plan is built:

```bash
cargo xtask e2e -- -n "uninstall-"
Expand Down
14 changes: 14 additions & 0 deletions tests/e2e-cucumber/features/uninstall.feature
Original file line number Diff line number Diff line change
Expand Up @@ -66,3 +66,17 @@ Feature: Uninstall
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

# `rocm uninstall --dry-run` shows what would be removed without changing
# anything. It once started the file logger first, which created the data
# folder's `logs` folder, and then listed that folder as the one thing to
# remove, so a machine ROCm CLI had never touched looked installed. The
# promise is two-sided: the plan says there is nothing to remove, and the
# folders it would have removed are still absent afterwards.
@id:uninstall-dry-run-on-an-untouched-machine-creates-nothing
Scenario: uninstall-06 - A dry run on a machine ROCm CLI never touched reports nothing to remove and creates nothing
Given ROCm CLI has never created its folders in the user's home
When the user dry-runs an uninstall
Then the uninstall succeeds
And the uninstall reports that nothing would be removed
And ROCm CLI has still not created its folders
58 changes: 58 additions & 0 deletions tests/e2e-cucumber/tests/e2e/uninstall_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,64 @@ async fn default_cache_intact(world: &mut E2eWorld) {
);
}

/// Every place the CLI keeps state when only the config and data folders are
/// set: those two, the cache folder inside the data folder, and the default
/// `~/.rocm` that a stray write would land in. The harness's own cache folder
/// is left out because a runner may share it between scenarios.
fn state_folders(world: &E2eWorld) -> Vec<PathBuf> {
let paths = world.app_paths();
vec![
paths.config_dir,
paths.data_dir.clone(),
paths.data_dir.join("cache"),
home_dir(world).join(".rocm"),
]
}

#[given("ROCm CLI has never created its folders in the user's home")]
async fn no_folders_yet(world: &mut E2eWorld) {
// The harness creates the config, data and cache folders empty before each
// scenario; a machine ROCm CLI never touched has none of them.
for sub in ["config", "data", "cache"] {
let folder = root(world).join(sub);
std::fs::remove_dir(&folder)
.unwrap_or_else(|e| panic!("failed to remove the empty {}: {e}", folder.display()));
}
for folder in state_folders(world) {
assert!(
std::fs::symlink_metadata(&folder).is_err(),
"the scenario must start without {}",
folder.display()
);
}
}

#[when("the user dry-runs an uninstall")]
async fn dry_run_uninstall(world: &mut E2eWorld) {
run_without_cache_setting(world, &["uninstall", "--keep-binaries", "--dry-run"]);
}

#[then("the uninstall reports that nothing would be removed")]
async fn reports_nothing_to_remove(world: &mut E2eWorld) {
let stdout = output(world);
assert!(
stdout.contains("Nothing managed by rocm-cli would be removed."),
"a machine with no folders has nothing to remove:\n{stdout}"
);
}

#[then("ROCm CLI has still not created its folders")]
async fn still_no_folders(world: &mut E2eWorld) {
for folder in state_folders(world) {
assert!(
std::fs::symlink_metadata(&folder).is_err(),
"the dry run created {}:\n{}",
folder.display(),
output(world)
);
}
}

fn output(world: &E2eWorld) -> &str {
world
.cli_output
Expand Down
Loading