Skip to content

tidy: only run eslint on JS files if specified with "--extra-checks" #142851

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all 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
7 changes: 1 addition & 6 deletions src/ci/docker/host-x86_64/mingw-check-tidy/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,6 @@ COPY scripts/nodejs.sh /scripts/
RUN sh /scripts/nodejs.sh /node
ENV PATH="/node/bin:${PATH}"

# Install eslint
COPY host-x86_64/mingw-check-tidy/eslint.version /tmp/

COPY scripts/sccache.sh /scripts/
RUN sh /scripts/sccache.sh

Expand All @@ -41,9 +38,7 @@ RUN pip3 install --no-deps --no-cache-dir --require-hashes -r /tmp/reuse-require
COPY host-x86_64/mingw-check-1/validate-toolstate.sh /scripts/
COPY host-x86_64/mingw-check-1/validate-error-codes.sh /scripts/

RUN bash -c 'npm install -g eslint@$(cat /tmp/eslint.version)'

# NOTE: intentionally uses python2 for x.py so we can test it still works.
# validate-toolstate only runs in our CI, so it's ok for it to only support python3.
ENV SCRIPT TIDY_PRINT_DIFF=1 python2.7 ../x.py test --stage 0 \
src/tools/tidy tidyselftest --extra-checks=py,cpp
src/tools/tidy tidyselftest --extra-checks=py,cpp,js
64 changes: 62 additions & 2 deletions src/tools/tidy/src/ext_tool_checks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ use std::path::{Path, PathBuf};
use std::process::Command;
use std::{fmt, fs, io};

use crate::walk::walk_no_read;

const MIN_PY_REV: (u32, u32) = (3, 9);
const MIN_PY_REV_STR: &str = "≥3.9";

Expand Down Expand Up @@ -56,8 +58,8 @@ fn check_impl(
extra_checks: Option<&str>,
pos_args: &[String],
) -> Result<(), Error> {
let show_diff = std::env::var("TIDY_PRINT_DIFF")
.map_or(false, |v| v.eq_ignore_ascii_case("true") || v == "1");
let show_diff =
std::env::var("TIDY_PRINT_DIFF").is_ok_and(|v| v.eq_ignore_ascii_case("true") || v == "1");

// Split comma-separated args up
let lint_args = match extra_checks {
Expand All @@ -72,6 +74,8 @@ fn check_impl(
let shell_lint = lint_args.contains(&"shell:lint") || shell_all;
let cpp_all = lint_args.contains(&"cpp");
let cpp_fmt = lint_args.contains(&"cpp:fmt") || cpp_all;
let js_all = lint_args.contains(&"js");
let js_lint = lint_args.contains(&"js:lint") || js_all;

let mut py_path = None;

Expand Down Expand Up @@ -224,6 +228,44 @@ fn check_impl(
shellcheck_runner(&merge_args(&cfg_args, &file_args_shc))?;
}

if js_lint {
eprintln!("running `eslint` on JS files");
let src_path = root_path.join("src");
let eslint_version_path =
src_path.join("ci/docker/host-x86_64/mingw-check-tidy/eslint.version");
let eslint_version = fs::read_to_string(&eslint_version_path)
.map_err(|error| {
Error::Generic(format!(
"failed to read `eslint` version file `{}`: {error:?}",
eslint_version_path.display()
))
})?
.trim()
.to_string();
let mut files_to_check = Vec::new();
let librustdoc_path = src_path.join("librustdoc");
let tools_path = src_path.join("tools");
walk_no_read(
&[&librustdoc_path.join("html/static/js")],
|path, is_dir| is_dir || path.extension() != Some(OsStr::new("js")),
&mut |path| {
files_to_check.push(path.path().into());
},
);
run_eslint(&eslint_version, &files_to_check, librustdoc_path.join("html/static"))?;

run_eslint(
&eslint_version,
&[tools_path.join("rustdoc-js/tester.js")],
tools_path.join("rustdoc-js"),
)?;
run_eslint(
&eslint_version,
&[tools_path.join("rustdoc-gui/tester.js")],
tools_path.join("rustdoc-gui"),
)?;
}

Ok(())
}

Expand Down Expand Up @@ -532,6 +574,24 @@ fn find_with_extension(
Ok(output)
}

fn run_eslint(eslint_version: &str, args: &[PathBuf], config_folder: PathBuf) -> Result<(), Error> {
match Command::new("npx")
.arg(format!("eslint@{eslint_version}"))
.arg("-c")
.arg(config_folder.join(".eslintrc.js"))
.args(args)
.output()
{
Ok(output) if output.status.success() => Ok(()),
Ok(_) => Err(Error::FailedCheck("eslint")),
Err(_) => Err(Error::MissingReq(
"`npx`",
"`eslint` JS linter",
Some("`npx` comes bundled with `node` and `npm`".to_string()),
)),
}
}

#[derive(Debug)]
enum Error {
Io(io::Error),
Expand Down
1 change: 0 additions & 1 deletion src/tools/tidy/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@ pub mod mir_opt_tests;
pub mod pal;
pub mod rustdoc_css_themes;
pub mod rustdoc_gui_tests;
pub mod rustdoc_js;
pub mod rustdoc_json;
pub mod rustdoc_templates;
pub mod style;
Expand Down
2 changes: 0 additions & 2 deletions src/tools/tidy/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ fn main() {
let library_path = root_path.join("library");
let compiler_path = root_path.join("compiler");
let librustdoc_path = src_path.join("librustdoc");
let tools_path = src_path.join("tools");
let crashes_path = tests_path.join("crashes");

let args: Vec<String> = env::args().skip(1).collect();
Expand Down Expand Up @@ -109,7 +108,6 @@ fn main() {
check!(rustdoc_gui_tests, &tests_path);
check!(rustdoc_css_themes, &librustdoc_path);
check!(rustdoc_templates, &librustdoc_path);
check!(rustdoc_js, &librustdoc_path, &tools_path, &src_path);
check!(rustdoc_json, &src_path);
check!(known_bug, &crashes_path);
check!(unknown_revision, &tests_path);
Expand Down
99 changes: 0 additions & 99 deletions src/tools/tidy/src/rustdoc_js.rs

This file was deleted.

Loading