Skip to content
Merged
Changes from 3 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
52 changes: 42 additions & 10 deletions src/tools/tidy/src/extra_checks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,12 @@ fn check_impl(
};
}

let rerun_with_bless = |mode: &str, action: &str| {
if !bless {
eprintln!("rerun tidy with `--extra-checks={mode} --bless` to {action}");
}
};

let python_lint = extra_check!(Py, Lint);
let python_fmt = extra_check!(Py, Fmt);
let shell_lint = extra_check!(Shell, Lint);
Expand All @@ -147,21 +153,32 @@ fn check_impl(
}

if python_lint {
eprintln!("linting python files");
let py_path = py_path.as_ref().unwrap();
let res = run_ruff(root_path, outdir, py_path, &cfg_args, &file_args, &["check".as_ref()]);
let args: &[&OsStr] = if bless {
eprintln!("linting python files and applying suggestions");
&["check".as_ref(), "--fix".as_ref()]
} else {
eprintln!("linting python files");
&["check".as_ref()]
};

let res = run_ruff(root_path, outdir, py_path, &cfg_args, &file_args, args);

if res.is_err() && show_diff {
if res.is_err() && show_diff && !bless {
Copy link
Member

Choose a reason for hiding this comment

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

I'm not sure if ruff --fix can actually fix all the issues that it can find (probably yes for formatting, but I doubt that also for linting)? If not, then we should probably still show the diff even if we used bless mode (in case it returned something).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ruff definitely can't fix all the issues that it can find, but I believe --diff only shows the diff for machine-applicable suggestions, so there would not be anything to show in bless mode. If you want to show the diff for everything, that would be ruff check --diff --unsafe-fixes.

Copy link
Member

Choose a reason for hiding this comment

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

I see. Ok, makes sense I guess.

eprintln!("\npython linting failed! Printing diff suggestions:");

let _ = run_ruff(
let diff_res = run_ruff(
root_path,
outdir,
py_path,
&cfg_args,
&file_args,
&["check".as_ref(), "--diff".as_ref()],
);
// `ruff check --diff` will return status 0 if there are no suggestions.
if diff_res.is_err() {
rerun_with_bless("py:lint", "apply ruff suggestions");
}
}
// Rethrow error
res?;
Expand Down Expand Up @@ -192,7 +209,7 @@ fn check_impl(
&["format".as_ref(), "--diff".as_ref()],
);
}
eprintln!("rerun tidy with `--extra-checks=py:fmt --bless` to reformat Python code");
rerun_with_bless("py:fmt", "reformat Python code");
}

// Rethrow error
Expand Down Expand Up @@ -225,7 +242,7 @@ fn check_impl(
let args = merge_args(&cfg_args_clang_format, &file_args_clang_format);
let res = py_runner(py_path.as_ref().unwrap(), false, None, "clang-format", &args);

if res.is_err() && show_diff {
if res.is_err() && show_diff && !bless {
eprintln!("\nclang-format linting failed! Printing diff suggestions:");

let mut cfg_args_clang_format_diff = cfg_args.clone();
Expand Down Expand Up @@ -265,6 +282,7 @@ fn check_impl(
);
}
}
rerun_with_bless("cpp:fmt", "reformat C++ code");
}
// Rethrow error
res?;
Expand All @@ -290,24 +308,38 @@ fn check_impl(
args.extend_from_slice(SPELLCHECK_DIRS);

if bless {
eprintln!("spellcheck files and fix");
eprintln!("spellchecking files and fixing typos");
args.push("--write-changes");
} else {
eprintln!("spellcheck files");
eprintln!("spellchecking files");
}
spellcheck_runner(root_path, &outdir, &cargo, &args)?;
let res = spellcheck_runner(root_path, &outdir, &cargo, &args);
if res.is_err() {
rerun_with_bless("spellcheck", "fix typos");
}
res?;
}

if js_lint || js_typecheck {
rustdoc_js::npm_install(root_path, outdir, npm)?;
}

if js_lint {
rustdoc_js::lint(outdir, librustdoc_path, tools_path, bless)?;
if bless {
eprintln!("linting javascript files");
} else {
eprintln!("linting javascript files and applying suggestions");
}
let res = rustdoc_js::lint(outdir, librustdoc_path, tools_path, bless);
if res.is_err() {
rerun_with_bless("js:lint", "apply eslint suggestions");
}
res?;
rustdoc_js::es_check(outdir, librustdoc_path)?;
}

if js_typecheck {
eprintln!("typechecking javascript files");
rustdoc_js::typecheck(outdir, librustdoc_path)?;
}

Expand Down