Skip to content

Commit fe7e0c5

Browse files
committed
tidy: make npm_install use root_path and improve error reporting
1 parent f303b86 commit fe7e0c5

File tree

2 files changed

+24
-56
lines changed

2 files changed

+24
-56
lines changed

src/tools/tidy/src/ext_tool_checks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ fn check_impl(
296296
}
297297

298298
if js_lint || js_typecheck {
299-
rustdoc_js::npm_install(outdir)?;
299+
rustdoc_js::npm_install(root_path, outdir)?;
300300
}
301301

302302
if js_lint {

src/tools/tidy/src/ext_tool_checks/rustdoc_js.rs

Lines changed: 23 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,34 @@
22
//! characters.
33
44
use std::ffi::OsStr;
5-
use std::fs;
5+
use std::{fs, io};
66
use std::path::{Path, PathBuf};
7-
use std::process::Command;
7+
use std::process::{Child, Command};
88

99
use build_helper::ci::CiEnv;
1010
use ignore::DirEntry;
1111

1212
use crate::walk::walk_no_read;
1313

1414
fn node_module_bin(outdir: &Path, name: &str) -> PathBuf {
15-
outdir.join(name)
15+
outdir.join("node_modules/.bin").join(name)
16+
}
17+
18+
fn spawn_cmd(cmd: &mut Command) -> Result<Child, io::Error> {
19+
cmd.spawn().map_err(|err| {
20+
eprintln!("unable to run {cmd:?} due to {err:?}");
21+
err
22+
})
1623
}
1724

1825
/// install all js dependencies from package.json.
19-
pub(super) fn npm_install(outdir: &Path) -> Result<(), super::Error> {
20-
let copy_to_build = |p| fs::copy(p, outdir.join(p));
26+
pub(super) fn npm_install(root_path: &Path, outdir: &Path) -> Result<(), super::Error> {
27+
let copy_to_build = |p| {
28+
fs::copy(root_path.join(p), outdir.join(p)).map_err(|e| {
29+
eprintln!("unable to copy {p:?} to build directory: {e:?}");
30+
e
31+
})
32+
};
2133
// copy stuff to the output directory to make node_modules get put there.
2234
copy_to_build("package.json")?;
2335
copy_to_build("package-lock.json")?;
@@ -34,7 +46,7 @@ pub(super) fn npm_install(outdir: &Path) -> Result<(), super::Error> {
3446
// of repeated tidy invokations.
3547
cmd.args(&["--audit=false", "--save=false", "--fund=false"]);
3648
cmd.current_dir(outdir);
37-
let mut child = cmd.spawn()?;
49+
let mut child = spawn_cmd(cmd)?;
3850
match child.wait() {
3951
Ok(exit_status) => {
4052
if exit_status.success() {
@@ -59,11 +71,10 @@ fn rustdoc_js_files(librustdoc_path: &Path) -> Vec<PathBuf> {
5971
}
6072

6173
fn run_eslint(outdir: &Path, args: &[PathBuf], config_folder: PathBuf) -> Result<(), super::Error> {
62-
let mut child = Command::new(node_module_bin(outdir, "eslint"))
74+
let mut child = spawn_cmd(Command::new(node_module_bin(outdir, "eslint"))
6375
.arg("-c")
6476
.arg(config_folder.join(".eslintrc.js"))
65-
.args(args)
66-
.spawn()?;
77+
.args(args))?;
6778
match child.wait() {
6879
Ok(exit_status) => {
6980
if exit_status.success() {
@@ -75,21 +86,6 @@ fn run_eslint(outdir: &Path, args: &[PathBuf], config_folder: PathBuf) -> Result
7586
}
7687
}
7788

78-
fn get_eslint_version_inner(global: bool) -> Option<String> {
79-
let mut command = Command::new("npm");
80-
command.arg("list").arg("--parseable").arg("--long").arg("--depth=0");
81-
if global {
82-
command.arg("--global");
83-
}
84-
let output = command.output().ok()?;
85-
let lines = String::from_utf8_lossy(&output.stdout);
86-
lines.lines().find_map(|l| l.split(':').nth(1)?.strip_prefix("eslint@")).map(|v| v.to_owned())
87-
}
88-
89-
fn get_eslint_version() -> Option<String> {
90-
get_eslint_version_inner(false).or_else(|| get_eslint_version_inner(true))
91-
}
92-
9389
pub(super) fn lint(
9490
outdir: &Path,
9591
librustdoc_path: &Path,
@@ -104,33 +100,6 @@ pub(super) fn lint(
104100
return Err(error.into());
105101
}
106102
};
107-
// Having the correct `eslint` version installed via `npm` isn't strictly necessary, since we're invoking it via `npx`,
108-
// but this check allows the vast majority that is not working on the rustdoc frontend to avoid the penalty of running
109-
// `eslint` in tidy. See also: https://github.com/rust-lang/rust/pull/142851
110-
match get_eslint_version() {
111-
Some(version) => {
112-
if version != eslint_version {
113-
// unfortunatly we can't use `Error::Version` here becuse `str::trim` isn't const and
114-
// Version::required must be a static str
115-
return Err(super::Error::Generic(format!(
116-
"⚠️ Installed version of eslint (`{version}`) is different than the \
117-
one used in the CI (`{eslint_version}`)\n\
118-
You can install this version using `npm update eslint` or by using \
119-
`npm install eslint@{eslint_version}`\n
120-
"
121-
)));
122-
}
123-
}
124-
None => {
125-
//eprintln!("`eslint` doesn't seem to be installed. Skipping tidy check for JS files.");
126-
//eprintln!("You can install it using `npm install eslint@{eslint_version}`");
127-
return Err(super::Error::MissingReq(
128-
"eslint",
129-
"js lint checks",
130-
Some(format!("You can install it using `npm install eslint@{eslint_version}`")),
131-
));
132-
}
133-
}
134103
let files_to_check = rustdoc_js_files(librustdoc_path);
135104
println!("Running eslint on rustdoc JS files");
136105
run_eslint(outdir, &files_to_check, librustdoc_path.join("html/static"))?;
@@ -146,10 +115,9 @@ pub(super) fn lint(
146115

147116
pub(super) fn typecheck(outdir: &Path, librustdoc_path: &Path) -> Result<(), super::Error> {
148117
// use npx to ensure correct version
149-
let mut child = Command::new(node_module_bin(outdir, "tsc"))
118+
let mut child = spawn_cmd(Command::new(node_module_bin(outdir, "tsc"))
150119
.arg("-p")
151-
.arg(librustdoc_path.join("html/static/js/tsconfig.json"))
152-
.spawn()?;
120+
.arg(librustdoc_path.join("html/static/js/tsconfig.json")))?;
153121
match child.wait() {
154122
Ok(exit_status) => {
155123
if exit_status.success() {
@@ -168,7 +136,7 @@ pub(super) fn es_check(outdir: &Path, librustdoc_path: &Path) -> Result<(), supe
168136
for f in files_to_check {
169137
cmd.arg(f);
170138
}
171-
match cmd.spawn()?.wait() {
139+
match spawn_cmd(cmd)?.wait() {
172140
Ok(exit_status) => {
173141
if exit_status.success() {
174142
return Ok(());

0 commit comments

Comments
 (0)