Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions bevy_lint/src/bin/bevy_lint_driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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
Expand Down
5 changes: 0 additions & 5 deletions bevy_lint/tests/test_utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,6 @@ pub fn base_config(test_dir: &str) -> color_eyre::Result<Config> {
);

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`.
Expand Down
Loading