diff --git a/esphome/build_gen/arduino8266.py b/esphome/build_gen/arduino8266.py index fa06982141..d6cdbdb8f6 100644 --- a/esphome/build_gen/arduino8266.py +++ b/esphome/build_gen/arduino8266.py @@ -14,7 +14,6 @@ from the build flags with the same precedence as the PlatformIO builder. from __future__ import annotations -from contextlib import suppress from dataclasses import dataclass import hashlib import logging @@ -154,6 +153,10 @@ _LWIP_VARIANTS = { 1460, 0, 0, "lwip2-1460" ), } +# The default is PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY's variant: upstream +# has no branch for that spelling (it is the else), so any listed knob wins +# over it -- sntp emits LOW_MEMORY while esp8266 always emits +# HIGHER_BANDWIDTH_LOW_FLASH, and the latter must win as under PlatformIO _LWIP_DEFAULT = _LwipVariant(536, 1, 0, "lwip2-536-feat") # Knob define -> MMU_* defines; first match wins, in insertion order (as @@ -708,8 +711,13 @@ def generate_ld_scripts( _LOGGER.warning("Linker-script preprocessor: %s", result.stderr.strip()) note_persisted = _write_note(stderr_note, result.stderr.strip(), warn=True) else: - with suppress(OSError): + try: stderr_note.unlink(missing_ok=True) + except OSError as err: + # A kept stale note would re-emit an obsolete diagnostic on + # every cache hit; skip the stamp so -E re-derives the truth + _LOGGER.debug("Could not remove %s: %s", stderr_note, err) + note_persisted = False if "SECTIONS" not in result.stdout: # A degenerate zero-exit run must not be stamped as a good cache raise EsphomeError( @@ -735,11 +743,12 @@ def generate_ld_scripts( "Linker-script preprocessor: %s", stderr_note.read_text(encoding="utf-8"), ) - except (OSError, UnicodeDecodeError): + except (OSError, UnicodeDecodeError) as err: _LOGGER.warning( "A cached linker-script preprocessor diagnostic exists at %s " - "but could not be read", + "but could not be read: %s", stderr_note, + err, ) if CORE.testing_mode: diff --git a/esphome/components/esp8266/__init__.py b/esphome/components/esp8266/__init__.py index 0c13625a64..111dc4ccc8 100644 --- a/esphome/components/esp8266/__init__.py +++ b/esphome/components/esp8266/__init__.py @@ -35,7 +35,7 @@ from esphome.platformio.toolchain import copy_ccache_script from esphome.storage_json import StorageJSON from esphome.types import ConfigType -from .boards import BOARDS, ESP8266_LD_SCRIPTS +from .boards import BOARDS, ESP8266_LD_SCRIPTS, board_ld_script from .const import ( CONF_EARLY_PIN_INIT, CONF_ENABLE_SERIAL, @@ -421,7 +421,7 @@ async def to_code(config: ConfigType) -> None: else: # A per-board override preserves a layout the board shipped # with (see d1_wroom_02 in boards.py) - ld_script = board_data.get("ldscript", ld_scripts[1]) + ld_script = board_ld_script(board_data) if ld_script is not None: cg.add_platformio_option("board_build.ldscript", ld_script) diff --git a/esphome/components/esp8266/boards.py b/esphome/components/esp8266/boards.py index 397de60e24..35d9d30e8c 100644 --- a/esphome/components/esp8266/boards.py +++ b/esphome/components/esp8266/boards.py @@ -1,3 +1,5 @@ +from .const import KEY_FLASH_SIZE + FLASH_SIZE_1_MB = 2**20 FLASH_SIZE_512_KB = FLASH_SIZE_1_MB // 2 FLASH_SIZE_2_MB = 2 * FLASH_SIZE_1_MB @@ -182,6 +184,17 @@ for x in platform-espressif8266/boards/*.json; do done | sort """ + +def board_ld_script(board_data: dict) -> str: + """The modern (core > 2.4.2) flash linker script for a board: its + shipped-layout override, else the size default (the no-FS layout). + + Single source of truth for the PlatformIO pinning in __init__ and the + native generator's fallback, so the per-board rule cannot drift. + """ + return board_data.get("ldscript", ESP8266_LD_SCRIPTS[board_data[KEY_FLASH_SIZE]][1]) + + BOARDS = { "agruminolemon": { "name": "Lifely Agrumino Lemon v4", diff --git a/tests/unit_tests/build_gen/test_arduino8266.py b/tests/unit_tests/build_gen/test_arduino8266.py index 8f0035cb3e..4d8dcd2f2a 100644 --- a/tests/unit_tests/build_gen/test_arduino8266.py +++ b/tests/unit_tests/build_gen/test_arduino8266.py @@ -345,6 +345,8 @@ def test_write_project_scanf_float_and_waveform_kept(tmp_path: Path) -> None: ), ("PIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH", "lwip2-1460-feat", 1460, 1, 0), ("PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY_LOW_FLASH", "lwip2-536", 536, 0, 0), + # LOW_MEMORY has no upstream branch: it is the default (else) variant + ("PIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY", "lwip2-536-feat", 536, 1, 0), ], ) def test_build_config_lwip_variants( @@ -359,6 +361,17 @@ def test_build_config_lwip_variants( assert f"LWIP_IPV6={ipv6}" in config.knob_defines +def test_lwip_low_memory_loses_to_listed_knobs() -> None: + """The ordinary SNTP multi-server config: sntp emits LOW_MEMORY, esp8266 + always emits HIGHER_BANDWIDTH_LOW_FLASH, and the listed knob must win + exactly as in platformio-build.py's elif chain.""" + config = _resolve( + "-DPIO_FRAMEWORK_ARDUINO_LWIP2_LOW_MEMORY", + "-DPIO_FRAMEWORK_ARDUINO_LWIP2_HIGHER_BANDWIDTH_LOW_FLASH", + ) + assert config.lwip_lib == "lwip2-1460" + + @pytest.mark.parametrize( ("knob", "expected"), [ @@ -1005,6 +1018,38 @@ def test_generate_ld_scripts_lost_warn_note_vetoes_the_stamp( assert caplog.text.count("Linker-script preprocessor: warning: something") == 2 +def test_generate_ld_scripts_unremovable_stale_note_vetoes_the_stamp( + tmp_path: Path, caplog: pytest.LogCaptureFixture +) -> None: + """A stale warn note that cannot be removed skips the stamp, so the + obsolete diagnostic is not re-emitted on cache hits forever.""" + paths = _make_framework(tmp_path) + _set_flags() + warn = MagicMock(returncode=0, stdout=_COMMON_LD_H_OUTPUT, stderr="warning: old") + clean = MagicMock(returncode=0, stdout=_COMMON_LD_H_OUTPUT, stderr="") + with patch.object(arduino8266.subprocess, "run", return_value=warn): + _run_generate_ld_scripts(paths) + + real_unlink = Path.unlink + + def fail_note_unlink(self: Path, missing_ok: bool = False) -> None: + if self.name.endswith(".stderr"): + raise OSError("locked") + real_unlink(self, missing_ok=missing_ok) + + # Flags changed -> regenerate; clean stderr but the stale note is stuck + _set_flags("-DVTABLES_IN_DRAM") + with ( + patch.object(arduino8266.subprocess, "run", return_value=clean), + patch.object(Path, "unlink", fail_note_unlink), + ): + _run_generate_ld_scripts(paths) + # Unstamped: the next build re-runs -E instead of trusting the cache + with patch.object(arduino8266.subprocess, "run", return_value=clean) as run3: + _run_generate_ld_scripts(paths) + run3.assert_called_once() + + def test_build_config_mmu_knob_with_raw_mmu_flag_raises() -> None: """A variant knob plus a raw MMU_* define would split the compile line from the linker script; refuse like the no-knob case."""