diff --git a/.buildifier.lint.json b/.buildifier.lint.json new file mode 100644 index 000000000..2c298e8ea --- /dev/null +++ b/.buildifier.lint.json @@ -0,0 +1,106 @@ +{ + "type": "default", + "mode": "check", + "lint": "warn", + "uncheckedWarningsList": [ + "function-docstring", + "function-docstring-args", + "function-docstring-header", + "function-docstring-return", + "module-docstring", + "skylark-docstring", + "unsorted-dict-items" + ], + "warningsList": [ + "attr-applicable_licenses", + "attr-cfg", + "attr-license", + "attr-licenses", + "attr-non-empty", + "attr-output-default", + "attr-single-file", + "build-args-kwargs", + "bzl-visibility", + "confusing-name", + "constant-glob", + "ctx-actions", + "ctx-args", + "deprecated-function", + "depset-items", + "depset-iteration", + "depset-union", + "dict-concatenation", + "dict-method-named-arg", + "duplicated-name", + "filetype", + "git-repository", + "http-archive", + "integer-division", + "keyword-positional-params", + "list-append", + "load", + "name-conventions", + "native-android", + "native-build", + "native-cc-binary", + "native-cc-common", + "native-cc-debug-package-info", + "native-cc-fdo-prefetch-hints", + "native-cc-fdo-profile", + "native-cc-import", + "native-cc-info", + "native-cc-library", + "native-cc-memprof-profile", + "native-cc-objc-import", + "native-cc-objc-library", + "native-cc-propeller-optimize", + "native-cc-proto", + "native-cc-shared-library", + "native-cc-shared-library-hint-info", + "native-cc-shared-library-info", + "native-cc-test", + "native-cc-toolchain", + "native-cc-toolchain-suite", + "native-java-binary", + "native-java-common", + "native-java-import", + "native-java-info", + "native-java-library", + "native-java-lite-proto", + "native-java-package-config", + "native-java-plugin", + "native-java-plugin-info", + "native-java-proto", + "native-java-runtime", + "native-java-test", + "native-java-toolchain", + "native-package", + "native-proto", + "native-proto-common", + "native-proto-info", + "native-proto-lang-toolchain", + "native-proto-lang-toolchain-info", + "native-py", + "native-sh-binary", + "native-sh-library", + "native-sh-test", + "no-effect", + "output-group", + "overly-nested-depset", + "package-name", + "package-on-top", + "positional-args", + "print", + "provider-params", + "redefined-variable", + "repository-name", + "return-value", + "rule-impl-return", + "skylark-comment", + "string-iteration", + "uninitialized", + "unnamed-macro", + "unreachable", + "unused-variable" + ] +} diff --git a/.github/workflows/per-pr.yml b/.github/workflows/per-pr.yml index c32311943..6671a4b82 100644 --- a/.github/workflows/per-pr.yml +++ b/.github/workflows/per-pr.yml @@ -59,6 +59,10 @@ jobs: uses: ./.github/actions/kurtosis-install - name: Kurtosis Lint run: kurtosis lint ${{ github.workspace }} + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + - name: Buildifier Lint + run: make lint assertoor: runs-on: ubuntu-latest diff --git a/Dockerfile.buildifier b/Dockerfile.buildifier new file mode 100644 index 000000000..cd9c83905 --- /dev/null +++ b/Dockerfile.buildifier @@ -0,0 +1,16 @@ +FROM alpine:latest + +# Install required dependencies +RUN apk add --no-cache curl + +# Install buildifier +RUN ARCH=$(uname -m) \ + && if [ "$ARCH" = "x86_64" ]; then BUILDIFIER_ARCH="amd64"; elif [ "$ARCH" = "aarch64" ]; then BUILDIFIER_ARCH="arm64"; else BUILDIFIER_ARCH="amd64"; fi \ + && curl -fsSL -o /usr/local/bin/buildifier "https://github.com/bazelbuild/buildtools/releases/download/v8.2.1/buildifier-linux-${BUILDIFIER_ARCH}" \ + && chmod +x /usr/local/bin/buildifier + +# Set working directory for linting +WORKDIR /workspace + +# Entry point to run buildifier +ENTRYPOINT ["/usr/local/bin/buildifier"] \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 000000000..540704a71 --- /dev/null +++ b/Makefile @@ -0,0 +1,55 @@ +# Dockerfile-based linter for Starlark files +DOCKERFILE_LINT := Dockerfile.buildifier +LINT_IMAGE := ethereum-package-buildifier + +.PHONY: lint lint-build fmt fmt-check + +# Build the lint Docker image +lint-build: + docker build -f $(DOCKERFILE_LINT) -t $(LINT_IMAGE) . + +# Run buildifier on .star files (all files if no FILES specified, or specific FILES/patterns) +lint: lint-build + @if [ "$(FILES)" ]; then \ + echo "Running buildifier on specified files: $(FILES)"; \ + exit_code=0; \ + for pattern in $(FILES); do \ + for file in $$pattern; do \ + if [ -f "$$file" ]; then \ + printf "\033[1;34mLinting file: \033[1;33m%s\033[0m\n" "$$file"; \ + docker run --rm -u $(shell id -u):$(shell id -g) -v $(PWD):/workspace $(LINT_IMAGE) --config=.buildifier.lint.json "$$file" || exit_code=1; \ + fi; \ + done; \ + done; \ + exit $$exit_code; \ + else \ + echo "Running buildifier on all .star files..."; \ + exit_code=0; \ + for file in $$(find . -name "*.star" -type f); do \ + printf "\033[1;34mLinting file: \033[1;33m%s\033[0m\n" "$$file"; \ + docker run --rm -u $(shell id -u):$(shell id -g) -v $(PWD):/workspace $(LINT_IMAGE) --config=.buildifier.lint.json "$$file" || exit_code=1; \ + done; \ + exit $$exit_code; \ + fi + @echo "Linting complete." + +# Format .star files using buildifier (all files if no FILES specified, or specific FILES/patterns) +fmt: lint-build + @if [ "$(FILES)" ]; then \ + echo "Formatting specified files: $(FILES)"; \ + for pattern in $(FILES); do \ + for file in $$pattern; do \ + if [ -f "$$file" ]; then \ + printf "\033[1;32mFormatting file: \033[1;33m%s\033[0m\n" "$$file"; \ + cat "$$file" | docker run --rm -i -u $(shell id -u):$(shell id -g) -v $(PWD):/workspace $(LINT_IMAGE) --type=bzl > "$$file.tmp" && mv "$$file.tmp" "$$file"; \ + fi; \ + done; \ + done; \ + else \ + echo "Formatting all .star files..."; \ + find . -name "*.star" -type f | while read file; do \ + printf "\033[1;32mFormatting file: \033[1;33m%s\033[0m\n" "$$file"; \ + cat "$$file" | docker run --rm -i -u $(shell id -u):$(shell id -g) -v $(PWD):/workspace $(LINT_IMAGE) --type=bzl > "$$file.tmp" && mv "$$file.tmp" "$$file"; \ + done; \ + fi + @echo "Formatting complete."