@@ -33,11 +33,11 @@ use crate::opts::{Coloring, Expand, Subcommand};
33
33
use crate :: unparse:: unparse_maximal;
34
34
use crate :: version:: Version ;
35
35
use bat:: { PagingMode , PrettyPrinter } ;
36
- use clap:: { Parser , ValueEnum } ;
36
+ use clap:: { CommandFactory as _ , Parser , ValueEnum } ;
37
37
use quote:: quote;
38
38
use std:: env;
39
39
use std:: error:: Error as StdError ;
40
- use std:: ffi:: OsString ;
40
+ use std:: ffi:: { OsStr , OsString } ;
41
41
use std:: io:: { self , BufRead , IsTerminal , Write } ;
42
42
use std:: iter;
43
43
use std:: panic:: { self , PanicInfo , UnwindSafe } ;
@@ -51,7 +51,12 @@ use termcolor::{Color::Green, ColorChoice, ColorSpec, StandardStream, WriteColor
51
51
cargo_subcommand_metadata:: description!( "Show result of macro expansion" ) ;
52
52
53
53
fn main ( ) {
54
- let result = cargo_expand ( ) ;
54
+ let result = if let Some ( wrapper) = env:: var_os ( CARGO_EXPAND_RUSTC_WRAPPER ) {
55
+ do_rustc_wrapper ( & wrapper)
56
+ } else {
57
+ do_cargo_expand ( )
58
+ } ;
59
+
55
60
process:: exit ( match result {
56
61
Ok ( code) => code,
57
62
Err ( err) => {
@@ -67,11 +72,40 @@ fn main() {
67
72
} ) ;
68
73
}
69
74
75
+ const CARGO_EXPAND_RUSTC_WRAPPER : & str = "CARGO_EXPAND_RUSTC_WRAPPER" ;
76
+ const ARG_Z_UNPRETTY_EXPANDED : & str = "-Zunpretty=expanded" ;
77
+
70
78
fn cargo_binary ( ) -> OsString {
71
79
env:: var_os ( "CARGO" ) . unwrap_or_else ( || "cargo" . to_owned ( ) . into ( ) )
72
80
}
73
81
74
- fn cargo_expand ( ) -> Result < i32 > {
82
+ fn do_rustc_wrapper ( wrapper : & OsStr ) -> Result < i32 > {
83
+ let mut rustc_command = env:: args_os ( ) . skip ( 1 ) ;
84
+ let mut cmd = if wrapper != "/" {
85
+ Command :: new ( wrapper)
86
+ } else if let Some ( rustc) = rustc_command. next ( ) {
87
+ Command :: new ( rustc)
88
+ } else {
89
+ Subcommand :: command ( ) . print_help ( ) ?;
90
+ return Ok ( 1 ) ;
91
+ } ;
92
+
93
+ let mut is_unpretty_expanded = false ;
94
+ for arg in rustc_command {
95
+ is_unpretty_expanded |= arg == ARG_Z_UNPRETTY_EXPANDED ;
96
+ cmd. arg ( arg) ;
97
+ }
98
+
99
+ if is_unpretty_expanded {
100
+ cmd. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
101
+ }
102
+
103
+ let exit_status = cmd. status ( ) ?;
104
+ let code = exit_status. code ( ) . unwrap_or ( 1 ) ;
105
+ Ok ( code)
106
+ }
107
+
108
+ fn do_cargo_expand ( ) -> Result < i32 > {
75
109
let Subcommand :: Expand ( args) = Subcommand :: parse ( ) ;
76
110
77
111
if args. version {
@@ -124,9 +158,18 @@ fn cargo_expand() -> Result<i32> {
124
158
// Run cargo
125
159
let mut cmd = Command :: new ( cargo_binary ( ) ) ;
126
160
apply_args ( & mut cmd, & args, & color, & outfile_path) ;
161
+
127
162
if needs_rustc_bootstrap ( ) {
128
- cmd. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
163
+ if let Ok ( current_exe) = env:: current_exe ( ) {
164
+ let original_wrapper = env:: var_os ( "RUSTC_WRAPPER" ) ;
165
+ let wrapper = original_wrapper. as_deref ( ) . unwrap_or ( OsStr :: new ( "/" ) ) ;
166
+ cmd. env ( CARGO_EXPAND_RUSTC_WRAPPER , wrapper) ;
167
+ cmd. env ( "RUSTC_WRAPPER" , current_exe) ;
168
+ } else {
169
+ cmd. env ( "RUSTC_BOOTSTRAP" , "1" ) ;
170
+ }
129
171
}
172
+
130
173
let code = filter_err ( & mut cmd) ?;
131
174
132
175
if !outfile_path. exists ( ) {
@@ -402,7 +445,7 @@ fn apply_args(cmd: &mut Command, args: &Expand, color: &Coloring, outfile: &Path
402
445
403
446
line. arg ( "-o" ) ;
404
447
line. arg ( outfile) ;
405
- line. arg ( "-Zunpretty=expanded" ) ;
448
+ line. arg ( ARG_Z_UNPRETTY_EXPANDED ) ;
406
449
407
450
if args. verbose {
408
451
let mut display = line. clone ( ) ;
0 commit comments