[espidf] Prune tool download cache after install to shrink the ESP-IDF cache (#17661)

This commit is contained in:
Jonathan Swoboda
2026-07-17 22:49:50 -04:00
committed by GitHub
parent bacc223360
commit 70421fb14b
2 changed files with 33 additions and 1 deletions
+10
View File
@@ -679,6 +679,16 @@ def _check_esphome_idf_framework_install(
)
raise RuntimeError(f"ESP-IDF {version} framework installation failure")
# idf_tools.py extracts tool archives from <IDF_TOOLS_PATH>/dist into tools/; the
# archives are not needed afterward and, already compressed, dominate the cached install.
# Best-effort: a failure to prune must not fail an otherwise successful install.
try:
rmdir(
get_idf_tools_path() / "dist", msg="Remove ESP-IDF tool download cache"
)
except RuntimeError as err:
_LOGGER.debug("Could not remove ESP-IDF tool download cache: %s", err)
_write_stamp(env_stamp_file, stamp_info)
return framework_path, install
+23 -1
View File
@@ -317,7 +317,7 @@ def espidf_mocks(setup_core: Path):
# extracted-marker touch writes into.
_get_framework_path(_IDF_VERSION).mkdir(parents=True, exist_ok=True)
with (
patch("esphome.espidf.framework.rmdir"),
patch("esphome.espidf.framework.rmdir") as rmdir_mock,
patch(
"esphome.espidf.framework.download_from_mirrors",
return_value="https://example.com/idf.tar.xz",
@@ -344,6 +344,7 @@ def espidf_mocks(setup_core: Path):
run_ok=run_ok,
tool_paths=tool_paths,
clone=clone,
rmdir=rmdir_mock,
)
@@ -358,6 +359,27 @@ def test_check_esp_idf_install_fresh(espidf_mocks: SimpleNamespace) -> None:
espidf_mocks.extract.assert_called_once()
espidf_mocks.venv.assert_called_once()
espidf_mocks.clone.assert_not_called()
# the tool download cache (<IDF_TOOLS_PATH>/dist) is pruned after install
espidf_mocks.rmdir.assert_any_call(
get_idf_tools_path() / "dist", msg="Remove ESP-IDF tool download cache"
)
def test_check_esp_idf_install_dist_prune_failure_ignored(
espidf_mocks: SimpleNamespace,
) -> None:
"""A failure to prune the tool download cache must not fail the install."""
tools_dist = get_idf_tools_path() / "dist"
def rmdir_side_effect(directory: Path, msg: str | None = None) -> None:
if directory == tools_dist:
raise RuntimeError("cannot remove dist")
espidf_mocks.rmdir.side_effect = rmdir_side_effect
# install still succeeds despite the failed prune
framework_path, _ = check_esp_idf_install(_IDF_VERSION, force=True)
assert framework_path == _get_framework_path(_IDF_VERSION)
def test_check_esp_idf_install_git_source(espidf_mocks: SimpleNamespace) -> None: