diff --git a/bevy_lint/src/bin/bevy_lint_driver.rs b/bevy_lint/src/bin/bevy_lint_driver.rs index 5793c893..692faf38 100644 --- a/bevy_lint/src/bin/bevy_lint_driver.rs +++ b/bevy_lint/src/bin/bevy_lint_driver.rs @@ -9,7 +9,7 @@ extern crate rustc_driver; extern crate rustc_session; extern crate rustc_span; -use std::process::ExitCode; +use std::{ffi::OsStr, path::Path, process::ExitCode}; use bevy_lint::BevyLintCallback; use rustc_driver::{catch_with_exit_code, init_rustc_env_logger, install_ice_hook, run_compiler}; @@ -35,15 +35,27 @@ fn main() -> ExitCode { // Run the passed closure, but catch any panics and return the respective exit code. let exit_code = catch_with_exit_code(move || { // Get the arguments passed through the CLI. This is equivalent to `std::env::args()`, but - // it returns a `Result` instead of panicking. + // it prints a pretty error message instead of panicking when encountering non-UTF-8 args. let mut args = rustc_driver::args::raw_args(&early_dcx); - // The arguments are formatted as `[DRIVER_PATH, RUSTC_PATH, ARGS...]`. We skip the driver - // path so that `run_compiler()` just sees `rustc`'s path. - args.remove(0); + // There are two scenarios we want to catch: + // 1. When called by Cargo: `[DRIVER_PATH, RUSTC_PATH, ...ARGS]` + // 2. When called by user: `[DRIVER_PATH, ...ARGS]` + // + // This handles both cases and converts the args to `[RUSTC_PATH, ...ARGS]`, since that is + // what `run_compiler()` expects. + let args = + if args.get(1).map(Path::new).and_then(Path::file_stem) == Some(OsStr::new("rustc")) { + // When called by Cargo, remove the driver path. + &args[1..] + } else { + // When called by user, replace the driver path with the `rustc` path. + args[0] = "rustc".to_string(); + &args + }; // Call the compiler with our custom callback. - run_compiler(&args, &mut BevyLintCallback); + run_compiler(args, &mut BevyLintCallback); }); // We truncate the `i32` to a `u8`. `catch_with_exit_code()` currently only returns 1 or 0, so diff --git a/bevy_lint/tests/test_utils/mod.rs b/bevy_lint/tests/test_utils/mod.rs index 86f2f393..caf2dcaf 100644 --- a/bevy_lint/tests/test_utils/mod.rs +++ b/bevy_lint/tests/test_utils/mod.rs @@ -27,11 +27,6 @@ pub fn base_config(test_dir: &str) -> color_eyre::Result { ); let config = Config { - // When `host` is `None`, `ui_test` will attempt to auto-discover the host by calling - // `program -vV`. Unfortunately, `bevy_lint_driver` does not yet support the version flag, - // so we manually specify the host as an empty string. This means that, for now, host- - // specific configuration in UI tests will not work. - host: Some(String::new()), program: CommandBuilder { // We call `rustup run` to setup the proper environmental variables, so that // `bevy_lint_driver` can link to `librustc_driver.so`.