From 7caac9790d772776a036ef239d87e426a2312b00 Mon Sep 17 00:00:00 2001 From: Samson <16504129+sagudev@users.noreply.github.com> Date: Tue, 19 Sep 2023 10:57:27 +0200 Subject: [PATCH] Enforce formatting of TOML files (#30128) * Fmt all toml files * bootstrap taplo * enforce toml formatting with taplo * Install taplo in CI using cargo-install action --- .cargo/config.toml | 2 +- .github/workflows/linux.yml | 5 +++++ .github/workflows/mac.yml | 5 +++++ .github/workflows/windows.yml | 5 +++++ python/servo/platform/base.py | 15 ++++++++++++++- python/servo/testing_commands.py | 23 +++++++++++++++++++++-- 6 files changed, 51 insertions(+), 4 deletions(-) diff --git a/.cargo/config.toml b/.cargo/config.toml index c78db7978d5..10ce481511d 100644 --- a/.cargo/config.toml +++ b/.cargo/config.toml @@ -41,4 +41,4 @@ linker = "lld-link.exe" MACOSX_DEPLOYMENT_TARGET = "10.10" [build] -rustdocflags = ["--document-private-items"] \ No newline at end of file +rustdocflags = ["--document-private-items"] diff --git a/.github/workflows/linux.yml b/.github/workflows/linux.yml index 3ef755554d6..f891d297c0a 100644 --- a/.github/workflows/linux.yml +++ b/.github/workflows/linux.yml @@ -71,6 +71,11 @@ jobs: uses: actions/setup-python@v4 with: python-version: '3.9' + - name: Install taplo + uses: baptiste0928/cargo-install@v2 + with: + crate: taplo-cli + locked: true - name: Bootstrap Python run: python3 -m pip install --upgrade pip virtualenv - name: Bootstrap dependencies diff --git a/.github/workflows/mac.yml b/.github/workflows/mac.yml index db7b9518886..b974f97ff40 100644 --- a/.github/workflows/mac.yml +++ b/.github/workflows/mac.yml @@ -60,6 +60,11 @@ jobs: fetch-depth: 2 - name: Run sccache-cache uses: mozilla-actions/sccache-action@v0.0.3 + - name: Install taplo + uses: baptiste0928/cargo-install@v2 + with: + crate: taplo-cli + locked: true - name: Bootstrap run: | python3 -m pip install --upgrade pip virtualenv diff --git a/.github/workflows/windows.yml b/.github/workflows/windows.yml index f93930673fc..e416cde2707 100644 --- a/.github/workflows/windows.yml +++ b/.github/workflows/windows.yml @@ -55,6 +55,11 @@ jobs: fetch-depth: 2 - name: ccache uses: hendrikmuhs/ccache-action@v1.2 + - name: Install taplo + uses: baptiste0928/cargo-install@v2 + with: + crate: taplo-cli + locked: true - name: Install wixtoolset run: | choco install wixtoolset diff --git a/python/servo/platform/base.py b/python/servo/platform/base.py index 7fde2b60c97..8af3e2056eb 100644 --- a/python/servo/platform/base.py +++ b/python/servo/platform/base.py @@ -8,6 +8,7 @@ # except according to those terms. import os +import shutil import subprocess from typing import Dict, Optional @@ -96,9 +97,21 @@ class Base: ) def bootstrap(self, force: bool): - if not self._platform_bootstrap(force): + installed_something = self._platform_bootstrap(force) + installed_something |= self.install_taplo(force) + if not installed_something: print("Dependencies were already installed!") + def install_taplo(self, force: bool) -> bool: + if not force and shutil.which("taplo") is not None: + return False + + if subprocess.call(["cargo", "install", "taplo-cli", "--locked"], + stdout=subprocess.PIPE, stderr=subprocess.PIPE) != 0: + raise EnvironmentError("Installation of taplo failed.") + + return True + def passive_bootstrap(self) -> bool: """A bootstrap method that is called without explicitly invoking `./mach bootstrap` but that is executed in the process of other `./mach` commands. This should be diff --git a/python/servo/testing_commands.py b/python/servo/testing_commands.py index 328a82a7a23..a0e60cb2e72 100644 --- a/python/servo/testing_commands.py +++ b/python/servo/testing_commands.py @@ -57,6 +57,20 @@ TEST_SUITES = OrderedDict([ TEST_SUITES_BY_PREFIX = {path: k for k, v in TEST_SUITES.items() if "paths" in v for path in v["paths"]} +def format_toml_files_with_taplo(check_only: bool = True) -> int: + taplo = shutil.which("taplo") + if taplo is None: + print("Taplo is not installed.") + print("It should be installed using `./mach bootstrap`, \ + but it can be installed manually using `cargo install taplo-cli --locked`") + return 1 + + if check_only: + return call([taplo, "fmt", "--check"], env={'RUST_LOG': 'error'}) + else: + return call([taplo, "fmt"], env={'RUST_LOG': 'error'}) + + @CommandProvider class MachCommands(CommandBase): DEFAULT_RENDER_MODE = "cpu" @@ -279,7 +293,9 @@ class MachCommands(CommandBase): if rustfmt_failed: print("Run `./mach fmt` to fix the formatting") - return tidy_failed or manifest_dirty or rustfmt_failed + taplo_failed = format_toml_files_with_taplo() + + return tidy_failed or manifest_dirty or rustfmt_failed or taplo_failed @Command('test-scripts', description='Run tests for all build and support scripts.', @@ -370,9 +386,12 @@ class MachCommands(CommandBase): return wpt.manifestupdate.update(check_clean=False) @Command('fmt', - description='Format the Rust and CPP source files with rustfmt', + description='Format Rust and TOML files', category='testing') def format_code(self): + result = format_toml_files_with_taplo(check_only=False) + if result != 0: + return result return call(["cargo", "fmt"]) @Command('update-wpt',