From 365dcba50605a60ee80fa1ffb9d8f69e79d292c6 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Tue, 25 Aug 2026 20:48:28 -0500 Subject: [PATCH] Skip compile-DB entries with null fields instead of raising --- esphome/build_helpers/pch.py | 13 +++++++++---- tests/unit_tests/build_gen/test_espidf.py | 11 +++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/esphome/build_helpers/pch.py b/esphome/build_helpers/pch.py index e30ca0821b..f840457111 100644 --- a/esphome/build_helpers/pch.py +++ b/esphome/build_helpers/pch.py @@ -203,16 +203,21 @@ def pch_compile_command( e for e in entries if isinstance(e, dict) - and e.get("file", "").replace("\\", "/").startswith(src_prefix) - and e.get("file", "").endswith(CXX_SOURCE_SUFFIXES) + and isinstance(e.get("file"), str) + and e["file"].replace("\\", "/").startswith(src_prefix) + and e["file"].endswith(CXX_SOURCE_SUFFIXES) ), None, ) if entry is None: _LOGGER.warning("No src C++ entry in the compile database, skipping pch") return None - cmd_dir = Path(entry.get("directory") or build_dir) - tokens = expand_response_files(split_command(entry.get("command", "")), cmd_dir) + directory = entry.get("directory") + cmd_dir = Path(directory) if isinstance(directory, str) and directory else build_dir + command = entry.get("command") + tokens = expand_response_files( + split_command(command if isinstance(command, str) else ""), cmd_dir + ) # A DB recorded with ccache enabled prefixes the compiler with the # launcher; the .gch must be compiled directly if tokens and is_launcher(tokens[0]): diff --git a/tests/unit_tests/build_gen/test_espidf.py b/tests/unit_tests/build_gen/test_espidf.py index 852526d648..a01c35c18c 100644 --- a/tests/unit_tests/build_gen/test_espidf.py +++ b/tests/unit_tests/build_gen/test_espidf.py @@ -690,6 +690,17 @@ def test_pch_compile_command_rejects_unusable_entries(tmp_path: Path) -> None: _, cmd_dir = pch_compile_command(build, header, gch) assert cmd_dir == build + # Corrupted entries with null fields must skip, not raise + db.write_text( + json.dumps( + [ + {"file": None, "command": "g++ -c x.cpp", "directory": None}, + {"file": src_file, "command": None, "directory": None}, + ] + ) + ) + assert pch_compile_command(build, header, gch) is None + # arguments-style entry (allowed by the spec, unused by CMake) db.write_text( json.dumps([{"arguments": ["g++", "-c", src_file], "file": src_file}])