8
8
// option. This file may not be copied, modified, or distributed
9
9
// except according to those terms.
10
10
11
- use cargo:: core:: { PackageId , Shell , Target , Workspace , Verbosity } ;
11
+ use cargo:: core:: { PackageId , Shell , Target , TargetKind , Workspace , Verbosity } ;
12
12
use cargo:: ops:: { compile_with_exec, Executor , Context , Packages , CompileOptions , CompileMode , CompileFilter , Unit } ;
13
- use cargo:: util:: { Config as CargoConfig , process_builder, ProcessBuilder , homedir, important_paths, ConfigValue , CargoResult } ;
14
- use cargo:: util:: errors:: { CargoErrorKind , process_error} ;
13
+ use cargo:: util:: { Config as CargoConfig , ProcessBuilder , homedir, important_paths, ConfigValue , CargoResult } ;
15
14
use serde_json;
16
15
17
16
use data:: Analysis ;
@@ -296,37 +295,25 @@ impl Executor for RlsExecutor {
296
295
}
297
296
}
298
297
299
- let rustc_exe = env :: var ( " RUSTC" ) . unwrap_or ( env :: args ( ) . next ( ) . unwrap ( ) ) ;
300
- let mut cmd = Command :: new ( & rustc_exe ) ;
298
+ // Cargo should handle `$ RUSTC` wrapper override already
299
+ let mut cmd = cargo_cmd . clone ( ) ;
301
300
cmd. env ( :: RUSTC_SHIM_ENV_VAR_NAME , "1" ) ;
302
301
303
302
// We only want to intercept rustc call targeting current crate to cache
304
303
// args/envs generated by cargo so we can run only rustc later ourselves
305
304
// Currently we don't cache nor modify build script args
306
- let is_build_script = crate_name == "build_script_build" ;
305
+ let is_build_script = * target . kind ( ) == TargetKind :: CustomBuild ;
307
306
if !self . is_primary_crate ( id) || is_build_script {
308
307
let build_script_notice = if is_build_script { " (build script)" } else { "" } ;
309
308
trace ! ( "rustc not intercepted - {}{}" , id. name( ) , build_script_notice) ;
310
309
310
+ // Recreate the command, minus -Zsave-analysis.
311
311
if :: CRATE_BLACKLIST . contains ( & & * crate_name) {
312
- // Recreate the command, minus -Zsave-analysis.
313
- for a in cargo_args {
314
- if a != "-Zsave-analysis" {
315
- cmd. arg ( a) ;
316
- }
317
- }
318
- cmd. envs ( cargo_cmd. get_envs ( ) . iter ( ) . filter_map ( |( k, v) | v. as_ref ( ) . map ( |v| ( k, v) ) ) ) ;
319
- if let Some ( cwd) = cargo_cmd. get_cwd ( ) {
320
- cmd. current_dir ( cwd) ;
321
- }
322
- return match cmd. status ( ) {
323
- Ok ( ref e) if e. success ( ) => Ok ( ( ) ) ,
324
- _ => Err ( CargoErrorKind :: ProcessErrorKind ( process_error (
325
- "process didn't exit successfully" , None , None ) ) . into ( ) ) ,
326
- } ;
327
- } else {
328
- return cargo_cmd. exec ( ) ;
312
+ let args: Vec < _ > = cmd. get_args ( ) . iter ( ) . cloned ( )
313
+ . filter ( |x| x != "Zsave-analysis" ) . collect ( ) ;
314
+ cmd. args_replace ( & args) ;
329
315
}
316
+ return cmd. exec ( ) ;
330
317
}
331
318
332
319
trace ! ( "rustc intercepted - args: {:?} envs: {:?}" , cargo_args, cargo_cmd. get_envs( ) ) ;
@@ -369,43 +356,28 @@ impl Executor for RlsExecutor {
369
356
// so the dep-info is ready by the time we return from this callback.
370
357
// NB: In `workspace_mode` regular compilation is performed here (and we don't
371
358
// only calculate dep-info) so it should fix the problem mentioned above.
372
- // Since ProcessBuilder doesn't allow to modify args, we need to create
373
- // our own command here from scratch here.
374
- for a in & args {
375
- // Emitting only dep-info is possible only for final crate type, as
376
- // as others may emit required metadata for dependent crate types
377
- if a. starts_with ( "--emit" ) && is_final_crate_type && !self . workspace_mode {
378
- cmd. arg ( "--emit=dep-info" ) ;
379
- } else if a != "-Zsave-analysis" {
380
- cmd. arg ( a) ;
381
- }
382
- }
383
- cmd. envs ( cargo_cmd. get_envs ( ) . iter ( ) . filter_map ( |( k, v) | v. as_ref ( ) . map ( |v| ( k, v) ) ) ) ;
384
- if let Some ( cwd) = cargo_cmd. get_cwd ( ) {
385
- cmd. current_dir ( cwd) ;
386
- }
359
+ let modified = args. iter ( )
360
+ . filter ( |a| * a != "-Zsave-analysis" )
361
+ . map ( |a| {
362
+ // Emitting only dep-info is possible only for final crate type, as
363
+ // as others may emit required metadata for dependent crate types
364
+ if a. starts_with ( "--emit" ) && is_final_crate_type && !self . workspace_mode {
365
+ "--emit=dep-info"
366
+ } else { a }
367
+ } )
368
+ . collect :: < Vec < _ > > ( ) ;
369
+ cmd. args_replace ( & modified) ;
387
370
}
388
371
389
372
// Cache executed command for the build plan
390
373
{
391
374
let mut cx = self . compilation_cx . lock ( ) . unwrap ( ) ;
392
-
393
- let mut store_cmd = process_builder:: process ( & rustc_exe) ;
394
- store_cmd. args ( & args. iter ( ) . map ( OsString :: from) . collect :: < Vec < _ > > ( ) ) ;
395
- for ( k, v) in cargo_cmd. get_envs ( ) . clone ( ) {
396
- if let Some ( v) = v {
397
- store_cmd. env ( & k, v) ;
398
- }
399
- }
400
- if let Some ( cwd) = cargo_cmd. get_cwd ( ) {
401
- cmd. current_dir ( cwd) ;
402
- }
403
-
404
- cx. build_plan . cache_compiler_job ( id, target, & store_cmd) ;
375
+ cx. build_plan . cache_compiler_job ( id, target, & cmd) ;
405
376
}
406
377
407
378
// Prepare modified cargo-generated args/envs for future rustc calls
408
- args. insert ( 0 , rustc_exe) ;
379
+ let rustc = cargo_cmd. get_program ( ) . to_owned ( ) . into_string ( ) . unwrap ( ) ;
380
+ args. insert ( 0 , rustc) ;
409
381
let envs = cargo_cmd. get_envs ( ) . clone ( ) ;
410
382
411
383
if self . workspace_mode {
@@ -425,7 +397,7 @@ impl Executor for RlsExecutor {
425
397
_ => { }
426
398
}
427
399
} else {
428
- cmd. status ( ) . expect ( "Couldn't execute rustc" ) ;
400
+ cmd. exec ( ) ? ;
429
401
}
430
402
431
403
// Finally, store the modified cargo-generated args/envs for future rustc calls
0 commit comments