Skip to content
Draft
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
14 changes: 9 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1727,7 +1727,7 @@ rocm services logs <service-id> [--follow]

rocm uninstall [--yes] [--dry-run]
[--keep-binaries] [--keep-config] [--keep-data] [--keep-cache]
[--force-dev-binaries]
[--allow-dev-binaries]
```

`--follow` (`-f`) keeps printing a server's new log output, such as a model
Expand All @@ -1745,7 +1745,7 @@ do and asks `[y/N]` (default: no) before doing it. Only `y` or `yes` proceeds.
Enter, `n` and `no` cancel: it prints `uninstall cancelled`, removes nothing and
exits with code 0. Any other answer cancels the same way, after a line saying
that the answer was not understood and is treated as no. Use `--dry-run` to only
list, and `--yes` to skip the confirmation. There is no `--force`; `--force-dev-binaries`
list, and `--yes` to skip the confirmation. There is no `--force`; `--allow-dev-binaries`
is a separate opt-in for development builds. At a terminal, a dry run that finds
something to remove ends by saying to run the same command again without
`--dry-run`; with nothing to remove, or when stdin or stdout is not a terminal
Expand Down Expand Up @@ -1801,9 +1801,13 @@ If `rocm uninstall` cannot check whether one of those folders is there (for
example, a link on the way loops back on itself), it stops with an error naming
the folder before anything is removed.

When you run `rocm` from a cargo `target` build, `uninstall` skips binary
removal. Pass `--force-dev-binaries` to remove the ROCm CLI binaries in that build
directory.
When you run `rocm` from a cargo `target/debug` or `target/release` folder,
`uninstall` keeps the binaries and says so under `Left alone:`. Pass
`--allow-dev-binaries` to remove the ROCm CLI binaries in that folder (`rocm`,
`rocmd`, `rocm-codex` and `rocm-engine-*`; the rest of the build is left alone).
It overrides nothing else: `--keep-binaries` still keeps them, `--dry-run` still
removes nothing, and the run still needs confirming. `--force-dev-binaries`, the
older spelling, still works.

Before removing anything, `uninstall` prints warnings about two situations:

Expand Down
100 changes: 83 additions & 17 deletions apps/rocm/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,12 +726,18 @@ rocm uninstall --keep-data --yes")]
/// which lives under the data directory; use --keep-data for that.
#[arg(long)]
keep_cache: bool,
/// Allow removing development binaries inside the current build tree.
#[arg(long)]
force_dev_binaries: bool,
/// Also remove the ROCm CLI binaries in the cargo `target/debug` or
/// `target/release` folder `rocm` runs from, which uninstall otherwise
/// keeps. Overrides nothing else: `--keep-binaries`, `--dry-run` and the
/// confirmation still apply.
// `--force-dev-binaries` is the older spelling, kept working and out of
// the help and the completions.
#[arg(long, alias = "force-dev-binaries")]
allow_dev_binaries: bool,
/// Not an option; accepted only to say so. Without it clap answers a
/// mistyped `--force` (a likely guess for skipping the confirmation)
/// with `--force-dev-binaries`, which removes development binaries.
/// with `--force-dev-binaries`, the older spelling of
/// `--allow-dev-binaries`, which removes development binaries.
#[arg(long, hide = true, num_args = 0..=1, default_missing_value = "")]
force: Option<String>,
},
Expand Down Expand Up @@ -2348,7 +2354,9 @@ mod tests {

/// `--force` is a likely guess for "skip the confirmation", and without a
/// declared `--force` clap answers it, and every near miss of it, with
/// `--force-dev-binaries`, which removes the binaries of a development build.
/// `--force-dev-binaries`, the older spelling of `--allow-dev-binaries`, which
/// removes the binaries of a development build. Neither spelling may be
/// offered for a `--force` near miss.
/// The scenario `uninstall-mistyped-force-is-not-answered-with-a-destructive-flag`
/// proves the real binary's output; this pins the parse behind it.
#[test]
Expand All @@ -2357,17 +2365,17 @@ mod tests {
let cli = Cli::try_parse_from(["rocm", "uninstall", "--force"]).unwrap();
let Some(Command::Uninstall {
force,
force_dev_binaries,
allow_dev_binaries,
..
}) = cli.command
else {
panic!("not an uninstall command");
};
assert!(force.is_some() && !force_dev_binaries);
assert!(force.is_some() && !allow_dev_binaries);
let refusal = reject_force(force.is_some()).unwrap_err().to_string();
assert!(refusal.contains("`--force` is not an option"), "{refusal}");
assert!(refusal.contains("--yes"), "{refusal}");
assert!(!refusal.contains("force-dev-binaries"), "{refusal}");
assert!(!refusal.contains("dev-binaries"), "{refusal}");
assert!(reject_force(false).is_ok());
// `--force=x` is the same mistake, and gets the same answer rather than
// a usage line that lists every flag.
Expand All @@ -2391,7 +2399,7 @@ mod tests {
.unwrap_err();
let rendered = error.render().to_string();
assert!(
!rendered.contains("force-dev-binaries"),
!rendered.contains("dev-binaries"),
"{args:?} was answered with the destructive flag:\n{rendered}"
);
assert_eq!(error.exit_code(), 2, "{args:?}");
Expand All @@ -2414,18 +2422,25 @@ mod tests {
.render()
.to_string();
assert!(
rendered.contains("--force") && !rendered.contains("force-dev-binaries"),
rendered.contains("--force") && !rendered.contains("dev-binaries"),
"{args:?} must be steered to the declared `--force`, not the destructive flag:\n{rendered}"
);
}

// The whole flag misspelled is still met with the flag it was meant to be.
let error = cli_command()
.try_get_matches_from(["rocm", "uninstall", "--force-dev-binary"])
.unwrap_err()
.render()
.to_string();
assert!(error.contains("--force-dev-binaries"), "{error}");
// The whole flag misspelled is still met with the flag it was meant to
// be. The older spelling is a hidden alias, which clap still offers as
// a suggestion, so a misspelling of it is answered with it.
for (typed, meant) in [
("--allow-dev-binary", "--allow-dev-binaries"),
("--force-dev-binary", "--force-dev-binaries"),
] {
let error = cli_command()
.try_get_matches_from(["rocm", "uninstall", typed])
.unwrap_err()
.render()
.to_string();
assert!(error.contains(meant), "{typed}: {error}");
}

// It stays out of the help.
let mut command = cli_command();
Expand All @@ -2437,6 +2452,57 @@ mod tests {
assert!(!help.contains("Not an option"), "{help}");
}

/// `--allow-dev-binaries` and its older spelling `--force-dev-binaries` set
/// the same option, and only the new name is advertised. This pins the parse
/// and the help text; what the option does, and that it overrides nothing
/// else, is proven by `a_development_build_keeps_its_binaries_unless_allowed`
/// in `uninstall_plan.rs` and by the scenario
/// `uninstall-development-build-keeps-its-binaries-unless-allowed`.
#[test]
fn both_spellings_allow_dev_binaries_and_only_the_new_one_is_advertised() {
for flag in ["--allow-dev-binaries", "--force-dev-binaries"] {
let cli = Cli::try_parse_from(["rocm", "uninstall", flag]).unwrap();
let Some(Command::Uninstall {
allow_dev_binaries,
force,
..
}) = cli.command
else {
panic!("not an uninstall command");
};
assert!(allow_dev_binaries && force.is_none(), "{flag}");
}
let cli = Cli::try_parse_from(["rocm", "uninstall"]).unwrap();
let Some(Command::Uninstall {
allow_dev_binaries, ..
}) = cli.command
else {
panic!("not an uninstall command");
};
assert!(!allow_dev_binaries, "off unless asked for");

let mut command = cli_command();
let help = command
.find_subcommand_mut("uninstall")
.unwrap()
.render_long_help()
.to_string();
assert!(help.contains("--allow-dev-binaries"), "{help}");
assert!(help.contains("Overrides nothing else"), "{help}");
assert!(!help.contains("force-dev-binaries"), "{help}");

let mut completions = Vec::new();
clap_complete::generate(
clap_complete::Shell::Bash,
&mut crate::completion_command(),
"rocm",
&mut completions,
);
let completions = String::from_utf8(completions).unwrap();
assert!(completions.contains("--allow-dev-binaries"));
assert!(!completions.contains("force-dev-binaries"));
}

/// The help leads with the safe preview, then the real run, as the tip a
/// person reads before running a command that deletes things.
#[test]
Expand Down
4 changes: 2 additions & 2 deletions apps/rocm/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -931,7 +931,7 @@ fn dispatch(cli: Cli) -> Result<()> {
keep_config,
keep_data,
keep_cache,
force_dev_binaries,
allow_dev_binaries,
force,
}) => {
uninstall::reject_force(force.is_some())?;
Expand All @@ -942,7 +942,7 @@ fn dispatch(cli: Cli) -> Result<()> {
keep_config,
keep_data,
keep_cache,
force_dev_binaries,
allow_dev_binaries,
})
}
Some(Command::Completions { shell }) => {
Expand Down
7 changes: 4 additions & 3 deletions apps/rocm/src/uninstall.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,10 @@ impl PathEdit {

/// `rocm uninstall` has no `--force`; the flag is declared (hidden) only so this
/// can answer it. Left undeclared, clap's nearest-name guess for `--force` is
/// `--force-dev-binaries`, which removes development binaries, and a near miss
/// such as `--forc` gets that same suggestion. Declared, those guesses land here
/// instead, before anything is planned or removed.
/// `--force-dev-binaries` (the older spelling of `--allow-dev-binaries`, still
/// accepted and so still a candidate), which removes development binaries, and
/// a near miss such as `--forc` gets that same suggestion. Declared, those
/// guesses land here instead, before anything is planned or removed.
pub(crate) fn reject_force(force: bool) -> Result<()> {
if force {
bail!(
Expand Down
79 changes: 76 additions & 3 deletions apps/rocm/src/uninstall_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ pub(crate) struct UninstallOptions {
pub(crate) keep_config: bool,
pub(crate) keep_data: bool,
pub(crate) keep_cache: bool,
pub(crate) force_dev_binaries: bool,
pub(crate) allow_dev_binaries: bool,
}

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -217,9 +217,9 @@ pub(crate) fn plan_installed_binaries(
return Ok(());
}
let current_exe = current_exe()?;
if is_dev_binary_layout(&current_exe) && !options.force_dev_binaries {
if is_dev_binary_layout(&current_exe) && !options.allow_dev_binaries {
plan.skipped.push(format!(
"binary removal skipped because {} looks like a cargo target build; pass --force-dev-binaries to remove sibling debug/release binaries",
"binary removal skipped because {} looks like a cargo target build; pass --allow-dev-binaries to remove the ROCm CLI binaries in that folder",
current_exe.display()
));
return Ok(());
Expand Down Expand Up @@ -596,6 +596,79 @@ mod tests {
}
}

/// A `rocm` run from a cargo build folder keeps that folder's binaries
/// unless `--allow-dev-binaries` is given, and says which flag removes
/// them; given, it plans the ROCm CLI binaries there and nothing else in
/// the folder; `--keep-binaries` still wins over it. The scenario
/// `uninstall-development-build-keeps-its-binaries-unless-allowed` runs the
/// real binary from such a folder.
#[test]
fn a_development_build_keeps_its_binaries_unless_allowed() -> Result<()> {
let root = tempfile::tempdir()?;
let build = root.path().join("target").join("debug");
fs::create_dir_all(build.join("deps"))?;
let exe = build.join(if cfg!(windows) { "rocm.exe" } else { "rocm" });
let daemon = build.join(if cfg!(windows) { "rocmd.exe" } else { "rocmd" });
for file in [&exe, &daemon, &build.join("rocm.d")] {
fs::write(file, "built")?;
}
let plan_with = |allow_dev_binaries, keep_binaries| -> Result<UninstallPlan> {
let mut plan = UninstallPlan::default();
let options = UninstallOptions {
keep_binaries,
allow_dev_binaries,
..UninstallOptions::default()
};
plan_installed_binaries(&mut plan, &options, || Ok(exe.clone()), None)?;
Ok(plan)
};
// The running binary is planned as an action, except on Windows,
// where it is deleted as the command exits.
let planned = |plan: &UninstallPlan| {
let mut binaries: Vec<PathBuf> = plan
.actions
.iter()
.filter(|entry| entry.kind == "binary")
.map(|entry| entry.path.clone())
.chain(plan.self_delete.clone())
.collect();
binaries.sort();
binaries
};

let kept = plan_with(false, false)?;
assert!(planned(&kept).is_empty(), "{kept:?}");
assert!(
kept.actions.is_empty() && kept.path_edits.is_empty(),
"{kept:?}"
);
assert_eq!(
kept.skipped,
[format!(
"binary removal skipped because {} looks like a cargo target build; pass \
--allow-dev-binaries to remove the ROCm CLI binaries in that folder",
exe.display()
)]
);

let allowed = plan_with(true, false)?;
let mut expected = vec![exe.clone(), daemon];
expected.sort();
assert_eq!(planned(&allowed), expected, "{allowed:?}");
assert!(allowed.skipped.is_empty(), "{allowed:?}");

let still_kept = plan_with(true, true)?;
assert!(planned(&still_kept).is_empty(), "{still_kept:?}");
assert!(
still_kept
.skipped
.iter()
.any(|note| note.starts_with("binary removal disabled by --keep-binaries")),
"{still_kept:?}"
);
Ok(())
}

/// A person at a terminal who dry-runs is told to run the same command again
/// without `--dry-run`: the next step is a rerun, not a control the command
/// line lacks. The scenario `uninstall-07` runs it again at a terminal and
Expand Down
12 changes: 11 additions & 1 deletion docs/manual-testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1433,9 +1433,19 @@ Expected result:
- Passing `--force` to `rocm uninstall` is refused with a message that says it
is not an option and points to `--yes`; a near miss such as `--forc` is an
unknown-argument error (exit 2). Neither
suggests `--force-dev-binaries` (scenario `uninstall-10`).
suggests `--allow-dev-binaries` or its older spelling `--force-dev-binaries`
(scenario `uninstall-10`).
`rocm uninstall --help` ends with examples that start with a dry run
(scenario `uninstall-11`).
- Run from a cargo build, `HOME=$(mktemp -d) ./target/debug/rocm uninstall
--keep-config --keep-data --keep-cache --dry-run` lists nothing to remove and,
under `Left alone:`, says the binaries are kept and to pass
`--allow-dev-binaries`. Adding `--allow-dev-binaries` (or its older spelling
`--force-dev-binaries`) lists `rocm`, `rocmd` and the other ROCm CLI programs
in `target/debug`, and nothing else from the build; without `--dry-run` it
still needs `--yes` or a `y` at the prompt (scenario `uninstall-16`). Do the
real run only with `rocm` copied into a scratch `target/debug` folder, not on
your working build.
- 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
11 changes: 10 additions & 1 deletion docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -1806,7 +1806,7 @@ ends by saying to run it again without `--dry-run`, and `uninstall-07` does that
and checks the listed folder goes; a run whose stdin or stdout is not a terminal, and an
empty plan even at one, name no next step (`uninstall-08`, `uninstall-09`). A
mistyped `--force` is refused with a pointer to `--yes`, and a near miss of it
is not answered with `--force-dev-binaries` (`uninstall-10`); the help starts
is not answered with either spelling of `--allow-dev-binaries` (`uninstall-10`); the help starts
its examples with a dry run (`uninstall-11`). The assistant's uninstall preview
ends differently, since it has no flag to offer: it says removal needs approval,
which `lifecycle_read_mutate_split_is_honest` in [tool_policy.rs](../apps/rocm/src/tool_policy.rs) backs.
Expand All @@ -1817,6 +1817,15 @@ with Ctrl-D, and for a plain no, each together with the config folder being left
in place. They run on the default mock lane; the helper's other callers (`rocm fix`,
the SDK-overwrite prompt) share it and are covered by its unit tests in
[confirm.rs](../crates/rocm-core/src/confirm.rs).
`uninstall-16` (Linux) runs a copy of the binary from a `target/debug` folder in
the scenario's own folder: a dry run keeps that folder's binaries and names
`--allow-dev-binaries`, the older spelling `--force-dev-binaries` lists them,
allowing them without `--yes` outside a terminal is still refused and removes
nothing, and with `--yes` they are removed. It is Linux-only because Windows
deletes the running `rocm.exe` only after the command exits. The plan behind it
is unit-tested by `a_development_build_keeps_its_binaries_unless_allowed` in
[uninstall_plan.rs](../apps/rocm/src/uninstall_plan.rs), including
`--keep-binaries` winning over the flag.
The `uninstall` command does not start the file logger, which would otherwise
create the data folder's `logs` folder before the plan is built:

Expand Down
Loading
Loading