From bf0e3fcd3e6e496933d7bb4e84e06c3169f08c63 Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 14 Nov 2026 10:32:50 +0000 Subject: [PATCH] fix(make): drop the previous checkout's env values and ignore a command-line ENV_SH MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Two review points on #1951. When checkout A's .env was loaded and checkout B's loader runs in the same shell with the same INFINITO_INSTANCE, the instance reset never fired, the preserve loop kept A's INFINITO_CONTAINER as a caller value and B's loader then recorded B as loaded, so the stale value stuck. On a checkout change the loader now sources the previous .env in a subshell and unsets every key whose value is still that file's value; caller overrides differ from it and survive. The previous file is read only when it is a checkout's .env, with scripts/meta/env/load.sh beside it. A command-line ENV_SH beats a := assignment in GNU make and travels to every sub-make through MAKEFLAGS, so make -C B ENV_SH= still ran A's loader. The Makefile now sets it with override. Suppression: nocheck env-loader-internal-guard on the new INFINITO_ENV_LOADED_FILE read, as on the existing guard lines (operator-approved 2026-11-14). MEDUSA covers none of the 4 files; reviewed by hand. 🎯 Fix: 92 % 💥 Break: 4 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: ~100 % 🏆 Score: 88 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 --- Makefile | 2 +- scripts/meta/env/load.sh | 20 +++++++ .../filesystem/makefile/test_target_re.py | 1 + .../meta/env/test_load_cross_checkout.py | 52 +++++++++++++++---- 4 files changed, 63 insertions(+), 12 deletions(-) diff --git a/Makefile b/Makefile index 2373cb41de..8218c56b20 100644 --- a/Makefile +++ b/Makefile @@ -3,7 +3,7 @@ SHELL := /bin/bash .SHELLFLAGS := -euo pipefail -c # SPOT: Global environment is defined in scripts/meta/env/load.sh. -ENV_SH := $(CURDIR)/scripts/meta/env/load.sh +override ENV_SH := $(CURDIR)/scripts/meta/env/load.sh export ENV_SH # For non-interactive bash, BASH_ENV is sourced so the env layer applies to *all* Make recipes. diff --git a/scripts/meta/env/load.sh b/scripts/meta/env/load.sh index e7c3d58963..64a39ecf94 100755 --- a/scripts/meta/env/load.sh +++ b/scripts/meta/env/load.sh @@ -85,6 +85,26 @@ if [[ -n "${_infinito_env_slot}" && -n "${INFINITO_INSTANCE:-}" && "${INFINITO_I fi unset _infinito_env_slot +_infinito_env_previous="${INFINITO_ENV_LOADED_FILE:-}" # nocheck: env-loader-internal-guard +if [[ "${_infinito_env_previous}" == */.env && "${_infinito_env_previous}" != "${_infinito_env_dotenv}" && -f "${_infinito_env_previous}" && -f "${_infinito_env_previous%/.env}/scripts/meta/env/load.sh" ]]; then + while IFS= read -r -d '' _infinito_env_pair; do + _infinito_env_key="${_infinito_env_pair%%=*}" + if [[ "${!_infinito_env_key-}" == "${_infinito_env_pair#*=}" ]]; then + unset "${_infinito_env_key}" + fi + done < <( + set -a + # shellcheck disable=SC1090 + source "${_infinito_env_previous}" >/dev/null 2>&1 + sed -n 's/^\([A-Za-z_][A-Za-z0-9_]*\)=.*/\1/p' "${_infinito_env_previous}" | + while IFS= read -r _infinito_env_key; do + printf '%s=%s\0' "${_infinito_env_key}" "${!_infinito_env_key-}" + done + ) + unset _infinito_env_pair _infinito_env_key +fi +unset _infinito_env_previous + declare -A _infinito_env_preserved=() while IFS= read -r _infinito_env_line; do case "${_infinito_env_line}" in diff --git a/tests/lint/filesystem/makefile/test_target_re.py b/tests/lint/filesystem/makefile/test_target_re.py index b6c1db9d3f..1817425096 100644 --- a/tests/lint/filesystem/makefile/test_target_re.py +++ b/tests/lint/filesystem/makefile/test_target_re.py @@ -15,6 +15,7 @@ class TestTargetRe(unittest.TestCase): for line in ( "SHELL := /bin/bash", "ENV_SH := $(CURDIR)/scripts/meta/env/load.sh", + "override ENV_SH := $(CURDIR)/scripts/meta/env/load.sh", "X ::= y", "X :::= y", "X ?= y", diff --git a/tests/unit/python/scripts/meta/env/test_load_cross_checkout.py b/tests/unit/python/scripts/meta/env/test_load_cross_checkout.py index 7e91c4c086..92a5661f2c 100644 --- a/tests/unit/python/scripts/meta/env/test_load_cross_checkout.py +++ b/tests/unit/python/scripts/meta/env/test_load_cross_checkout.py @@ -41,28 +41,45 @@ class TestCrossCheckout(unittest.TestCase): subprocess.run(["touch", "-d", STAMP, str(dotenv)], check=True) return root - def test_a_second_checkout_loads_its_own_dotenv(self) -> None: - caller = self.checkout(255, "caller_lane") - target = self.checkout(226, "target_lane") + def container_after(self, caller: Path, target: Path, **overrides: str) -> str: script = ( f'source "{caller / LOADER}"\nsource "{target / LOADER}"\n' 'printf "%s" "${INFINITO_CONTAINER}"' ) - - shown = subprocess.run( + return subprocess.run( ["bash", "-c", script], - env=clean_env(), + env=clean_env(**overrides), capture_output=True, text=True, check=True, ).stdout - self.assertEqual("target_lane", shown) - - def test_a_nested_make_reads_its_own_env_loader(self) -> None: + def test_a_second_checkout_loads_its_own_dotenv(self) -> None: caller = self.checkout(255, "caller_lane") + target = self.checkout(226, "target_lane") - shown = subprocess.run( + self.assertEqual("target_lane", self.container_after(caller, target)) + + def test_a_second_checkout_of_the_same_instance_loads_its_own_dotenv( + self, + ) -> None: + caller = self.checkout(226, "caller_lane") + target = self.checkout(226, "target_lane") + + self.assertEqual("target_lane", self.container_after(caller, target)) + + def test_a_caller_override_survives_the_checkout_change(self) -> None: + caller = self.checkout(226, "caller_lane") + target = self.checkout(226, "target_lane") + + shown = self.container_after( + caller, target, INFINITO_CONTAINER="operator_lane" + ) + + self.assertEqual("operator_lane", shown) + + def env_sh_of_make(self, *args: str, **overrides: str) -> str: + return subprocess.run( [ "make", "--no-print-directory", @@ -71,14 +88,27 @@ class TestCrossCheckout(unittest.TestCase): "-n", "--eval", "cross-checkout-probe: ; @: $(info ENV_SH=$(ENV_SH))", + *args, "cross-checkout-probe", ], - env=clean_env(ENV_SH=str(caller / LOADER)), + env=clean_env(**overrides), capture_output=True, text=True, check=True, ).stdout + def test_a_nested_make_reads_its_own_env_loader(self) -> None: + caller = self.checkout(255, "caller_lane") + + shown = self.env_sh_of_make(ENV_SH=str(caller / LOADER)) + + self.assertIn(f"ENV_SH={Path(PROJECT_ROOT) / LOADER}\n", shown) + + def test_a_command_line_env_sh_cannot_select_another_loader(self) -> None: + caller = self.checkout(255, "caller_lane") + + shown = self.env_sh_of_make(f"ENV_SH={caller / LOADER}") + self.assertIn(f"ENV_SH={Path(PROJECT_ROOT) / LOADER}\n", shown)