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
17 changes: 15 additions & 2 deletions crates/rocm-core/src/diagnose.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,13 +1039,26 @@ fn check_6_path_missing(e: &Examination, symptom: &str) -> Diagnosis {
return zero("fix-6-path", "ROCm not on PATH");
}
let fix = if windows {
// The command printed here is the manual-paste twin of the write
// `crate::fix::path_export_windows` performs, and is built the same
// way: read the *User* scope, put `bin_dir` first, write the User
// scope back. `setx PATH "%PATH%;{bin_dir}"` -- what this used to
// advertise -- is not an equivalent: setx truncates the value at 1024
// characters while still exiting 0, and `%PATH%` expands to the
// combined user+system PATH, so pasting it would fold every system
// entry into the user's own PATH. The escape is the same one the real
// write uses, so an apostrophe in `bin_dir` cannot end the PowerShell
// string literal early in the printed copy either.
let escaped_bin_dir = crate::fix::ps_single_quote_escape(&bin_dir);
Fix {
summary: format!("Add {bin_dir} to your User PATH and reopen the shell."),
commands: vec![
format!("setx PATH \"%PATH%;{bin_dir}\""),
format!(
"[Environment]::SetEnvironmentVariable('PATH', '{escaped_bin_dir};' + [Environment]::GetEnvironmentVariable('PATH', 'User'), 'User')"
),
"# Or: System Properties -> Environment Variables -> Path -> Edit -> New."
.to_owned(),
"# `setx` only affects NEW shells; close and reopen this terminal afterwards."
"# A PATH change only affects NEW shells; close and reopen this terminal afterwards."
.to_owned(),
],
fix_id: "fix-6-path".to_owned(),
Expand Down
22 changes: 18 additions & 4 deletions crates/rocm-core/src/fix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -381,14 +381,22 @@ const RECIPES: &[FixRecipe] = &[
commands: &[
"# Linux:",
"echo 'export PATH=\"/opt/rocm/bin:$PATH\"' >> ~/.bashrc",
"# Windows:",
"setx PATH \"%PATH%;C:\\Program Files\\AMD\\ROCm\\<version>\\bin\"",
"# Windows (PowerShell):",
"[Environment]::SetEnvironmentVariable('PATH', 'C:\\Program Files\\AMD\\ROCm\\<version>\\bin;' + [Environment]::GetEnvironmentVariable('PATH', 'User'), 'User')",
],
needs_sudo: false,
needs_reboot: false,
needs_relogin: false,
verify: "rocminfo | head -n 5 && hipcc --version",
notes: &[],
// The Windows command above is the manual-paste twin of what
// `path_export_windows` actually runs -- same scope, same prepend
// ordering -- so a user who pastes it and a user who lets `rocm fix`
// do it end up with the same User PATH. These notes say why it is not
// the `setx` one-liner a Windows user would otherwise reach for.
notes: &[
"Windows: the PowerShell line reads and rewrites the User scope only, and puts the SDK's bin dir first -- the same write `rocm fix fix-6-path` performs. Do not substitute `setx PATH \"%PATH%;...\"`: setx silently truncates the value at 1024 characters while still exiting 0, and %PATH% expands to the combined user+system PATH, so that form would copy every system entry into your User PATH.",
"Windows: a PATH change only takes effect in NEW shells -- close this terminal and reopen it before re-running hipInfo.",
],
doc_url: None,
applies_on: AUTO_ON_LINUX_WINDOWS_AND_WSL,
runner: Some(run_path_export),
Expand Down Expand Up @@ -1937,7 +1945,13 @@ fn windows_path_prepend(bin_dir: &str, user_path: &str) -> String {
/// `bin_dir` -- is an ordinary Windows path, not a contrived edge case, and
/// without this escape it would split the generated `-Command` script
/// mid-statement.
fn ps_single_quote_escape(value: &str) -> String {
///
/// `pub(crate)` because the same escape has to apply to the copy of this
/// command `crate::diagnose` *prints* for the user to paste: the two are one
/// plan on two surfaces, and a path the write survives but the printed
/// version breaks on would be the same ROCMAI-449 bug moved from the write to
/// the documentation.
pub(crate) fn ps_single_quote_escape(value: &str) -> String {
value.replace('\'', "''")
}

Expand Down
159 changes: 158 additions & 1 deletion tests/e2e-cucumber/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,44 @@ pub fn chat_response_is_successful(response: &serde_json::Value) -> bool {
.is_some_and(|choices| !choices.is_empty())
}

/// Opening anchor of the region of `rocm fix fix-6-path` output that describes
/// the User PATH write: the first line `path_export_windows` prints once it has
/// read the existing User PATH and decided what to write.
const PATH_WRITE_PLAN_PREFIX: &str = "Plan: prepend ";

/// Closing anchor: the confirmation `path_export_windows` prints after the
/// write has succeeded.
const PATH_WRITE_CONFIRMATION: &str = "This only takes effect in NEW shells";

/// The slice of `rocm fix fix-6-path` output that is the PATH *write*.
///
/// That is the `Plan: prepend ...` line, the PowerShell script line printed
/// under it, anything that run relayed, and the confirmation line that
/// follows. `None` when either anchor is missing, which is itself a failure
/// the caller should report.
///
/// Scoped rather than matched against the whole output on purpose.
/// `rocm fix <id>` prints the catalog recipe — rationale, command block,
/// notes — *before* it runs anything, so the whole output legitimately
/// contains prose about the fix as well as the fix's own actions. An
/// assertion that `setx` appears nowhere in all of that answers a question
/// about the documentation, not about what the CLI did: it fails when the
/// catalog merely *mentions* `setx` (including, as `fix-6-path`'s notes now
/// do, to warn a user off it), while still being unable to distinguish the
/// write from any other line. This region is the span in which `setx` would
/// mean the regression the scenario is guarding against, and the only span in
/// which the PowerShell call proves the write went the right way.
#[must_use]
pub fn user_path_write_region(output: &str) -> Option<&str> {
let plan = output.find(PATH_WRITE_PLAN_PREFIX)?;
let region = &output[plan..];
let confirmation = region.find(PATH_WRITE_CONFIRMATION)?;
let end = region[confirmation..]
.find('\n')
.map_or(region.len(), |newline| confirmation + newline);
Some(&region[..end])
}

// The report generator lives in its own lean crate (only maud + serde) so xtask
// can reuse it without pulling this harness's heavy tree. Re-export it under the
// original path so `e2e_cucumber::report::{generate, evaluate_xfail}` call sites
Expand All @@ -117,9 +155,128 @@ pub use e2e_report as report;

#[cfg(test)]
mod tests {
use super::{chat_response_is_successful, cli_failure_report, lifecycle_binary_dir};
use super::{
chat_response_is_successful, cli_failure_report, lifecycle_binary_dir,
user_path_write_region,
};
use std::path::Path;

/// What `rocm fix fix-6-path --yes` prints *before* it does anything: the
/// catalog recipe, reproduced here as `print_recipe` lays it out. The
/// notes deliberately name `setx` — warning a Windows user off the
/// `setx PATH "%PATH%;..."` one-liner is the point of them — which is
/// exactly the text a whole-output `setx` ban cannot tell apart from a
/// regressed write.
const PATH_FIX_CATALOG_BLOCK: &str = concat!(
"Fix: fix-6-path -- Add the ROCm/HIP bin directory to PATH\n",
"OS scope: linux, windows, wsl\n",
"Rationale: Windows: the HIP SDK is installed but its bin directory isn't on the User PATH.\n",
"Commands:\n",
" $ # Linux:\n",
" $ echo 'export PATH=\"/opt/rocm/bin:$PATH\"' >> ~/.bashrc\n",
" $ # Windows (PowerShell):\n",
" $ [Environment]::SetEnvironmentVariable('PATH', 'C:\\Program Files\\AMD\\ROCm\\<version>\\bin;' + [Environment]::GetEnvironmentVariable('PATH', 'User'), 'User')\n",
"Flags: rocm fix can run it\n",
"Note: Windows: the PowerShell line reads and rewrites the User scope only. Do not substitute `setx PATH \"%PATH%;...\"`: setx silently truncates the value at 1024 characters.\n",
"Note: Windows: a PATH change only takes effect in NEW shells.\n",
"Verify: rocminfo | head -n 5 && hipcc --version\n",
"\n",
);

/// What `path_export_windows` prints when the write goes the way
/// `cb9f748d` made it go.
const PATH_WRITE_THROUGH_POWERSHELL: &str = concat!(
"Plan: prepend C:\\ROCm\\bin to your User PATH:\n",
" [Environment]::SetEnvironmentVariable('PATH', 'C:\\ROCm\\bin;C:\\Windows', 'User')\n",
"Added C:\\ROCm\\bin to your User PATH. This only takes effect in NEW shells -- close this terminal and reopen it before re-running hipInfo.\n",
);

/// The regression the scenario exists to catch: the same plan/confirmation
/// framing, with the write itself back on `setx`.
const PATH_WRITE_REGRESSED_TO_SETX: &str = concat!(
"Plan: prepend C:\\ROCm\\bin to your User PATH:\n",
" setx PATH \"%PATH%;C:\\ROCm\\bin\"\n",
"Added C:\\ROCm\\bin to your User PATH. This only takes effect in NEW shells -- close this terminal and reopen it before re-running hipInfo.\n",
);

/// The narrowing this helper exists for. The catalog block above the write
/// says `setx` twice, on purpose; the write below it does not. A
/// whole-output `!contains("setx ")` — what diagnose-38 asserted before —
/// fails on this output even though the CLI did exactly the right thing,
/// which is what left `main`'s Windows E2E lane permanently red.
#[test]
fn the_write_region_excludes_the_catalog_prose_that_merely_names_setx() {
let output = format!("{PATH_FIX_CATALOG_BLOCK}{PATH_WRITE_THROUGH_POWERSHELL}");

assert!(
output.contains("setx "),
"the fixture must reproduce the catalog text the old assertion tripped on"
);

let region = user_path_write_region(&output).expect("a successful write has both anchors");

assert!(
region.contains("[Environment]::SetEnvironmentVariable("),
"the write itself must be the PowerShell call:\n{region}"
);
assert!(
!region.contains("setx "),
"no part of the write mentions setx; only the catalog above it does:\n{region}"
);
}

/// ...and the narrowing must not have narrowed the guard away: the same
/// output with the write back on `setx` still fails.
#[test]
fn the_write_region_still_catches_a_write_that_regressed_to_setx() {
let output = format!("{PATH_FIX_CATALOG_BLOCK}{PATH_WRITE_REGRESSED_TO_SETX}");

let region = user_path_write_region(&output).expect("the plan line is still printed");

assert!(
region.contains("setx "),
"a write that went back to setx must land inside the asserted region:\n{region}"
);
assert!(
!region.contains("[Environment]::SetEnvironmentVariable("),
"the PowerShell call is gone in the regressed output:\n{region}"
);
}

/// A run that printed the catalog and then never reached the write has no
/// region at all, rather than an empty one that would vacuously satisfy
/// both halves of the assertion.
#[test]
fn there_is_no_write_region_when_the_write_never_ran() {
assert!(user_path_write_region(PATH_FIX_CATALOG_BLOCK).is_none());
}

/// The plan was printed but the write failed or was aborted: no
/// confirmation, so no region. Half a write must not read as a whole one.
#[test]
fn there_is_no_write_region_when_the_write_was_not_confirmed() {
let output = format!(
"{PATH_FIX_CATALOG_BLOCK}Plan: prepend C:\\ROCm\\bin to your User PATH:\n [Environment]::SetEnvironmentVariable('PATH', 'C:\\ROCm\\bin', 'User')\n(dry-run; not executed)\n"
);

assert!(user_path_write_region(&output).is_none());
}

/// The region stops at the end of the confirmation line, so output printed
/// after the write is outside it for the same reason the catalog above is.
#[test]
fn the_write_region_ends_at_the_confirmation_line() {
let output = format!(
"{PATH_WRITE_THROUGH_POWERSHELL}Tip: `setx` is not used for PATH; see the note above.\n"
);

let region = user_path_write_region(&output).expect("both anchors are present");

assert!(region.starts_with("Plan: prepend C:\\ROCm\\bin"));
assert!(region.ends_with("before re-running hipInfo."), "{region}");
assert!(!region.contains("setx "), "{region}");
}

#[test]
fn lifecycle_packaging_rejects_rocm_and_rocmd_from_different_directories() {
let error = lifecycle_binary_dir(
Expand Down
28 changes: 20 additions & 8 deletions tests/e2e-cucumber/tests/e2e/diagnose_steps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1506,17 +1506,29 @@ async fn assert_path_fix_used_set_environment_variable(world: &mut E2eWorld) {
Some(0),
"expected the real PATH write to succeed:\n{output}"
);
// Scoped to the write, not to the whole command output. `rocm fix <id>`
// prints the catalog recipe first, and `fix-6-path`'s notes name
// `setx PATH "%PATH%;..."` in order to warn a Windows user off it -- so a
// whole-output `!contains("setx ")` reports on the documentation rather
// than on what the CLI did, and fails on a correct run. Taking the region
// between the plan line and the post-write confirmation asserts both
// halves where they mean something: the write went through PowerShell,
// and nothing in the write went through `setx`. Obtaining the region at
// all is what pins the plan line and the confirmation wording cb9f748d
// introduced.
let write_region = e2e_cucumber::user_path_write_region(output).unwrap_or_else(|| {
panic!(
"expected the plan/write region cb9f748d introduced -- a `Plan: prepend ...` line \
through a `This only takes effect in NEW shells` confirmation:\n{output}"
)
});
assert!(
output.contains("[Environment]::SetEnvironmentVariable("),
"expected the PowerShell plan/write line cb9f748d introduced, not setx:\n{output}"
);
assert!(
!output.contains("setx "),
"setx must not appear now that the write goes through PowerShell:\n{output}"
write_region.contains("[Environment]::SetEnvironmentVariable("),
"expected the PowerShell plan/write line cb9f748d introduced, not setx:\n{write_region}"
);
assert!(
output.contains("This only takes effect in NEW shells"),
"expected the post-write wording cb9f748d introduced:\n{output}"
!write_region.contains("setx "),
"setx must not appear in the plan or the write now that it goes through PowerShell:\n{write_region}"
);
}

Expand Down
Loading