6
6
7
7
use std:: {
8
8
fmt, io,
9
- path:: Path ,
10
9
process:: { ChildStderr , ChildStdout , Command , Stdio } ,
11
10
time:: Duration ,
12
11
} ;
@@ -25,7 +24,6 @@ pub use cargo_metadata::diagnostic::{
25
24
#[ derive( Copy , Clone , Debug , Default , PartialEq , Eq ) ]
26
25
pub enum InvocationStrategy {
27
26
OnceInRoot ,
28
- PerWorkspaceWithManifestPath ,
29
27
#[ default]
30
28
PerWorkspace ,
31
29
}
@@ -153,7 +151,9 @@ struct FlycheckActor {
153
151
id : usize ,
154
152
sender : Box < dyn Fn ( Message ) + Send > ,
155
153
config : FlycheckConfig ,
156
- workspace_root : AbsPathBuf ,
154
+ /// Either the workspace root of the workspace we are flychecking,
155
+ /// or the project root of the project.
156
+ root : AbsPathBuf ,
157
157
/// CargoHandle exists to wrap around the communication needed to be able to
158
158
/// run `cargo check` without blocking. Currently the Rust standard library
159
159
/// doesn't provide a way to read sub-process output without blocking, so we
@@ -175,7 +175,7 @@ impl FlycheckActor {
175
175
workspace_root : AbsPathBuf ,
176
176
) -> FlycheckActor {
177
177
tracing:: info!( %id, ?workspace_root, "Spawning flycheck" ) ;
178
- FlycheckActor { id, sender, config, workspace_root, cargo_handle : None }
178
+ FlycheckActor { id, sender, config, root : workspace_root, cargo_handle : None }
179
179
}
180
180
181
181
fn report_progress ( & self , progress : Progress ) {
@@ -201,20 +201,7 @@ impl FlycheckActor {
201
201
self . cancel_check_process ( ) ;
202
202
while let Ok ( _) = inbox. recv_timeout ( Duration :: from_millis ( 50 ) ) { }
203
203
204
- let mut command = self . check_command ( ) ;
205
- let invocation_strategy = self . invocation_strategy ( ) ;
206
- match invocation_strategy {
207
- InvocationStrategy :: OnceInRoot => ( ) ,
208
- InvocationStrategy :: PerWorkspaceWithManifestPath => {
209
- command. arg ( "--manifest-path" ) ;
210
- command. arg ( <_ as AsRef < Path > >:: as_ref (
211
- & self . workspace_root . join ( "Cargo.toml" ) ,
212
- ) ) ;
213
- }
214
- InvocationStrategy :: PerWorkspace => {
215
- command. current_dir ( & self . workspace_root ) ;
216
- }
217
- }
204
+ let command = self . check_command ( ) ;
218
205
tracing:: debug!( ?command, "will restart flycheck" ) ;
219
206
match CargoHandle :: spawn ( command) {
220
207
Ok ( cargo_handle) => {
@@ -256,7 +243,7 @@ impl FlycheckActor {
256
243
CargoMessage :: Diagnostic ( msg) => {
257
244
self . send ( Message :: AddDiagnostic {
258
245
id : self . id ,
259
- workspace_root : self . workspace_root . clone ( ) ,
246
+ workspace_root : self . root . clone ( ) ,
260
247
diagnostic : msg,
261
248
} ) ;
262
249
}
@@ -278,15 +265,8 @@ impl FlycheckActor {
278
265
}
279
266
}
280
267
281
- fn invocation_strategy ( & self ) -> InvocationStrategy {
282
- match self . config {
283
- FlycheckConfig :: CargoCommand { invocation_strategy, .. }
284
- | FlycheckConfig :: CustomCommand { invocation_strategy, .. } => invocation_strategy,
285
- }
286
- }
287
-
288
268
fn check_command ( & self ) -> Command {
289
- let mut cmd = match & self . config {
269
+ let ( mut cmd, args , invocation_strategy ) = match & self . config {
290
270
FlycheckConfig :: CargoCommand {
291
271
command,
292
272
target_triple,
@@ -296,13 +276,11 @@ impl FlycheckActor {
296
276
extra_args,
297
277
features,
298
278
extra_env,
299
- invocation_strategy : _ ,
279
+ invocation_strategy,
300
280
} => {
301
281
let mut cmd = Command :: new ( toolchain:: cargo ( ) ) ;
302
282
cmd. arg ( command) ;
303
- cmd. current_dir ( & self . workspace_root ) ;
304
- cmd. args ( & [ "--workspace" , "--message-format=json" , "--manifest-path" ] )
305
- . arg ( self . workspace_root . join ( "Cargo.toml" ) . as_os_str ( ) ) ;
283
+ cmd. args ( & [ "--workspace" , "--message-format=json" ] ) ;
306
284
307
285
if let Some ( target) = target_triple {
308
286
cmd. args ( & [ "--target" , target. as_str ( ) ] ) ;
@@ -321,18 +299,35 @@ impl FlycheckActor {
321
299
cmd. arg ( features. join ( " " ) ) ;
322
300
}
323
301
}
324
- cmd. args ( extra_args) ;
325
302
cmd. envs ( extra_env) ;
326
- cmd
303
+ ( cmd, extra_args , invocation_strategy )
327
304
}
328
- FlycheckConfig :: CustomCommand { command, args, extra_env, invocation_strategy : _ } => {
305
+ FlycheckConfig :: CustomCommand { command, args, extra_env, invocation_strategy } => {
329
306
let mut cmd = Command :: new ( command) ;
330
- cmd. args ( args) ;
331
307
cmd. envs ( extra_env) ;
332
- cmd
308
+ ( cmd, args , invocation_strategy )
333
309
}
334
310
} ;
335
- cmd. current_dir ( & self . workspace_root ) ;
311
+ if let InvocationStrategy :: PerWorkspace = invocation_strategy {
312
+ let mut with_manifest_path = false ;
313
+ for arg in args {
314
+ if let Some ( _) = arg. find ( "$manifest_path" ) {
315
+ with_manifest_path = true ;
316
+ cmd. arg ( arg. replace (
317
+ "$manifest_path" ,
318
+ & self . root . join ( "Cargo.toml" ) . display ( ) . to_string ( ) ,
319
+ ) ) ;
320
+ } else {
321
+ cmd. arg ( arg) ;
322
+ }
323
+ }
324
+
325
+ if !with_manifest_path {
326
+ cmd. current_dir ( & self . root ) ;
327
+ }
328
+ } else {
329
+ cmd. args ( args) ;
330
+ }
336
331
cmd
337
332
}
338
333
0 commit comments