@@ -10,7 +10,6 @@ pub mod pre_commit;
10
10
pub mod codegen;
11
11
mod ast_src;
12
12
13
- use anyhow:: Context ;
14
13
use std:: {
15
14
env,
16
15
io:: Write ,
@@ -24,9 +23,9 @@ use crate::{
24
23
not_bash:: { date_iso, fs2, pushd, rm_rf, run} ,
25
24
} ;
26
25
27
- pub use anyhow:: Result ;
26
+ pub use anyhow:: { bail , Context as _ , Result } ;
28
27
29
- const TOOLCHAIN : & str = "stable" ;
28
+ const RUSTFMT_TOOLCHAIN : & str = "stable" ;
30
29
31
30
pub fn project_root ( ) -> PathBuf {
32
31
Path :: new (
@@ -57,15 +56,25 @@ pub fn run_rustfmt(mode: Mode) -> Result<()> {
57
56
let _dir = pushd ( project_root ( ) ) ;
58
57
ensure_rustfmt ( ) ?;
59
58
60
- let check = if mode == Mode :: Verify { "--check" } else { "" } ;
61
- run ! ( "rustup run {} -- cargo fmt -- {}" , TOOLCHAIN , check) ?;
62
- Ok ( ( ) )
59
+ if Command :: new ( "cargo" )
60
+ . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
61
+ . args ( & [ "fmt" , "--" ] )
62
+ . args ( if mode == Mode :: Verify { & [ "--check" ] [ ..] } else { & [ ] } )
63
+ . stderr ( Stdio :: inherit ( ) )
64
+ . status ( ) ?
65
+ . success ( )
66
+ {
67
+ Ok ( ( ) )
68
+ } else {
69
+ bail ! ( "Rustfmt failed" ) ;
70
+ }
63
71
}
64
72
65
73
fn reformat ( text : impl std:: fmt:: Display ) -> Result < String > {
66
74
ensure_rustfmt ( ) ?;
67
- let mut rustfmt = Command :: new ( "rustup" )
68
- . args ( & [ "run" , TOOLCHAIN , "--" , "rustfmt" , "--config-path" ] )
75
+ let mut rustfmt = Command :: new ( "rustfmt" )
76
+ . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
77
+ . args ( & [ "--config-path" ] )
69
78
. arg ( project_root ( ) . join ( "rustfmt.toml" ) )
70
79
. args ( & [ "--config" , "fn_single_line=true" ] )
71
80
. stdin ( Stdio :: piped ( ) )
@@ -79,29 +88,42 @@ fn reformat(text: impl std::fmt::Display) -> Result<String> {
79
88
}
80
89
81
90
fn ensure_rustfmt ( ) -> Result < ( ) > {
82
- match Command :: new ( "rustup" )
83
- . args ( & [ "run" , TOOLCHAIN , "--" , "cargo" , "fmt" , "--version" ] )
91
+ match Command :: new ( "rustfmt" )
92
+ . args ( & [ "--version" ] )
93
+ . env ( "RUSTUP_TOOLCHAIN" , RUSTFMT_TOOLCHAIN )
94
+ . stdout ( Stdio :: piped ( ) )
84
95
. stderr ( Stdio :: null ( ) )
85
- . stdout ( Stdio :: null ( ) )
86
- . status ( )
96
+ . spawn ( )
97
+ . and_then ( |child| child . wait_with_output ( ) )
87
98
{
88
- Ok ( status) if status. success ( ) => return Ok ( ( ) ) ,
89
- _ => ( ) ,
90
- } ;
91
- run ! ( "rustup toolchain install {}" , TOOLCHAIN ) ?;
92
- run ! ( "rustup component add rustfmt --toolchain {}" , TOOLCHAIN ) ?;
93
- Ok ( ( ) )
99
+ Ok ( output)
100
+ if output. status . success ( )
101
+ && std:: str:: from_utf8 ( & output. stdout ) ?. contains ( RUSTFMT_TOOLCHAIN ) =>
102
+ {
103
+ Ok ( ( ) )
104
+ }
105
+ _ => {
106
+ bail ! (
107
+ "Failed to run rustfmt from toolchain '{0}'. \
108
+ Please run `rustup component add rustfmt --toolchain {0}` to install it.",
109
+ RUSTFMT_TOOLCHAIN ,
110
+ ) ;
111
+ }
112
+ }
94
113
}
95
114
96
115
pub fn run_clippy ( ) -> Result < ( ) > {
97
- match Command :: new ( "rustup " )
98
- . args ( & [ "run" , TOOLCHAIN , "--" , "cargo" , " clippy", "--version" ] )
116
+ match Command :: new ( "cargo " )
117
+ . args ( & [ "clippy" , "--version" ] )
99
118
. stderr ( Stdio :: null ( ) )
100
119
. stdout ( Stdio :: null ( ) )
101
120
. status ( )
102
121
{
103
122
Ok ( status) if status. success ( ) => ( ) ,
104
- _ => install_clippy ( ) . context ( "install clippy" ) ?,
123
+ _ => bail ! (
124
+ "Failed run cargo clippy. \
125
+ Please run `rustup component add clippy` to install it.",
126
+ ) ,
105
127
} ;
106
128
107
129
let allowed_lints = [
@@ -110,17 +132,7 @@ pub fn run_clippy() -> Result<()> {
110
132
"clippy::nonminimal_bool" ,
111
133
"clippy::redundant_pattern_matching" ,
112
134
] ;
113
- run ! (
114
- "rustup run {} -- cargo clippy --all-features --all-targets -- -A {}" ,
115
- TOOLCHAIN ,
116
- allowed_lints. join( " -A " )
117
- ) ?;
118
- Ok ( ( ) )
119
- }
120
-
121
- fn install_clippy ( ) -> Result < ( ) > {
122
- run ! ( "rustup toolchain install {}" , TOOLCHAIN ) ?;
123
- run ! ( "rustup component add clippy --toolchain {}" , TOOLCHAIN ) ?;
135
+ run ! ( "cargo clippy --all-features --all-targets -- -A {}" , allowed_lints. join( " -A " ) ) ?;
124
136
Ok ( ( ) )
125
137
}
126
138
@@ -130,7 +142,29 @@ pub fn run_fuzzer() -> Result<()> {
130
142
run ! ( "cargo install cargo-fuzz" ) ?;
131
143
} ;
132
144
133
- run ! ( "rustup run nightly -- cargo fuzz run parser" ) ?;
145
+ // Expecting nightly rustc
146
+ match Command :: new ( "rustc" )
147
+ . args ( & [ "--version" ] )
148
+ . env ( "RUSTUP_TOOLCHAIN" , "nightly" )
149
+ . stdout ( Stdio :: piped ( ) )
150
+ . stderr ( Stdio :: null ( ) )
151
+ . spawn ( )
152
+ . and_then ( |child| child. wait_with_output ( ) )
153
+ {
154
+ Ok ( output)
155
+ if output. status . success ( )
156
+ && std:: str:: from_utf8 ( & output. stdout ) ?. contains ( "nightly" ) => { }
157
+ _ => bail ! ( "fuzz tests require nightly rustc" ) ,
158
+ }
159
+
160
+ let status = Command :: new ( "cargo" )
161
+ . env ( "RUSTUP_TOOLCHAIN" , "nightly" )
162
+ . args ( & [ "fuzz" , "run" , "parser" ] )
163
+ . stderr ( Stdio :: inherit ( ) )
164
+ . status ( ) ?;
165
+ if !status. success ( ) {
166
+ bail ! ( "{}" , status) ;
167
+ }
134
168
Ok ( ( ) )
135
169
}
136
170
0 commit comments