From ce732d2b8b29f689fd43bef545fbc513306b18b1 Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 13:25:09 +0100 Subject: [PATCH 1/2] ci: preserve full Windows formatting with package-sized invocations --- scripts/ci-format-workspace.py | 46 ++++++++++++++++ scripts/ci-full-validation.sh | 7 ++- scripts/test-full-validation.py | 95 ++++++++++++++++++++++++++++++--- 3 files changed, 141 insertions(+), 7 deletions(-) create mode 100644 scripts/ci-format-workspace.py diff --git a/scripts/ci-format-workspace.py b/scripts/ci-format-workspace.py new file mode 100644 index 000000000..a9b1569a2 --- /dev/null +++ b/scripts/ci-format-workspace.py @@ -0,0 +1,46 @@ +#!/usr/bin/env python3 +"""Check every workspace package separately to bound rustfmt's Windows argv.""" +import json +from pathlib import Path +import re +import subprocess + +ROOT = Path(__file__).resolve().parents[1] + + +def workspace_names(metadata): + members = metadata["workspace_members"] + packages = metadata["packages"] + if not members or len(members) != len(set(members)): + raise ValueError("workspace member inventory is empty or duplicated") + by_id = {package["id"]: package for package in packages} + if len(by_id) != len(packages): + raise ValueError("metadata contains duplicate package IDs") + names = [] + for member in members: + name = by_id[member]["name"] + # Package names are data, never command text or option fragments. + if not isinstance(name, str) or not re.fullmatch(r"[A-Za-z0-9_][A-Za-z0-9_-]*", name): + raise ValueError("invalid workspace package name") + names.append(name) + if len(names) != len(set(names)): + raise ValueError("workspace package names are ambiguous") + return sorted(names) + + +def check_workspace(): + result = subprocess.run( + ["cargo", "metadata", "--no-deps", "--offline", "--locked", "--format-version", "1"], + cwd=ROOT, check=True, stdout=subprocess.PIPE, text=True, + ) + names = workspace_names(json.loads(result.stdout)) + # From this virtual workspace root, cargo fmt normally checks all workspace packages. + # Keep Cargo responsible for target kinds, editions and rustfmt configuration. + # One package per call retains all its targets without the workspace-sized argv. + print(f"Checking formatting for {len(names)} workspace packages", flush=True) + for name in names: + subprocess.run(["cargo", "fmt", "--check", "--package", name], cwd=ROOT, check=True) + + +if __name__ == "__main__": + check_workspace() diff --git a/scripts/ci-full-validation.sh b/scripts/ci-full-validation.sh index 6e9509b5e..3b413ec88 100644 --- a/scripts/ci-full-validation.sh +++ b/scripts/ci-full-validation.sh @@ -5,7 +5,12 @@ unset CRAFT_HOSTILE_DIVISOR PROPTEST_CASES if [ "${RUNNER_OS:-}" = Windows ]; then powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"' fi -cargo fmt --check +# Windows cannot spawn one rustfmt command with every workspace target (#2200). +if [ "${RUNNER_OS:-}" = Windows ]; then + python scripts/ci-format-workspace.py +else + cargo fmt --check +fi cargo clippy --workspace --all-targets --locked -- -D warnings cargo clippy --workspace --all-targets --all-features --locked -- -D warnings # cargo test includes docs and ordinary example linking, as required by AGENTS.md. diff --git a/scripts/test-full-validation.py b/scripts/test-full-validation.py index 658f49f54..4181ef7cc 100644 --- a/scripts/test-full-validation.py +++ b/scripts/test-full-validation.py @@ -29,7 +29,11 @@ unset CRAFT_HOSTILE_DIVISOR PROPTEST_CASES if [ "${RUNNER_OS:-}" = Windows ]; then powershell.exe -NoProfile -Command '$ErrorActionPreference = "Stop"; Start-Service Spooler; Add-PrinterDriver -Name "Generic / Text Only"' fi -cargo fmt --check +if [ "${RUNNER_OS:-}" = Windows ]; then + python scripts/ci-format-workspace.py +else + cargo fmt --check +fi cargo clippy --workspace --all-targets --locked -- -D warnings cargo clippy --workspace --all-targets --all-features --locked -- -D warnings cargo test --workspace --locked @@ -54,6 +58,7 @@ cargo() { if [ "$*" = "$FAIL" ]; then return 17; fi } powershell.exe() { printf 'print-queue\n'; } +python() { test "$*" = "scripts/ci-format-workspace.py" || return 31; cargo fmt --check; } """ return subprocess.run([BASH, "-c", mock + SOURCE], env=env, capture_output=True, text=True, check=False) @@ -89,11 +94,13 @@ class FullValidation(unittest.TestCase): (["print-queue"] if name == "Windows" else []) + COMMANDS) def test_every_failure_stops_the_gate(self): - for i, command in enumerate(COMMANDS): - with self.subTest(command=command): - p = run("Linux", command) - self.assertEqual(p.returncode, 17, p.stdout + p.stderr) - self.assertEqual(p.stdout.splitlines(), COMMANDS[:i + 1]) + for name in ["Linux", "macOS", "Windows"]: + for i, command in enumerate(COMMANDS): + with self.subTest(os=name, command=command): + p = run(name, command) + self.assertEqual(p.returncode, 17, p.stdout + p.stderr) + self.assertEqual(p.stdout.splitlines(), + (["print-queue"] if name == "Windows" else []) + COMMANDS[:i + 1]) def test_workflow_preserves_fork_and_platform_boundaries(self): text = (ROOT / ".github/workflows/full-validation.yml").read_text() @@ -120,5 +127,81 @@ class FullValidation(unittest.TestCase): self.assertIn("macos-latest", pdf) + +class PackageFormatting(unittest.TestCase): + @staticmethod + def helper(): + import importlib.util + spec = importlib.util.spec_from_file_location("ci_format_workspace", ROOT / "scripts/ci-format-workspace.py") + module = importlib.util.module_from_spec(spec) + spec.loader.exec_module(module) + return module + + @staticmethod + def metadata(): + return {"workspace_members": ["id-b", "id-a"], "workspace_default_members": ["id-a"], + "packages": [{"id": "id-a", "name": "craft-a"}, + {"id": "id-b", "name": "craft-b"}, + {"id": "external", "name": "not-a-member"}]} + + def test_all_workspace_members_not_only_defaults_and_no_external_dependencies(self): + helper = self.helper() + self.assertEqual(helper.workspace_names(self.metadata()), ["craft-a", "craft-b"]) + + def test_new_package_automatically_included(self): + helper = self.helper() + data = self.metadata() + data["workspace_members"].append("id-new") + data["packages"].append({"id": "id-new", "name": "craft-new"}) + self.assertEqual(helper.workspace_names(data), ["craft-a", "craft-b", "craft-new"]) + + def test_bad_inventory_fails_closed(self): + helper = self.helper() + for members in ([], ["id-a", "id-a"], ["missing"]): + data = self.metadata() + data["workspace_members"] = members + with self.subTest(members=members), self.assertRaises((ValueError, KeyError)): + helper.workspace_names(data) + for name in ("--all", "craft-a;echo injected", "craft-a", ""): + data = self.metadata() + data["packages"][1]["name"] = name + with self.subTest(name=name), self.assertRaises(ValueError): + helper.workspace_names(data) + + def test_calls_cargo_with_one_package_and_no_shell(self): + import json + helper = self.helper() + result = subprocess.CompletedProcess([], 0, stdout=json.dumps(self.metadata())) + with patch.object(helper.subprocess, "run", return_value=result) as execute: + helper.check_workspace() + self.assertEqual([call.args[0] for call in execute.call_args_list], [ + ["cargo", "metadata", "--no-deps", "--offline", "--locked", "--format-version", "1"], + ["cargo", "fmt", "--check", "--package", "craft-a"], + ["cargo", "fmt", "--check", "--package", "craft-b"], + ]) + for call in execute.call_args_list: + self.assertEqual(call.kwargs["cwd"], ROOT) + self.assertTrue(call.kwargs["check"]) + self.assertNotIn("shell", call.kwargs) + + def test_metadata_failure_prevents_formatting(self): + helper = self.helper() + failure = subprocess.CalledProcessError(17, ["cargo", "metadata"]) + with patch.object(helper.subprocess, "run", side_effect=failure) as execute: + with self.assertRaises(subprocess.CalledProcessError): + helper.check_workspace() + self.assertEqual(execute.call_count, 1) + + def test_first_format_failure_stops_later_packages(self): + import json + helper = self.helper() + result = subprocess.CompletedProcess([], 0, stdout=json.dumps(self.metadata())) + failure = subprocess.CalledProcessError(17, ["cargo", "fmt"]) + with patch.object(helper.subprocess, "run", side_effect=[result, failure]) as execute: + with self.assertRaises(subprocess.CalledProcessError): + helper.check_workspace() + self.assertEqual(execute.call_count, 2) + + if __name__ == "__main__": unittest.main() From 83131112ab21848f577f87ccabfb31360913edb4 Mon Sep 17 00:00:00 2001 From: FredApps Date: Wed, 18 Nov 2026 13:26:52 +0100 Subject: [PATCH 2/2] Decode Cargo metadata as UTF-8 on Windows --- scripts/ci-format-workspace.py | 2 +- scripts/test-full-validation.py | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/scripts/ci-format-workspace.py b/scripts/ci-format-workspace.py index a9b1569a2..32605af70 100644 --- a/scripts/ci-format-workspace.py +++ b/scripts/ci-format-workspace.py @@ -31,7 +31,7 @@ def workspace_names(metadata): def check_workspace(): result = subprocess.run( ["cargo", "metadata", "--no-deps", "--offline", "--locked", "--format-version", "1"], - cwd=ROOT, check=True, stdout=subprocess.PIPE, text=True, + cwd=ROOT, check=True, stdout=subprocess.PIPE, text=True, encoding="utf-8", ) names = workspace_names(json.loads(result.stdout)) # From this virtual workspace root, cargo fmt normally checks all workspace packages. diff --git a/scripts/test-full-validation.py b/scripts/test-full-validation.py index 4181ef7cc..b37e979c8 100644 --- a/scripts/test-full-validation.py +++ b/scripts/test-full-validation.py @@ -179,6 +179,7 @@ class PackageFormatting(unittest.TestCase): ["cargo", "fmt", "--check", "--package", "craft-a"], ["cargo", "fmt", "--check", "--package", "craft-b"], ]) + self.assertEqual(execute.call_args_list[0].kwargs["encoding"], "utf-8") for call in execute.call_args_list: self.assertEqual(call.kwargs["cwd"], ROOT) self.assertTrue(call.kwargs["check"])