diff --git a/scripts/known_good/BUILD b/scripts/known_good/BUILD new file mode 100644 index 00000000000..12a21236bac --- /dev/null +++ b/scripts/known_good/BUILD @@ -0,0 +1,48 @@ +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* +load("@rules_python//python:defs.bzl", "py_binary", "py_library") +load("@score_tooling//python_basics:defs.bzl", "score_py_pytest") + +# Library target: the known_good package (models + generators). +# Used as a dep by //scripts/tooling and by the resolve_deps binary below. +py_library( + name = "known_good", + srcs = glob( + ["**/*.py"], + exclude = ["tests/**"], + ), + visibility = ["//visibility:public"], +) + +# Tests for the known_good package (currently: ResolvedDependencies). +# Not part of //scripts/tooling:tooling_tests, whose glob is scoped to scripts/tooling/tests/. +score_py_pytest( + name = "known_good_tests", + srcs = glob(["tests/**/*.py"]), + data = ["//:known_good.json"], + pytest_config = "//:pyproject.toml", + deps = [":known_good"], +) + +# Runnable binary for the resolve + inject workflow. +# Stage 1 (export): bazel run //scripts/known_good:resolve_deps -- \ +# --mod-graph graph.json --export artifacts/resolved_versions.json +# Stage 2 (inject): bazel run //scripts/known_good:resolve_deps -- \ +# _module/MODULE.bazel --resolved-deps _resolved_deps/ +py_binary( + name = "resolve_deps", + srcs = ["resolved_dependencies.py"], + main = "resolved_dependencies.py", + visibility = ["//visibility:public"], + deps = [":known_good"], +) diff --git a/scripts/known_good/resolved_dependencies.py b/scripts/known_good/resolved_dependencies.py new file mode 100644 index 00000000000..05dc7801768 --- /dev/null +++ b/scripts/known_good/resolved_dependencies.py @@ -0,0 +1,468 @@ +#!/usr/bin/env python3 +# ******************************************************************************* +# Copyright (c) 2026 Contributors to the Eclipse Foundation +# +# See the NOTICE file(s) distributed with this work for additional +# information regarding copyright ownership. +# +# This program and the accompanying materials are made available under the +# terms of the Apache License Version 2.0 which is available at +# https://www.apache.org/licenses/LICENSE-2.0 +# +# SPDX-License-Identifier: Apache-2.0 +# ******************************************************************************* +"""Resolved dependency versions from the reference_integration root. + +Provides :class:`ResolvedDependencies`, which holds the resolved version/commit per +dependency (sourced from ref_int's root — either ``known_good.json`` for local runs, +or the Stage-1 ``stage1-resolved-deps`` artifact for CI runs), and exposes an interface +to **scan** an individual module's ``MODULE.bazel`` and **overwrite** the declared +dependency versions to match the resolved set by appending the matching +``git_override`` / ``single_version_override`` directives. + +The injection operates on the CI checkout of the module — it is never committed back +to the module's released sources. +""" + +from __future__ import annotations + +import argparse +import json +import logging +import re +from pathlib import Path + +from known_good.models.known_good import load_known_good +from known_good.models.module import Module + +_HERE = Path(__file__).resolve().parent + +# Marker delimiting the block we append, so injection is idempotent / detectable. +INJECTION_BEGIN = "# --- BEGIN ref_int resolved-deps injection ---" +INJECTION_END = "# --- END ref_int resolved-deps injection ---" + + +def generate_override_directive(module: Module, repo_commit_dict: dict[str, str] | None = None) -> str | None: + """Return the override directive (single_version_override / git_override) for a module. + + Returns just the override call without a preceding ``bazel_dep(...)`` line, so the + same logic can be reused both to build ref_int's score_modules_*.MODULE.bazel files + and to inject overrides into a module's own MODULE.bazel where bazel_dep is already + declared (see :meth:`ResolvedDependencies.overwrite`). + + Returns ``None`` when the module has neither a usable version nor a valid repo+commit. + """ + repo_commit_dict = repo_commit_dict or {} + commit = module.hash + + if module.repo in repo_commit_dict: + commit = repo_commit_dict[module.repo] + + patches_lines = "" + if module.bazel_patches: + patches_lines = " patches = [\n" + for patch in module.bazel_patches: + patches_lines += f' "{patch}",\n' + patches_lines += " ],\n" + patch_strip_line = " patch_strip = 1,\n" if patches_lines else "" + + if module.version: + return ( + "single_version_override(\n" + f' module_name = "{module.name}",\n' + f"{patch_strip_line}" + f"{patches_lines}" + f' version = "{module.version}",\n' + ")\n" + ) + + if not module.repo or not commit: + logging.warning( + "Skipping module %s with missing repo or commit: repo=%s, commit=%s", + module.name, + module.repo, + commit, + ) + return None + + if not re.match(r"^[a-fA-F0-9]{7,40}$", commit): + logging.warning("Skipping module %s with invalid commit hash: %s", module.name, commit) + return None + + return ( + "git_override(\n" + f' module_name = "{module.name}",\n' + f' commit = "{commit}",\n' + f"{patch_strip_line}" + f"{patches_lines}" + f' remote = "{module.repo}",\n' + ")\n" + ) + + +# The single file that carries the resolved set from Stage 1 (resolve) to Stage 2 +# (per-module validation). It is the only handoff needed: first-party commits + +# third-party resolved versions, merged. The lock travels alongside only as evidence. +MANIFEST_NAME = "resolved_versions.json" + +# Built-in / non-registry modules that must not be given a single_version_override. +_SKIP_MODULES = {"bazel_tools"} + +# Capture the module name from any ``bazel_dep(name = "...")`` call (name is the first arg). +_BAZEL_DEP_RE = re.compile(r'bazel_dep\(\s*name\s*=\s*"([^"]+)"') +# Parsers for reconstructing the resolved set from generated score_modules_*.MODULE.bazel. +_GIT_OVERRIDE_BLOCK_RE = re.compile(r"git_override\((?P
.*?)\)", re.S) +_SINGLE_VERSION_BLOCK_RE = re.compile(r"single_version_override\((?P.*?)\)", re.S) +_FIELD_RE = lambda field: re.compile(rf'{field}\s*=\s*"([^"]+)"') # noqa: E731 + + +class ResolvedDependencies: + """Resolved dependency versions from the reference_integration root. + + Holds a ``name -> Module`` map of the dependencies ref_int pins, and provides an + interface to scan + overwrite a module's ``MODULE.bazel`` to those versions. + """ + + def __init__(self, resolved: dict[str, Module]): + self._resolved = resolved + + # -- construction: "resolved deps versions from ref_int root" -------------------- + + @classmethod + def from_known_good(cls, known_good_path: Path) -> ResolvedDependencies: + """Build from ``known_good.json`` (local / dev source of the resolved pins).""" + kg = load_known_good(Path(known_good_path).resolve()) + resolved: dict[str, Module] = {} + for group in kg.modules.values(): + for module in group.values(): + resolved[module.name] = module + return cls(resolved) + + @classmethod + def from_resolved_artifact(cls, artifact_dir: Path) -> ResolvedDependencies: + """Build from the Stage-1 ``stage1-resolved-deps`` artifact. + + The handoff is the single ``resolved_versions.json`` manifest (see + :meth:`from_mod_graph` / :meth:`to_file`). For backward compatibility, if the + manifest is absent the older format is parsed: the generated + ``score_modules_*.MODULE.bazel`` override files, gated on the presence of + ``MODULE.bazel.lock`` as evidence of full resolution. + """ + artifact_dir = Path(artifact_dir) + + manifest = artifact_dir / MANIFEST_NAME + if manifest.is_file(): + return cls.from_file(manifest) + + # Legacy fallback: reconstruct from the generated override files. + lock = artifact_dir / "MODULE.bazel.lock" + if not lock.is_file(): + raise FileNotFoundError( + f"Neither {MANIFEST_NAME} nor MODULE.bazel.lock found in resolved-deps artifact " + f"{artifact_dir}; Stage 2 must consume the Stage-1 resolved dependency set." + ) + + module_files = sorted(artifact_dir.glob("score_modules_*.MODULE.bazel")) + if not module_files: + raise FileNotFoundError(f"No score_modules_*.MODULE.bazel files in resolved-deps artifact {artifact_dir}.") + + resolved: dict[str, Module] = {} + for mf in module_files: + for module in cls._parse_override_file(mf.read_text()): + resolved[module.name] = module + return cls(resolved) + + @classmethod + def from_mod_graph(cls, mod_graph_json: Path, override_files: list[Path]) -> ResolvedDependencies: + """Build the *complete* resolved set by merging two sources. + + * The override directives ref_int actually declares — parsed from its root + ``MODULE.bazel`` and the ``bazel_common/*.MODULE.bazel`` files it ``include()``s. + This carries every module ref_int pins by a non-registry source as its real + directive: ``git_override(commit, remote)`` for ``score_*`` plus third-party like + ``trlc`` / ``flatbuffers`` / ``rules_oci``, and ``single_version_override`` where + ref_int pins a registry version. The graph cannot supply these — it reports + overridden modules as version ``0.0.0``. + * ``bazel mod graph --output=json`` — the post-MVS resolved version of every other + (registry) module (protobuf, abseil, rules_rust, ...), emitted as + ``single_version_override`` so each module under test is forced to the exact + version ref_int resolved (MVS is graph-global, so a module's own subgraph could + otherwise select a different version). + + ``archive_override`` / ``local_path_override`` targets (e.g. ``rules_boost``) cannot + be represented and are logged as not carried. + """ + resolved: dict[str, Module] = {} + unrepresentable: list[str] = [] + for f in override_files: + # Drop comment-only lines first: hand-written MODULE.bazel files contain + # commented-out overrides (e.g. "# git_override(... rules_rpm ...)") that must + # not be captured. Inline trailing comments (after a value) are left intact. + text = "\n".join(ln for ln in Path(f).read_text().splitlines() if not ln.lstrip().startswith("#")) + for module in cls._parse_override_file(text): # git_override + single_version_override + resolved[module.name] = module + for m in re.finditer(r'(archive_override|local_path_override)\(\s*module_name\s*=\s*"([^"]+)"', text): + unrepresentable.append(f"{m.group(2)} ({m.group(1)})") + + graph = json.loads(Path(mod_graph_json).read_text()) + versions: dict[str, str] = {} + _collect_resolved_versions(graph, versions) + skipped: list[str] = [] + for name, version in versions.items(): + if name in resolved or name in _SKIP_MODULES: + continue # already carried by an override directive, or non-overridable + if not version or version == "0.0.0": + # Non-registry version: ref_int pins it via an override we did not capture + # (e.g. archive_override). single_version_override cannot reproduce it. + skipped.append(name) + continue + resolved[name] = Module(name=name, hash="", repo="", version=version) + + if unrepresentable: + logging.warning( + "Overrides not carried into manifest (need manual handling): %s", ", ".join(unrepresentable) + ) + if skipped: + logging.warning( + "Graph modules at version 0.0.0 with no carried override, skipped: %s", ", ".join(sorted(skipped)) + ) + return cls(resolved) + + def to_file(self, path: Path) -> None: + """Serialize the resolved set to the JSON manifest (Stage 1 -> Stage 2 handoff). + + Only the fields needed to regenerate the override directive are stored + (``version`` for single_version_override; ``repo`` + ``hash`` for git_override). + Metadata is intentionally omitted — the manifest carries dependency pins, not the + module-under-test's test configuration (that comes from known_good.json). + """ + modules = {} + for name in sorted(self._resolved): + m = self._resolved[name] + entry: dict[str, object] = {"version": m.version} if m.version else {"repo": m.repo, "hash": m.hash} + if m.bazel_patches: + entry["bazel_patches"] = m.bazel_patches + modules[name] = entry + Path(path).write_text(json.dumps({"modules": modules}, indent=2) + "\n") + + @classmethod + def from_file(cls, path: Path) -> ResolvedDependencies: + """Load a resolved set previously written by :meth:`to_file`.""" + data = json.loads(Path(path).read_text()) + resolved = {name: Module.from_dict(name, md) for name, md in data.get("modules", {}).items()} + return cls(resolved) + + @staticmethod + def _parse_override_file(text: str) -> list[Module]: + """Reconstruct Module objects from generated git/single_version override blocks.""" + modules: list[Module] = [] + + for match in _GIT_OVERRIDE_BLOCK_RE.finditer(text): + body = match.group("body") + name = _field(body, "module_name") + commit = _field(body, "commit") + remote = _field(body, "remote") + if name and commit and remote: + modules.append(Module(name=name, hash=commit, repo=remote)) + + for match in _SINGLE_VERSION_BLOCK_RE.finditer(text): + body = match.group("body") + name = _field(body, "module_name") + version = _field(body, "version") + if name and version: + modules.append(Module(name=name, hash="", repo="", version=version)) + + return modules + + # -- interface: scan + overwrite a module's MODULE.bazel ------------------------- + + @property + def names(self) -> set[str]: + return set(self._resolved) + + @property + def modules(self) -> dict[str, Module]: + return dict(self._resolved) + + def get(self, name: str) -> Module | None: + return self._resolved.get(name) + + def scan(self, module_bazel: Path) -> list[str]: + """Return the names of dependencies a module declares via ``bazel_dep``.""" + text = Path(module_bazel).read_text() + # Ignore anything inside a previous injection block so re-scans are stable. + text = self._strip_injection(text) + return _BAZEL_DEP_RE.findall(text) + + def overwrite(self, module_bazel: Path, *, module_under_test: str | None = None, write: bool = True) -> str: + """Overwrite a module's declared dependency versions with the resolved set. + + Appends a ``git_override`` / ``single_version_override`` directive for every + dependency the module declares that we have a resolved version for, so the + module (and all its transitive deps) build against ref_int's resolved versions. + + * Skips the module under test itself (the root is never overridden). + * Always overwrites: any existing override the module already declares is replaced. + * A declared dependency with no entry in the resolved set is expected not to occur + when the resolved set comes from ref_int's full ``bazel mod graph`` (it is a + superset of every module's own graph) — if it does happen, a warning is logged + and that dependency is left to resolve on its own rather than failing the run. + * Re-running is idempotent: a prior injection block is replaced. + """ + module_bazel = Path(module_bazel) + original = self._strip_injection(module_bazel.read_text()) + + declared = set(_BAZEL_DEP_RE.findall(original)) + + from dataclasses import replace as _replace + + directives: list[str] = [] + # Inject overrides only for deps the module actually declares (intersected with the + # resolved set). Bazel fails with "root module specifies overrides on nonexistent + # module(s)" if an override targets a module that is not in this module's dependency + # graph, so the full resolved set cannot be injected wholesale — a declared bazel_dep + # is by definition in the graph, which makes its override safe. + # ref_int always decides the version — any existing module-level override is replaced. + for name in sorted(declared): + if name == module_under_test: + continue # the module under test is the root; never override it + module = self._resolved.get(name) + if module is None: + logging.warning( + "%s declares %s, which has no entry in the resolved set; " + "leaving it to resolve on its own instead of failing the run.", + module_bazel, + name, + ) + continue + # Strip bazel_patches: they reference //patches/... labels in ref_int's + # workspace which do not exist inside another module's checkout. + module = _replace(module, bazel_patches=None) + directive = generate_override_directive(module) + if directive is None: + continue + directives.append(directive) + + if not directives: + patched = original + else: + body = "\n".join(directives) + patched = f"{original.rstrip()}\n\n{INJECTION_BEGIN}\n{body}\n{INJECTION_END}\n" + + if write: + module_bazel.write_text(patched) + return patched + + @staticmethod + def _strip_injection(text: str) -> str: + """Remove a previously appended injection block, if present.""" + pattern = re.compile( + re.escape(INJECTION_BEGIN) + r".*?" + re.escape(INJECTION_END) + r"\n?", + re.S, + ) + return pattern.sub("", text).rstrip() + "\n" if pattern.search(text) else text + + +def _field(body: str, field: str) -> str: + match = _FIELD_RE(field).search(body) + return match.group(1) if match else "" + + +def _collect_resolved_versions(node: dict, acc: dict[str, str]) -> None: + """Walk a ``bazel mod graph --output=json`` tree, recording name -> resolved version. + + Each node carries the post-MVS ``name`` and ``version``; a module can appear many + times in the graph but always at the single resolved version, so deduping by name is + safe. The ``