Skip to content

Commit 2c07302

Browse files
committed
feat: support bevy_lint_driver being called without rustc path
1 parent f38247d commit 2c07302

File tree

2 files changed

+17
-10
lines changed

2 files changed

+17
-10
lines changed

bevy_lint/src/bin/driver.rs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ extern crate rustc_driver;
55
extern crate rustc_session;
66
extern crate rustc_span;
77

8-
use std::process::ExitCode;
8+
use std::{ffi::OsStr, path::Path, process::ExitCode};
99

1010
use bevy_lint::BevyLintCallback;
1111
use 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);

bevy_lint/tests/test_utils/mod.rs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,6 @@ pub fn base_config(test_dir: &str) -> color_eyre::Result<Config> {
2727
);
2828

2929
let config = Config {
30-
// When `host` is `None`, `ui_test` will attempt to auto-discover the host by calling
31-
// `program -vV`. Unfortunately, `bevy_lint_driver` does not yet support the version flag,
32-
// so we manually specify the host as an empty string. This means that, for now, host-
33-
// specific configuration in UI tests will not work.
34-
host: Some(String::new()),
3530
program: CommandBuilder {
3631
// We call `rustup run` to setup the proper environmental variables, so that
3732
// `bevy_lint_driver` can link to `librustc_driver.so`.

0 commit comments

Comments
 (0)