@@ -5,7 +5,7 @@ extern crate rustc_driver;
55extern crate rustc_session;
66extern crate rustc_span;
77
8- use std:: process:: ExitCode ;
8+ use std:: { ffi :: OsStr , path :: Path , process:: ExitCode } ;
99
1010use bevy_lint:: BevyLintCallback ;
1111use rustc_driver:: { catch_with_exit_code, init_rustc_env_logger, install_ice_hook, run_compiler} ;
@@ -31,12 +31,24 @@ fn main() -> ExitCode {
3131 // Run the passed closure, but catch any panics and return the respective exit code.
3232 let exit_code = catch_with_exit_code ( move || {
3333 // Get the arguments passed through the CLI. This is equivalent to `std::env::args()`, but
34- // it returns a `Result` instead of panicking.
34+ // it prints a pretty error message instead of panicking when encountering non-UTF-8 args .
3535 let mut args = rustc_driver:: args:: raw_args ( & early_dcx) ;
3636
37- // The arguments are formatted as `[DRIVER_PATH, RUSTC_PATH, ARGS...]`. We skip the driver
38- // path so that `run_compiler()` just sees `rustc`'s path.
39- args. remove ( 0 ) ;
37+ // There are two scenarios we want to catch:
38+ // 1. When called by Cargo: `[DRIVER_PATH, RUSTC_PATH, ...ARGS]`
39+ // 2. When called by user: `[DRIVER_PATH, ...ARGS]`
40+ //
41+ // This handles both cases and converts the args to `[RUSTC_PATH, ...ARGS]`, since that is
42+ // what `run_compiler()` expects.
43+ let args =
44+ if args. get ( 1 ) . map ( Path :: new) . and_then ( Path :: file_stem) == Some ( OsStr :: new ( "rustc" ) ) {
45+ // When called by Cargo, remove the driver path.
46+ & args[ 1 ..]
47+ } else {
48+ // When called by user, replace the driver path with the `rustc` path.
49+ args[ 0 ] = "rustc" . to_string ( ) ;
50+ & args
51+ } ;
4052
4153 // Call the compiler with our custom callback.
4254 run_compiler ( & args, & mut BevyLintCallback ) ;
0 commit comments