Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Install buildifier via Bazel #2777

Merged
merged 3 commits into from
Sep 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 0 additions & 9 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,6 @@ jobs:
chmod +x llvm.sh
sudo ./llvm.sh 18
sudo apt-get install -y --no-install-recommends clang-format-18
# buildifier won't install properly if specifying a particular version
go install github.com/bazelbuild/buildtools/buildifier@latest
echo "BUILDIFIER=$HOME/go/bin/buildifier" >> $GITHUB_ENV
- name: Install pnpm
uses: pnpm/action-setup@v4
# The pnpm version will be determined by the `packageManager` field in `.npmrc`
- name: Install project deps with pnpm
run: |
pnpm i
- name: Install Ruff
run: |
pip install ruff
Expand Down
36 changes: 36 additions & 0 deletions WORKSPACE
Original file line number Diff line number Diff line change
Expand Up @@ -597,3 +597,39 @@ new_local_repository(
visibility = ["//visibility:public"],)""",
path = "empty",
)

# Dev tools
http_file(
npaun marked this conversation as resolved.
Show resolved Hide resolved
name = "buildifier-darwin-arm64",
executable = True,
integrity = "sha256-Wmr8asegn1RVuguJvZnVriO0F03F3J1sDtXOjKrD+BM=",
url = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-darwin-arm64",
)

http_file(
name = "buildifier-darwin-amd64",
executable = True,
integrity = "sha256-Wmr8asegn1RVuguJvZnVriO0F03F3J1sDtXOjKrD+BM=",
url = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-darwin-arm64",
)

http_file(
name = "buildifier-linux-arm64",
executable = True,
integrity = "sha256-C/hsS//69PCO7Xe95bIILkrlA5oR4uiwOYTBc8NKVhw=",
url = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-linux-arm64",
)

http_file(
name = "buildifier-linux-amd64",
executable = True,
integrity = "sha256-VHTMUSinToBng9VAgfWBZixL6K5lAi9VfpKB7V3IgAk=",
url = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-linux-amd64",
)

http_file(
name = "buildifier-windows-amd64",
executable = True,
integrity = "sha256-NwzVdgda0pkwqC9d4TLxod5AhMeEqCUUvU2oDIWs9Kg=",
url = "https://github.com/bazelbuild/buildtools/releases/download/v7.3.1/buildifier-windows-amd64.exe",
)
63 changes: 48 additions & 15 deletions tools/cross/format.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@

import logging
import os
import platform
import re
import shutil
import subprocess
import sys
from argparse import ArgumentParser, Namespace
from concurrent.futures import ThreadPoolExecutor, as_completed
from dataclasses import dataclass
Expand All @@ -13,9 +15,10 @@
from typing import Callable, Optional

CLANG_FORMAT = os.environ.get("CLANG_FORMAT", "clang-format")
PRETTIER = os.environ.get("PRETTIER", "node_modules/.bin/prettier")
PRETTIER = os.environ.get(
"PRETTIER", "bazel-bin/node_modules/prettier/bin/prettier.cjs"
)
RUFF = os.environ.get("RUFF", "ruff")
BUILDIFIER = os.environ.get("BUILDIFIER", "buildifier")


def parse_args() -> Namespace:
Expand Down Expand Up @@ -100,6 +103,33 @@ def matches_any_glob(globs: tuple[str, ...], file: Path) -> bool:
return any(file.match(glob) for glob in globs)


def exec_target() -> str:
ALIASES = {"aarch64": "arm64", "x86_64": "amd64", "AMD64": "amd64"}

machine = platform.machine()
return f"{sys.platform}-{ALIASES.get(machine, machine)}"


def run_bazel_tool(tool_name: str, args: list[str]) -> subprocess.CompletedProcess:
external_dir = Path("external")
if not external_dir.exists():
# Create a symlink to the bazel external directory
bazel_base = Path(
subprocess.run(["bazel", "info", "output_base"], capture_output=True)
.stdout.decode()
.strip()
)
external_dir.symlink_to(bazel_base / "external")

tool_target = f"{tool_name}-{exec_target()}"
tool_path = Path("external") / tool_target / "file" / "downloaded"

if not tool_path.exists():
subprocess.run(["bazel", "fetch", f"@{tool_target}//file"])

return subprocess.run([tool_path, *args])


def clang_format(files: list[Path], check: bool = False) -> bool:
cmd = [CLANG_FORMAT]
if check:
Expand All @@ -111,14 +141,17 @@ def clang_format(files: list[Path], check: bool = False) -> bool:


def prettier(files: list[Path], check: bool = False) -> bool:
if not Path(PRETTIER).exists():
subprocess.run(["bazel", "build", "//:node_modules/prettier"])

cmd = [PRETTIER, "--log-level=warn", "--check" if check else "--write"]
result = subprocess.run(cmd + files)
return result.returncode == 0


def buildifier(files: list[Path], check: bool = False) -> bool:
cmd = [BUILDIFIER, "--mode=check" if check else "--mode=fix"]
npaun marked this conversation as resolved.
Show resolved Hide resolved
result = subprocess.run(cmd + files)
cmd = ["--mode=check" if check else "--mode=fix"]
result = run_bazel_tool("buildifier", cmd + files)
return result.returncode == 0


Expand Down Expand Up @@ -189,18 +222,18 @@ class FormatConfig:
# FormatConfig(
# directory="src/workerd", globs=("*.c++", "*.h"), formatter=clang_format
# ),
# FormatConfig(
# directory="src",
# globs=("*.js", "*.ts", "*.cjs", "*.ejs", "*.mjs"),
# formatter=prettier,
# ),
# FormatConfig(directory="src", globs=("*.json",), formatter=prettier),
FormatConfig(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring prettier because it is already installed by bazel

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was prettier intended to be included? Imo it requires running npm install or something and is not fetched automatically?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's listed as a runtime dependency of workerd, so it's somewhere... I'm currently doing a clean build to find out where it puts it.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found it (bazel-bin/node_modules/prettier/bin/prettier.cjs)

directory="src",
globs=("*.js", "*.ts", "*.cjs", "*.ejs", "*.mjs"),
formatter=prettier,
),
FormatConfig(directory="src", globs=("*.json",), formatter=prettier),
# FormatConfig(directory=".", globs=("*.py",), formatter=ruff),
# FormatConfig(
# directory=".",
# globs=("*.bzl", "WORKSPACE", "BUILD", "BUILD.*"),
# formatter=buildifier,
# ),
FormatConfig(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Restoring buildifier because this PR installs it via Bazel

directory=".",
globs=("*.bzl", "WORKSPACE", "BUILD", "BUILD.*"),
formatter=buildifier,
),
]


Expand Down