Skip to content
Closed
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
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,9 @@ exits without prompting or making changes.

`adopt` registers an existing TheRock-based Python environment as a managed
runtime. It does not work with standard ROCm package installs (for example,
`/opt/rocm`); use `rocm install sdk` instead.
`/opt/rocm`); use `rocm install sdk` instead. Without `--root`, the environment
folder is the one that holds the `--python` interpreter's `bin` (or `Scripts`)
folder; an interpreter anywhere else is refused until you pass `--root`.

### Disk space

Expand Down
2 changes: 1 addition & 1 deletion apps/rocm/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -888,7 +888,7 @@ left, so it cannot undo more than one activation."
/// Python executable inside the existing ROCm environment.
#[arg(long)]
python: PathBuf,
/// Optional SDK root path.
/// Python environment folder. Taken from --python when that sits in a bin or Scripts folder; required otherwise.
#[arg(long)]
root: Option<PathBuf>,
/// Runtime id to assign.
Expand Down
75 changes: 63 additions & 12 deletions apps/rocm/src/comfyui/runtime_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use crate::therock;
use anyhow::{Context, Result, bail};
use rocm_core::{
AppPaths, RocmCliConfig, runtime_is_linux, runtime_is_windows, runtime_path_list_join,
runtime_path_list_split, runtime_paths_equivalent,
runtime_path_list_split, runtime_paths_equivalent, runtime_python_env_root_for_executable,
};
use std::ffi::OsString;
use std::path::{Path, PathBuf};
Expand Down Expand Up @@ -237,7 +237,7 @@ pub(super) fn runtime_environment_for_manifest(
}

let mut env = ComfyUiRuntimeEnvironment {
venv_root: infer_python_env_root(&manifest.python_executable),
venv_root: runtime_python_env_root_for_executable(&manifest.python_executable),
rocm_root: Some(manifest.runtime_root.clone()),
path_entries: Vec::new(),
library_entries: Vec::new(),
Expand All @@ -254,7 +254,7 @@ pub(super) fn runtime_environment_from_runtime(
python: &Path,
) -> ComfyUiRuntimeEnvironment {
let mut env = ComfyUiRuntimeEnvironment {
venv_root: infer_python_env_root(python),
venv_root: runtime_python_env_root_for_executable(python),
rocm_root: manifest
.rocm_sdk
.as_ref()
Expand Down Expand Up @@ -307,15 +307,6 @@ fn collect_rocm_runtime_paths(root: &Path, env: &mut ComfyUiRuntimeEnvironment)
}
}

fn infer_python_env_root(python: &Path) -> Option<PathBuf> {
let bin_dir = python.parent()?;
let name = bin_dir.file_name()?.to_string_lossy().to_ascii_lowercase();
if matches!(name.as_str(), "scripts" | "bin") {
return bin_dir.parent().map(Path::to_path_buf);
}
None
}

pub(super) fn apply_runtime_environment(
command: &mut Command,
env: &ComfyUiRuntimeEnvironment,
Expand Down Expand Up @@ -964,4 +955,64 @@ mod tests {
.find(|(name, _)| name.to_string_lossy() == key)
.and_then(|(_, value)| value.map(OsString::from))
}

/// The environment folder ComfyUI exports as `VIRTUAL_ENV`, by the folder
/// the interpreter sits in. Today only a `bin` or `Scripts` folder
/// (any case) identifies a venv; anything else leaves `VIRTUAL_ENV` unset
/// rather than pointing it at a guessed folder.
#[test]
fn runtime_environment_exports_virtual_env_only_for_a_bin_or_scripts_folder() -> Result<()> {
let cases: [(&str, Option<&str>); 7] = [
("/opt/venv/bin/python", Some("/opt/venv")),
("/opt/venv/Scripts/python.exe", Some("/opt/venv")),
("/opt/venv/SCRIPTS/python.exe", Some("/opt/venv")),
("/opt/venv/BIN/python", Some("/opt/venv")),
("/opt/venv/tools/python", None),
("python", None),
("/python", None),
];
for (python, expected) in cases {
let python = PathBuf::from(python);
let manifest = minimal_runtime_manifest(&python);

let env = runtime_environment_from_runtime(&manifest, &python);
let mut command = Command::new(&python);
apply_runtime_environment(&mut command, &env)?;

assert_eq!(
command_env_value(&command, "VIRTUAL_ENV").as_deref(),
expected.map(std::ffi::OsStr::new),
"{}",
python.display()
);
}
Ok(())
}

fn minimal_runtime_manifest(python: &Path) -> therock::InstalledRuntimeManifest {
therock::InstalledRuntimeManifest {
runtime_key: "therock-release-gfx120x-all".to_owned(),
runtime_id: "therock-release:gfx120X-all".to_owned(),
channel: "release".to_owned(),
format: "wheel".to_owned(),
family: "gfx120X-all".to_owned(),
family_source: "test".to_owned(),
version: "7.13.0a20260511".to_owned(),
install_root: PathBuf::from("/nonexistent/runtime"),
selected_artifact_url: "https://example.invalid/simple".to_owned(),
source_layout_generation: None,
index_url: None,
tarball_file_name: None,
python_launcher: None,
python_executable: Some(python.display().to_string()),
pip_cache_dir: None,
rocm_sdk: None,
sdk_torch: None,
wheel_composition: None,
read_only: false,
imported_from: None,
devel: true,
installed_at_unix_ms: 100,
}
}
}
82 changes: 71 additions & 11 deletions apps/rocm/src/runtime_adopt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@ use std::fs;
use std::path::{Path, PathBuf};

use anyhow::{Context, Result, bail};
use rocm_core::{AppPaths, RocmCliConfig, managed_pip_cache_dir, runtime_python_executable_in_env};
use rocm_core::{
AppPaths, RocmCliConfig, managed_pip_cache_dir, runtime_python_env_root_for_executable,
runtime_python_executable_in_env,
};

use crate::runtime_registry::{
paths_equivalent, runtime_manifest_path, runtime_registry_dir,
Expand Down Expand Up @@ -230,7 +233,9 @@ fn resolve_adopt_python_input(input: &Path) -> Result<(PathBuf, Option<PathBuf>)
return Ok((python, Some(env_root)));
}
if absolute.is_file() {
let inferred_root = infer_python_env_root(&absolute);
// `None` for an interpreter outside a `bin`/`Scripts` folder: the
// request then asks for `--root` rather than adopting a guessed folder.
let inferred_root = runtime_python_env_root_for_executable(&absolute);
return Ok((absolute, inferred_root));
}
bail!(
Expand All @@ -239,15 +244,6 @@ fn resolve_adopt_python_input(input: &Path) -> Result<(PathBuf, Option<PathBuf>)
);
}

fn infer_python_env_root(python_executable: &Path) -> Option<PathBuf> {
let bin_dir = python_executable.parent()?;
let bin_name = bin_dir.file_name()?.to_string_lossy();
if bin_name.eq_ignore_ascii_case("Scripts") || bin_name == "bin" {
return bin_dir.parent().map(Path::to_path_buf);
}
bin_dir.parent().map(Path::to_path_buf)
}

pub(crate) fn adopt_runtime_from_probe(
paths: &AppPaths,
request: AdoptRuntimeRequest,
Expand Down Expand Up @@ -819,4 +815,68 @@ mod tests {
let _ = fs::remove_dir_all(root);
Ok(())
}

/// `rocm runtimes adopt --python <file>` without `--root` takes the
/// environment folder from the shared rule (see
/// `rocm_core::runtime_python_env_root_for_executable`): only an
/// interpreter in a `bin` or `Scripts` folder, in any case, names one. An
/// interpreter in an unrelated folder yields no root, and the request is
/// then refused with `--root` guidance rather than adopting a guessed
/// folder.
#[test]
fn adopt_python_file_infers_a_root_only_from_a_bin_or_scripts_folder() -> Result<()> {
let (root, _) = test_paths("runtime-adopt-infer-root");
for (folder, expected_root) in [
("bin", true),
("Scripts", true),
("SCRIPTS", true),
("tools", false),
] {
let env_root = root.join(format!("env-{folder}"));
let python = env_root.join(folder).join("python");
fs::create_dir_all(python.parent().unwrap())?;
fs::write(&python, "python")?;

let (resolved, inferred) = resolve_adopt_python_input(&python)?;

assert_eq!(resolved, python, "{folder}");
assert_eq!(
inferred.as_deref(),
expected_root.then_some(env_root.as_path()),
"{folder}"
);
}

let _ = fs::remove_dir_all(root);
Ok(())
}

/// What no inferred root costs the user: the request is refused and asks
/// for `--root`, and an explicit `--root` still adopts the same
/// interpreter.
#[test]
fn adopt_request_without_an_inferred_root_asks_for_root() {
let python = PathBuf::from("python");
let request = |install_root: Option<PathBuf>| {
infer_adopt_runtime_request(
python.clone(),
install_root,
None,
None,
None,
false,
&therock::RocmSdkPythonProbe {
rocm_sdk_version: Some("7.13.0".to_owned()),
resolved_target_family: Some("gfx120X-all".to_owned()),
..therock::RocmSdkPythonProbe::default()
},
)
};

let error = request(None).unwrap_err().to_string();
assert!(error.contains("pass --root"), "{error}");

let explicit = request(Some(PathBuf::from("/opt/rocm-env"))).unwrap();
assert_eq!(explicit.install_root, PathBuf::from("/opt/rocm-env"));
}
}
3 changes: 2 additions & 1 deletion crates/rocm-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,7 +205,8 @@ pub use runtime::{
runtime_path_sort_key, runtime_path_text_is_absolute_for_host,
runtime_path_text_is_absolute_for_platform, runtime_paths_equivalent,
runtime_python_activation_hint, runtime_python_activation_script, runtime_python_bin_dir_name,
runtime_python_env_bin_dir, runtime_python_executable_in_env, runtime_python_executable_name,
runtime_python_env_bin_dir, runtime_python_env_root_for_executable,
runtime_python_executable_in_env, runtime_python_executable_name,
runtime_rocm_library_filename, shell_command_for_host, user_runtime_dir,
};
pub use service_identity::{ServiceId, generate_service_id};
Expand Down
76 changes: 76 additions & 0 deletions crates/rocm-core/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,31 @@ pub fn runtime_python_executable_in_env(env_root: &Path) -> PathBuf {
runtime_python_env_bin_dir(env_root).join(runtime_python_executable_name())
}

/// The environment folder an interpreter belongs to, found from the
/// interpreter's own path: the inverse of [`runtime_python_executable_in_env`]
/// for a caller that holds only the interpreter.
///
/// An interpreter belongs to an environment only when it sits directly in that
/// environment's `bin` or `Scripts` folder, and the answer is that folder's
/// parent. Both layouts are accepted, in any letter case, whatever the host: a
/// recorded interpreter path is not always from this host's layout, and a
/// Windows file system does not tell `Scripts` from `SCRIPTS`.
///
/// Anything else gives `None`: an interpreter in some other folder, a bare file
/// name, or one at the file system root. A guess would name a folder that is
/// not an environment, and the caller would then export it as `VIRTUAL_ENV`,
/// look for `lib/python3.*` under it, or register it as a runtime. `None` lets
/// each caller refuse in its own words instead.
pub fn runtime_python_env_root_for_executable(python: &Path) -> Option<PathBuf> {
let bin_dir = python.parent()?;
let name = bin_dir.file_name()?.to_str()?;
if name.eq_ignore_ascii_case("bin") || name.eq_ignore_ascii_case("Scripts") {
bin_dir.parent().map(Path::to_path_buf)
} else {
None
}
}

pub fn runtime_python_activation_script(env_root: &Path) -> PathBuf {
let script = if runtime_is_windows() {
"activate.bat"
Expand Down Expand Up @@ -1026,6 +1051,57 @@ mod tests {
use super::*;
use std::fs;

/// The one rule every caller shares. Paths are built with `join`, not
/// written with a separator, so the table means the same on every host.
#[test]
fn env_root_for_executable_needs_a_bin_or_scripts_folder() {
let venv = Path::new("opt").join("venv");
for (folder, python) in [
("bin", "python"),
("BIN", "python"),
("Scripts", "python.exe"),
("SCRIPTS", "python.exe"),
("scripts", "python.exe"),
] {
assert_eq!(
runtime_python_env_root_for_executable(&venv.join(folder).join(python)),
Some(venv.clone()),
"{folder}"
);
}
for folder in ["tools", "bin2", "my-bin", "venv"] {
assert_eq!(
runtime_python_env_root_for_executable(&venv.join(folder).join("python")),
None,
"{folder}"
);
}
// A bare file name has an empty parent, and a root-level file's parent
// is the file system root: neither is inside a `bin` or `Scripts`.
assert_eq!(
runtime_python_env_root_for_executable(Path::new("python")),
None
);
assert_eq!(
runtime_python_env_root_for_executable(
&Path::new(std::path::MAIN_SEPARATOR_STR).join("python")
),
None
);
}

/// The inverse really is the inverse: the interpreter this host's layout
/// puts in an environment leads back to that environment.
#[test]
fn env_root_for_executable_inverts_the_hosts_layout() {
let env_root = Path::new("opt").join("venv");
let python = runtime_python_executable_in_env(&env_root);
assert_eq!(
runtime_python_env_root_for_executable(&python).as_deref(),
Some(normalize_runtime_path_for_host(&env_root).as_path())
);
}

fn scratch_dir(name: &str) -> PathBuf {
let root = std::env::temp_dir().join(format!(
"rocm-core-resolve-{name}-{}-{:?}",
Expand Down
2 changes: 1 addition & 1 deletion docs/architecture.md
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ Further `rocm-core` modules, each with its own doc comment except where noted:
- [`proc_lifecycle.rs`](../crates/rocm-core/src/proc_lifecycle.rs) — verified, bounded termination of a process by persisted PID, refusing to signal a PID whose start-time identity no longer matches.
- [`report.rs`](../crates/rocm-core/src/report.rs) — the content of a Doctor report and the rule deciding whether one may exist; it sends nothing.
- [`report_delivery.rs`](../crates/rocm-core/src/report_delivery.rs) — how a report leaves the machine: building its link and deciding whether a browser may be started.
- [`runtime.rs`](../crates/rocm-core/src/runtime.rs) — the host-platform layer: `RuntimePlatform`/`RuntimeHost`, OS-specific executable and Python-environment names, default config/data/cache directories, and path-text and path-list helpers (no module doc comment).
- [`runtime.rs`](../crates/rocm-core/src/runtime.rs) — the host-platform layer: `RuntimePlatform`/`RuntimeHost`, OS-specific executable and Python-environment names (including `runtime_python_env_root_for_executable`, the one rule for finding an environment's folder from its interpreter's path: only an interpreter directly inside a `bin` or `Scripts` folder, in any case, has one, and everything else gets `None`; the adopt command, the ComfyUI runtime environment and the vLLM launch all call it rather than keeping a copy, since the engine crate cannot reach `apps/rocm`), default config/data/cache directories, and path-text and path-list helpers (no module doc comment).
- [`terminal.rs`](../crates/rocm-core/src/terminal.rs) — one ECMA-48 walk over untrusted terminal output, shared by every caller that has to quote or re-render it (`serve_summary.rs`, `vllm/startup_diagnostics.rs`, `vllm_oom.rs`, `diagnose/engine.rs`).
- [`user_path.rs`](../crates/rocm-core/src/user_path.rs) — removing the install directory from the per-user Windows `Path` that the installer added it to.
- [`uv.rs`](../crates/rocm-core/src/uv.rs) — acquiring the `uv` package manager into the managed cache, plus the argument/environment helpers `apps/rocm` and the vLLM engine share.
Expand Down
4 changes: 4 additions & 0 deletions docs/testing.md
Original file line number Diff line number Diff line change
Expand Up @@ -907,6 +907,10 @@ rocm runtimes adopt \
--runtime-key adopted-release-pip-gfx120x-all-7-13-0
```

`--root` may be left out when `--python` sits directly in the environment's `bin`
(or `Scripts`) folder; an interpreter anywhere else is refused until `--root` is
given (scenario `runtime-23`).

Focused unit coverage:

```bash
Expand Down
Loading
Loading