Skip to content

Commit

Permalink
feat(linter): retry with the --format option when --output-format fai…
Browse files Browse the repository at this point in the history
…ls (#79)

This way we support both ruff<0.0.291 and newer versions of ruff.
  • Loading branch information
justinchuby authored Oct 17, 2023
1 parent 0609956 commit 4456097
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
4 changes: 2 additions & 2 deletions lintrunner_adapters/_common/lintrunner_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ def run_command(
time.sleep(1)


def add_default_options(parser: argparse.ArgumentParser) -> None:
def add_default_options(parser: argparse.ArgumentParser, retries: int = 3) -> None:
"""Add default options to a parser.
This should be called the last in the chain of add_argument calls.
"""
parser.add_argument(
"--retries",
type=int,
default=3,
default=retries,
help="number of times to retry if the linter times out.",
)
parser.add_argument(
Expand Down
64 changes: 42 additions & 22 deletions lintrunner_adapters/adapters/ruff_linter.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,28 +113,48 @@ def check_files(
check=True,
)
except (OSError, subprocess.CalledProcessError) as err:
return [
LintMessage(
path=None,
line=None,
char=None,
code=LINTER_CODE,
severity=LintSeverity.ERROR,
name="command-failed",
original=None,
replacement=None,
description=(
f"Failed due to {err.__class__.__name__}:\n{err}"
if not isinstance(err, subprocess.CalledProcessError)
else (
f"COMMAND (exit code {err.returncode})\n"
f"{' '.join(as_posix(x) for x in err.cmd)}\n\n"
f"STDERR\n{err.stderr.decode('utf-8').strip() or '(empty)'}\n\n"
f"STDOUT\n{err.stdout.decode('utf-8').strip() or '(empty)'}"
)
),
try:
# ruff<0.0.291 has the option --format instead of --output-format
# If --output-format fails, try --format
# if it still fails, raise the original error
proc = run_command(
[
sys.executable,
"-m",
"ruff",
"--exit-zero",
"--quiet",
"--format=json",
*([f"--config={config}"] if config else []),
*filenames,
],
retries=retries,
timeout=timeout,
check=True,
)
]
except (OSError, subprocess.CalledProcessError):
return [
LintMessage(
path=None,
line=None,
char=None,
code=LINTER_CODE,
severity=LintSeverity.ERROR,
name="command-failed",
original=None,
replacement=None,
description=(
f"Failed due to {err.__class__.__name__}:\n{err}"
if not isinstance(err, subprocess.CalledProcessError)
else (
f"COMMAND (exit code {err.returncode})\n"
f"{' '.join(as_posix(x) for x in err.cmd)}\n\n"
f"STDERR\n{err.stderr.decode('utf-8').strip() or '(empty)'}\n\n"
f"STDOUT\n{err.stdout.decode('utf-8').strip() or '(empty)'}"
)
),
)
]

stdout = str(proc.stdout, "utf-8").strip()
vulnerabilities = json.loads(stdout)
Expand Down Expand Up @@ -275,7 +295,7 @@ def main() -> None:
action="store_true",
help="Do not suggest fixes",
)
add_default_options(parser)
add_default_options(parser, retries=1)
args = parser.parse_args()

logging.basicConfig(
Expand Down

0 comments on commit 4456097

Please sign in to comment.