From 265c9f5db4489018fdc134a2ec5bf51dd15778fa Mon Sep 17 00:00:00 2001 From: Daniel Han Date: Wed, 10 Jun 2026 08:10:55 -0700 Subject: [PATCH] Fix UnboundLocalError in ROCm version detection dpkg/rpm fallback (#6149) * Fix UnboundLocalError in _detect_rocm_version dpkg/rpm fallback A leftover local import re inside the amd-smi branch made re function local for the whole scope. When amd-smi and hipconfig are absent and dpkg-query or rpm reports rocm-core, the epoch strip at the dpkg/rpm fallback hit re.sub before any local binding existed and crashed the installer with UnboundLocalError. Drop the local import (the module already imports re at top level) and add a regression test covering the dpkg path without hipconfig. * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> --- studio/install_python_stack.py | 1 - tests/studio/install/test_rocm_support.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/studio/install_python_stack.py b/studio/install_python_stack.py index eaab3f5559..0166bfd505 100644 --- a/studio/install_python_stack.py +++ b/studio/install_python_stack.py @@ -212,7 +212,6 @@ def _detect_rocm_version() -> tuple[int, int] | None: env = _amd_smi_env(), ) if result.returncode == 0: - import re m = re.search(r"ROCm version:\s*(\d+)\.(\d+)", result.stdout) if m: return int(m.group(1)), int(m.group(2)) diff --git a/tests/studio/install/test_rocm_support.py b/tests/studio/install/test_rocm_support.py index 02f52e7700..973c0a1e81 100644 --- a/tests/studio/install/test_rocm_support.py +++ b/tests/studio/install/test_rocm_support.py @@ -485,6 +485,21 @@ class TestDetectRocmVersion: result = _detect_rocm_version() assert result == (6, 3) + def test_dpkg_fallback_without_hipconfig(self, tmp_path): + """dpkg rocm-core fallback works when amd-smi and hipconfig are absent + (regression: a shadowing local re import raised UnboundLocalError).""" + + def which(cmd): + return "/usr/bin/dpkg-query" if cmd == "dpkg-query" else None + + mock_result = MagicMock() + mock_result.returncode = 0 + mock_result.stdout = "1:6.3.0-1\n" + with patch.dict(os.environ, {"ROCM_PATH": str(tmp_path / "nonexistent")}): + with patch("shutil.which", side_effect = which): + with patch("subprocess.run", return_value = mock_result): + assert _detect_rocm_version() == (6, 3) + def test_empty_version_file(self, tmp_path): """Empty version file should return None.""" info_dir = tmp_path / ".info"