2
2
//! characters.
3
3
4
4
use std:: ffi:: OsStr ;
5
- use std:: fs ;
5
+ use std:: { fs , io } ;
6
6
use std:: path:: { Path , PathBuf } ;
7
- use std:: process:: Command ;
7
+ use std:: process:: { Child , Command } ;
8
8
9
9
use build_helper:: ci:: CiEnv ;
10
10
use ignore:: DirEntry ;
11
11
12
12
use crate :: walk:: walk_no_read;
13
13
14
14
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
+ } )
16
23
}
17
24
18
25
/// 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
+ } ;
21
33
// copy stuff to the output directory to make node_modules get put there.
22
34
copy_to_build ( "package.json" ) ?;
23
35
copy_to_build ( "package-lock.json" ) ?;
@@ -34,7 +46,7 @@ pub(super) fn npm_install(outdir: &Path) -> Result<(), super::Error> {
34
46
// of repeated tidy invokations.
35
47
cmd. args ( & [ "--audit=false" , "--save=false" , "--fund=false" ] ) ;
36
48
cmd. current_dir ( outdir) ;
37
- let mut child = cmd . spawn ( ) ?;
49
+ let mut child = spawn_cmd ( cmd ) ?;
38
50
match child. wait ( ) {
39
51
Ok ( exit_status) => {
40
52
if exit_status. success ( ) {
@@ -59,11 +71,10 @@ fn rustdoc_js_files(librustdoc_path: &Path) -> Vec<PathBuf> {
59
71
}
60
72
61
73
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" ) )
63
75
. arg ( "-c" )
64
76
. arg ( config_folder. join ( ".eslintrc.js" ) )
65
- . args ( args)
66
- . spawn ( ) ?;
77
+ . args ( args) ) ?;
67
78
match child. wait ( ) {
68
79
Ok ( exit_status) => {
69
80
if exit_status. success ( ) {
@@ -75,21 +86,6 @@ fn run_eslint(outdir: &Path, args: &[PathBuf], config_folder: PathBuf) -> Result
75
86
}
76
87
}
77
88
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
-
93
89
pub ( super ) fn lint (
94
90
outdir : & Path ,
95
91
librustdoc_path : & Path ,
@@ -104,33 +100,6 @@ pub(super) fn lint(
104
100
return Err ( error. into ( ) ) ;
105
101
}
106
102
} ;
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
- }
134
103
let files_to_check = rustdoc_js_files ( librustdoc_path) ;
135
104
println ! ( "Running eslint on rustdoc JS files" ) ;
136
105
run_eslint ( outdir, & files_to_check, librustdoc_path. join ( "html/static" ) ) ?;
@@ -146,10 +115,9 @@ pub(super) fn lint(
146
115
147
116
pub ( super ) fn typecheck ( outdir : & Path , librustdoc_path : & Path ) -> Result < ( ) , super :: Error > {
148
117
// 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" ) )
150
119
. arg ( "-p" )
151
- . arg ( librustdoc_path. join ( "html/static/js/tsconfig.json" ) )
152
- . spawn ( ) ?;
120
+ . arg ( librustdoc_path. join ( "html/static/js/tsconfig.json" ) ) ) ?;
153
121
match child. wait ( ) {
154
122
Ok ( exit_status) => {
155
123
if exit_status. success ( ) {
@@ -168,7 +136,7 @@ pub(super) fn es_check(outdir: &Path, librustdoc_path: &Path) -> Result<(), supe
168
136
for f in files_to_check {
169
137
cmd. arg ( f) ;
170
138
}
171
- match cmd . spawn ( ) ?. wait ( ) {
139
+ match spawn_cmd ( cmd ) ?. wait ( ) {
172
140
Ok ( exit_status) => {
173
141
if exit_status. success ( ) {
174
142
return Ok ( ( ) ) ;
0 commit comments