Skip to content

Commit

Permalink
Do not unconditionally succeed RUSTC_WRAPPER when run by build scripts
Browse files Browse the repository at this point in the history
intellij-rust-native-helper in `RUSTC_WRAPPER` role unconditionally succeeds
 `cargo check` invocations tripping up build scripts using `cargo check` to
 probe for successful compilations. To prevent this from happening the
 `RUSTC_WRAPPER` now checks if it's run from a build script by looking for
 the `CARGO_CFG_TARGET_ARCH` env var that cargo sets only when running
 build scripts.
  • Loading branch information
vlad20012 committed Aug 24, 2022
1 parent ebcedfa commit cbe129a
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions native-helper/src/rustc_wrapper.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
use std::ffi::OsString;
use std::process::{Command, Stdio};
use std::io;
use std::process::{Command, Stdio};

pub struct ExitCode(pub Option<i32>);

pub fn run_rustc_skipping_cargo_checking(
rustc_executable: OsString,
args: Vec<OsString>,
) -> io::Result<ExitCode> {
// `CARGO_CFG_TARGET_ARCH` is only set by cargo when executing build scripts
// We don't want to exit out checks unconditionally with success if a build
// script tries to invoke checks themselves
// See https://github.com/rust-lang/rust-analyzer/issues/12973 for context
let is_invoked_by_build_script = std::env::var_os("CARGO_CFG_TARGET_ARCH").is_some();

let is_cargo_check = args.iter().any(|arg| {
let arg = arg.to_string_lossy();
// `cargo check` invokes `rustc` with `--emit=metadata` argument.
Expand All @@ -20,7 +26,7 @@ pub fn run_rustc_skipping_cargo_checking(
// The default output filename is CRATE_NAME.rmeta.
arg.starts_with("--emit=") && arg.contains("metadata") && !arg.contains("link")
});
if is_cargo_check {
if !is_invoked_by_build_script && is_cargo_check {
return Ok(ExitCode(Some(0)));
}
run_rustc(rustc_executable, args)
Expand Down

0 comments on commit cbe129a

Please sign in to comment.