From 6ea3dc6f498af547e0eb2292d46caf47e476bf0c Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Sat, 14 Nov 2026 07:52:14 +0000 Subject: [PATCH] fix(make): load the env of the checkout make runs in, not the caller's MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit A make -C into another worktree inherited ENV_SH and the loaded-env guard of the calling checkout, so the target worktree ran with the caller's .env (slot, container, subnet). ENV_SH is now always the current checkout's loader, and load.sh records which .env it loaded, so a second checkout reloads its own. The Makefile lints shared no target regex: two of three read VAR := assignments as targets, so ENV_SH := failed the ascending-order lint. One TARGET_RE in the lint package now excludes :=, ::= and :::= for all three, with its own test. 🎯 Fix: 92 % 💥 Break: 3 % 💸 Tokens: – ⏱️ Velocity: – 🛡️ Security: ~100 % 🏆 Score: 89 🚦 Gate: ✅ 90/5, tokens and velocity <= 100, security 100 % Co-Authored-By: Claude Opus 5.5 --- Makefile | 2 +- scripts/meta/env/load.sh | 3 +- tests/lint/filesystem/makefile/__init__.py | 6 +- .../makefile/test_alphabetical_targets.py | 7 +- .../makefile/test_phony_declarations.py | 5 +- .../makefile/test_single_line_comments.py | 5 +- .../filesystem/makefile/test_target_re.py | 25 ++++++ .../meta/env/test_load_cross_checkout.py | 86 +++++++++++++++++++ 8 files changed, 125 insertions(+), 14 deletions(-) create mode 100644 tests/lint/filesystem/makefile/test_target_re.py create mode 100644 tests/unit/python/scripts/meta/env/test_load_cross_checkout.py diff --git a/Makefile b/Makefile index b0e4424ab0..2373cb41de 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 +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 bb525dcfdc..e7c3d58963 100755 --- a/scripts/meta/env/load.sh +++ b/scripts/meta/env/load.sh @@ -47,7 +47,7 @@ if [[ -n "${VENV:-}" && -d "${VENV%/}/bin" ]]; then fi _infinito_env_mtime="$(stat -c %Y "${_infinito_env_dotenv}" 2>/dev/null || echo 0)" -if [[ "${INFINITO_ENV_LOADED:-}" == "1" && "${INFINITO_ENV_LOADED_MTIME:-}" == "${_infinito_env_mtime}" && "${INFINITO_ENV_LOADED_DISTRO:-}" == "${INFINITO_DISTRO:-}" ]]; then # nocheck: env-loader-internal-guard +if [[ "${INFINITO_ENV_LOADED:-}" == "1" && "${INFINITO_ENV_LOADED_FILE:-}" == "${_infinito_env_dotenv}" && "${INFINITO_ENV_LOADED_MTIME:-}" == "${_infinito_env_mtime}" && "${INFINITO_ENV_LOADED_DISTRO:-}" == "${INFINITO_DISTRO:-}" ]]; then # nocheck: env-loader-internal-guard unset _infinito_env_repo_root _infinito_env_dotenv _infinito_env_mtime return 0 fi @@ -127,6 +127,7 @@ if [[ -z "${REQUESTS_CA_BUNDLE:-}" ]]; then fi export INFINITO_ENV_LOADED="1" +export INFINITO_ENV_LOADED_FILE="${_infinito_env_dotenv}" _infinito_env_mtime="$(stat -c %Y "${_infinito_env_dotenv}" 2>/dev/null || echo 0)" export INFINITO_ENV_LOADED_MTIME="${_infinito_env_mtime}" export INFINITO_ENV_LOADED_DISTRO="${INFINITO_DISTRO:-}" diff --git a/tests/lint/filesystem/makefile/__init__.py b/tests/lint/filesystem/makefile/__init__.py index 347dab5787..caa3d2af05 100644 --- a/tests/lint/filesystem/makefile/__init__.py +++ b/tests/lint/filesystem/makefile/__init__.py @@ -1,3 +1,7 @@ +import re + from tests.lint.filesystem import PROJECT_ROOT -__all__ = ["PROJECT_ROOT"] +TARGET_RE = re.compile(r"^(?P[A-Za-z][A-Za-z0-9_-]*)\s*:(?!:*=)") + +__all__ = ["PROJECT_ROOT", "TARGET_RE"] diff --git a/tests/lint/filesystem/makefile/test_alphabetical_targets.py b/tests/lint/filesystem/makefile/test_alphabetical_targets.py index 8ab1a5c525..6e78a7f533 100644 --- a/tests/lint/filesystem/makefile/test_alphabetical_targets.py +++ b/tests/lint/filesystem/makefile/test_alphabetical_targets.py @@ -2,20 +2,17 @@ from __future__ import annotations -import re import unittest from dataclasses import dataclass from typing import TYPE_CHECKING from utils.cache.files import read_text -from . import PROJECT_ROOT +from . import PROJECT_ROOT, TARGET_RE if TYPE_CHECKING: from pathlib import Path -_TARGET_RE = re.compile(r"^(?P[A-Za-z][A-Za-z0-9_-]*)\s*:") - @dataclass(frozen=True) class _Target: @@ -26,7 +23,7 @@ class _Target: def _targets_in(path: Path) -> list[_Target]: out: list[_Target] = [] for index, line in enumerate(read_text(str(path)).splitlines(), start=1): - match = _TARGET_RE.match(line) + match = TARGET_RE.match(line) if match is None: continue out.append(_Target(index, match.group("name"))) diff --git a/tests/lint/filesystem/makefile/test_phony_declarations.py b/tests/lint/filesystem/makefile/test_phony_declarations.py index 87dcc25554..48c521ab0f 100644 --- a/tests/lint/filesystem/makefile/test_phony_declarations.py +++ b/tests/lint/filesystem/makefile/test_phony_declarations.py @@ -10,12 +10,11 @@ from typing import TYPE_CHECKING from utils.cache.files import read_text -from . import PROJECT_ROOT +from . import PROJECT_ROOT, TARGET_RE if TYPE_CHECKING: from pathlib import Path -_TARGET_RE = re.compile(r"^(?P[A-Za-z][A-Za-z0-9_-]*)\s*:(?!=)") _PHONY_RE = re.compile(r"^\.PHONY\s*:\s*(?P.+?)\s*$") _COMMENT_RE = re.compile(r"^\s*#") @@ -43,7 +42,7 @@ def _parse_makefile(path: Path) -> tuple[list[_Target], list[_PhonyDecl], list[s _PhonyDecl(idx, name) for name in phony_match.group("names").split() ) continue - target_match = _TARGET_RE.match(line) + target_match = TARGET_RE.match(line) if target_match is None: continue targets.append(_Target(idx, target_match.group("name"))) diff --git a/tests/lint/filesystem/makefile/test_single_line_comments.py b/tests/lint/filesystem/makefile/test_single_line_comments.py index 12f22a32ac..a2aafda6ea 100644 --- a/tests/lint/filesystem/makefile/test_single_line_comments.py +++ b/tests/lint/filesystem/makefile/test_single_line_comments.py @@ -28,12 +28,11 @@ from typing import TYPE_CHECKING from utils.cache.files import read_text -from . import PROJECT_ROOT +from . import PROJECT_ROOT, TARGET_RE if TYPE_CHECKING: from pathlib import Path -_TARGET_RE = re.compile(r"^(?P[A-Za-z][A-Za-z0-9_-]*)\s*:") _COMMENT_RE = re.compile(r"^\s*#") _NOCHECK_RE = re.compile(r"^\s*#\s*nocheck\s*:") _SCHEMA_RE = re.compile( @@ -70,7 +69,7 @@ def _scan_file(path: Path) -> list[_Violation]: lines = read_text(str(path)).splitlines() out: list[_Violation] = [] for index, line in enumerate(lines): - match = _TARGET_RE.match(line) + match = TARGET_RE.match(line) if match is None: continue block = _comment_block_above(lines, index) diff --git a/tests/lint/filesystem/makefile/test_target_re.py b/tests/lint/filesystem/makefile/test_target_re.py new file mode 100644 index 0000000000..b6c1db9d3f --- /dev/null +++ b/tests/lint/filesystem/makefile/test_target_re.py @@ -0,0 +1,25 @@ +from __future__ import annotations + +import unittest + +from . import TARGET_RE + + +class TestTargetRe(unittest.TestCase): + def test_rules_are_targets(self) -> None: + for line in ("all:", "build: deps", "clean::", "run: VAR := 1"): + with self.subTest(line=line): + self.assertIsNotNone(TARGET_RE.match(line)) + + def test_variable_assignments_are_not_targets(self) -> None: + for line in ( + "SHELL := /bin/bash", + "ENV_SH := $(CURDIR)/scripts/meta/env/load.sh", + "X ::= y", + "X :::= y", + "X ?= y", + "X = y", + ".PHONY: all", + ): + with self.subTest(line=line): + self.assertIsNone(TARGET_RE.match(line)) 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 new file mode 100644 index 0000000000..7e91c4c086 --- /dev/null +++ b/tests/unit/python/scripts/meta/env/test_load_cross_checkout.py @@ -0,0 +1,86 @@ +from __future__ import annotations + +import os +import shutil +import subprocess +import tempfile +import unittest +from pathlib import Path + +from utils.cache.files import PROJECT_ROOT + +LOADER_DIR = Path(PROJECT_ROOT) / "scripts" / "meta" / "env" +LOADER = Path("scripts") / "meta" / "env" / "load.sh" +STAMP = "2026-11-13 21:48:33" + + +def clean_env(**overrides: str) -> dict[str, str]: + env = { + k: v + for k, v in os.environ.items() + if not k.startswith("INFINITO_") + and k not in ("BASH_ENV", "ENV_SH", "PYTHONPATH", "VENV", "PYTHON") + } + env["PYTHONPATH"] = str(PROJECT_ROOT) + env.update(overrides) + return env + + +class TestCrossCheckout(unittest.TestCase): + def checkout(self, instance: int, container: str) -> Path: + root = Path(tempfile.mkdtemp()) + self.addCleanup(shutil.rmtree, root) + (root / LOADER).parent.mkdir(parents=True) + for name in ("load.sh", "python.sh"): + shutil.copy(LOADER_DIR / name, root / LOADER.parent) + dotenv = root / ".env" + dotenv.write_text( + f"INFINITO_INSTANCE={instance}\nINFINITO_CONTAINER={container}\n", + encoding="utf-8", + ) + 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") + script = ( + f'source "{caller / LOADER}"\nsource "{target / LOADER}"\n' + 'printf "%s" "${INFINITO_CONTAINER}"' + ) + + shown = subprocess.run( + ["bash", "-c", script], + env=clean_env(), + 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: + caller = self.checkout(255, "caller_lane") + + shown = subprocess.run( + [ + "make", + "--no-print-directory", + "-C", + str(PROJECT_ROOT), + "-n", + "--eval", + "cross-checkout-probe: ; @: $(info ENV_SH=$(ENV_SH))", + "cross-checkout-probe", + ], + env=clean_env(ENV_SH=str(caller / LOADER)), + capture_output=True, + text=True, + check=True, + ).stdout + + self.assertIn(f"ENV_SH={Path(PROJECT_ROOT) / LOADER}\n", shown) + + +if __name__ == "__main__": + unittest.main()