Skip to content

Commit

Permalink
fix: fix and enable rustfmt linter (#35)
Browse files Browse the repository at this point in the history
* fix: fix and enable rustfmt linter

* Add lint workflow

* SARIF

* Fix windows run

* Remove retry

* Remove timeout

* try this

* concurrency

* config
  • Loading branch information
justinchuby authored Feb 5, 2023
1 parent b0e8be2 commit 507d273
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 16 deletions.
4 changes: 4 additions & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ on:
tags:
- v*

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
cancel-in-progress: true

jobs:
test:
strategy:
Expand Down
59 changes: 59 additions & 0 deletions .github/workflows/Lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
name: Lint

on:
push:
branches:
- main
tags:
- v*
pull_request:

concurrency:
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
cancel-in-progress: true

jobs:
lintrunner:
name: lintrunner
strategy:
fail-fast: false
matrix:
os: [ubuntu-latest, windows-latest]
python_version: ["3.11"]

runs-on: ${{ matrix.os }}
steps:
- uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python_version }}
- name: Install Lintrunner
run: |
pip install .
lintrunner init
- name: Run lintrunner on all files - Linux
if: matrix.os != 'windows-latest'
run: |
set +e
if ! lintrunner -v --force-color --all-files --tee-json=lint.json; then
echo ""
echo -e "\e[1m\e[36mYou can reproduce these results locally by using \`lintrunner -m main\`.\e[0m"
exit 1
fi
- name: Run lintrunner on all files - Windows
if: matrix.os == 'windows-latest'
run: lintrunner -v --force-color --all-files
- name: Produce SARIF
if: always() && matrix.os == 'ubuntu-latest'
run: |
python tools/convert_to_sarif.py --input lint.json --output lintrunner.sarif
- name: Upload SARIF file
if: always() && matrix.os == 'ubuntu-latest'
continue-on-error: true
uses: github/codeql-action/upload-sarif@v2
with:
# Path to SARIF file relative to the root of the repository
sarif_file: lintrunner.sarif
category: lintrunner
checkout_path: ${{ github.workspace }}
11 changes: 11 additions & 0 deletions .lintrunner.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[linter]]
code = 'RUSTFMT'
include_patterns = ['**/*.rs']
command = [
'python',
'examples/rustfmt_linter.py',
'--binary=rustfmt',
'--config-path=rustfmt.toml',
'--',
'@{{PATHSFILE}}'
]
40 changes: 24 additions & 16 deletions examples/rustfmt_linter.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from __future__ import annotations

import argparse
import concurrent.futures
import json
Expand All @@ -8,7 +10,7 @@
import sys
import time
from enum import Enum
from typing import Any, List, NamedTuple, Optional, Pattern
from typing import Any, BinaryIO, List, NamedTuple, Optional, Pattern


IS_WINDOWS: bool = os.name == "nt"
Expand Down Expand Up @@ -56,16 +58,20 @@ def strip_path_from_error(error: str) -> str:


def run_command(
args: List[str],
) -> "subprocess.CompletedProcess[bytes]":
args: list[str],
*,
stdin: BinaryIO | None = None,
check: bool = False,
) -> subprocess.CompletedProcess[bytes]:
logging.debug("$ %s", " ".join(args))
start_time = time.monotonic()
try:
return subprocess.run(
args,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
check=True,
capture_output=True,
shell=False,
stdin=stdin,
check=check,
)
finally:
end_time = time.monotonic()
Expand All @@ -80,16 +86,18 @@ def check_file(
try:
with open(filename, "rb") as f:
original = f.read()
proc = run_command(
[
binary,
"--config-path",
config_path,
"--emit=stdout",
"--quiet",
filename,
]
)
with open(filename, "rb") as f:
proc = run_command(
[
binary,
"--config-path",
config_path,
"--emit=stdout",
"--quiet",
],
stdin=f,
check=True,
)
except (OSError, subprocess.CalledProcessError) as err:
# https://github.com/rust-lang/rustfmt#running
# TODO: Fix the syntax error regexp to handle multiple issues and
Expand Down
1 change: 1 addition & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# Please keep these in alphabetical order.
edition = "2018"
merge_derives = false
newline_style = "Native"
use_field_init_shorthand = true

0 comments on commit 507d273

Please sign in to comment.