diff --git a/esphome/build_helpers/ccache.py b/esphome/build_helpers/ccache.py index 2fbddd7008..1915b4a20c 100644 --- a/esphome/build_helpers/ccache.py +++ b/esphome/build_helpers/ccache.py @@ -99,10 +99,7 @@ def effective_ccache_basedir() -> str: from esphome.core import CORE raw = os.environ.get("CCACHE_BASEDIR") - if raw is not None: - # A degenerate value ("", "/", relative) must not be used for - # substring stripping; fall back to the resolved build path - if len(Path(raw).parts) > 1: - return raw - return str(Path(CORE.build_path).resolve()) + if raw is not None and Path(raw).is_absolute() and len(Path(raw).parts) > 1: + return raw + # Unset or degenerate ("", "/", relative): fall back to the build path return str(Path(CORE.build_path).resolve()) diff --git a/tests/unit_tests/build_helpers/test_ccache.py b/tests/unit_tests/build_helpers/test_ccache.py index 23f38019aa..a1ff40435e 100644 --- a/tests/unit_tests/build_helpers/test_ccache.py +++ b/tests/unit_tests/build_helpers/test_ccache.py @@ -130,6 +130,6 @@ def test_effective_ccache_basedir_prefers_user_value(tmp_path: Path) -> None: with patch.dict(os.environ, {}, clear=True): assert ccache.effective_ccache_basedir() == str(tmp_path.resolve()) # Degenerate values would strip substrings ccache never rewrites - for bad in ("", "/"): + for bad in ("", "/", "a/b"): with patch.dict(os.environ, {"CCACHE_BASEDIR": bad}, clear=True): assert ccache.effective_ccache_basedir() == str(tmp_path.resolve())