Skip to content

Commit 507d273

Browse files
authored
fix: fix and enable rustfmt linter (#35)
* fix: fix and enable rustfmt linter * Add lint workflow * SARIF * Fix windows run * Remove retry * Remove timeout * try this * concurrency * config
1 parent b0e8be2 commit 507d273

File tree

5 files changed

+99
-16
lines changed

5 files changed

+99
-16
lines changed

.github/workflows/CI.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ on:
88
tags:
99
- v*
1010

11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
13+
cancel-in-progress: true
14+
1115
jobs:
1216
test:
1317
strategy:

.github/workflows/Lint.yml

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
name: Lint
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
tags:
8+
- v*
9+
pull_request:
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.event.pull_request.number || github.sha }}-${{ github.event_name == 'workflow_dispatch' }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
lintrunner:
17+
name: lintrunner
18+
strategy:
19+
fail-fast: false
20+
matrix:
21+
os: [ubuntu-latest, windows-latest]
22+
python_version: ["3.11"]
23+
24+
runs-on: ${{ matrix.os }}
25+
steps:
26+
- uses: actions/checkout@v3
27+
- name: Setup Python
28+
uses: actions/setup-python@v4
29+
with:
30+
python-version: ${{ matrix.python_version }}
31+
- name: Install Lintrunner
32+
run: |
33+
pip install .
34+
lintrunner init
35+
- name: Run lintrunner on all files - Linux
36+
if: matrix.os != 'windows-latest'
37+
run: |
38+
set +e
39+
if ! lintrunner -v --force-color --all-files --tee-json=lint.json; then
40+
echo ""
41+
echo -e "\e[1m\e[36mYou can reproduce these results locally by using \`lintrunner -m main\`.\e[0m"
42+
exit 1
43+
fi
44+
- name: Run lintrunner on all files - Windows
45+
if: matrix.os == 'windows-latest'
46+
run: lintrunner -v --force-color --all-files
47+
- name: Produce SARIF
48+
if: always() && matrix.os == 'ubuntu-latest'
49+
run: |
50+
python tools/convert_to_sarif.py --input lint.json --output lintrunner.sarif
51+
- name: Upload SARIF file
52+
if: always() && matrix.os == 'ubuntu-latest'
53+
continue-on-error: true
54+
uses: github/codeql-action/upload-sarif@v2
55+
with:
56+
# Path to SARIF file relative to the root of the repository
57+
sarif_file: lintrunner.sarif
58+
category: lintrunner
59+
checkout_path: ${{ github.workspace }}

.lintrunner.toml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
[[linter]]
2+
code = 'RUSTFMT'
3+
include_patterns = ['**/*.rs']
4+
command = [
5+
'python',
6+
'examples/rustfmt_linter.py',
7+
'--binary=rustfmt',
8+
'--config-path=rustfmt.toml',
9+
'--',
10+
'@{{PATHSFILE}}'
11+
]

examples/rustfmt_linter.py

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
from __future__ import annotations
2+
13
import argparse
24
import concurrent.futures
35
import json
@@ -8,7 +10,7 @@
810
import sys
911
import time
1012
from enum import Enum
11-
from typing import Any, List, NamedTuple, Optional, Pattern
13+
from typing import Any, BinaryIO, List, NamedTuple, Optional, Pattern
1214

1315

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

5759

5860
def run_command(
59-
args: List[str],
60-
) -> "subprocess.CompletedProcess[bytes]":
61+
args: list[str],
62+
*,
63+
stdin: BinaryIO | None = None,
64+
check: bool = False,
65+
) -> subprocess.CompletedProcess[bytes]:
6166
logging.debug("$ %s", " ".join(args))
6267
start_time = time.monotonic()
6368
try:
6469
return subprocess.run(
6570
args,
66-
stdout=subprocess.PIPE,
67-
stderr=subprocess.PIPE,
68-
check=True,
71+
capture_output=True,
72+
shell=False,
73+
stdin=stdin,
74+
check=check,
6975
)
7076
finally:
7177
end_time = time.monotonic()
@@ -80,16 +86,18 @@ def check_file(
8086
try:
8187
with open(filename, "rb") as f:
8288
original = f.read()
83-
proc = run_command(
84-
[
85-
binary,
86-
"--config-path",
87-
config_path,
88-
"--emit=stdout",
89-
"--quiet",
90-
filename,
91-
]
92-
)
89+
with open(filename, "rb") as f:
90+
proc = run_command(
91+
[
92+
binary,
93+
"--config-path",
94+
config_path,
95+
"--emit=stdout",
96+
"--quiet",
97+
],
98+
stdin=f,
99+
check=True,
100+
)
93101
except (OSError, subprocess.CalledProcessError) as err:
94102
# https://github.com/rust-lang/rustfmt#running
95103
# TODO: Fix the syntax error regexp to handle multiple issues and

rustfmt.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
# Please keep these in alphabetical order.
33
edition = "2018"
44
merge_derives = false
5+
newline_style = "Native"
56
use_field_init_shorthand = true

0 commit comments

Comments
 (0)