Skip to content

Commit e120b18

Browse files
committed
feat: Don't check antivirus when few binaries are affected
This is very much a heuristic to avoid outputting the note to users where it might not be relevant.
1 parent c2c5287 commit e120b18

File tree

1 file changed

+26
-7
lines changed
  • src/cargo/ops/cargo_compile

1 file changed

+26
-7
lines changed

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ use crate::core::compiler::UserIntent;
4343
use crate::core::compiler::unit_dependencies::build_unit_dependencies;
4444
use crate::core::compiler::unit_graph::{self, UnitDep, UnitGraph};
4545
use crate::core::compiler::{BuildConfig, BuildContext, BuildRunner, Compilation};
46-
use crate::core::compiler::{CompileKind, CompileTarget, RustcTargetData, Unit};
46+
use crate::core::compiler::{CompileKind, CompileMode, CompileTarget, RustcTargetData, Unit};
4747
use crate::core::compiler::{CrateType, TargetInfo, apply_env_config, standard_lib};
4848
use crate::core::compiler::{DefaultExecutor, Executor, UnitInterner};
4949
use crate::core::profiles::Profiles;
@@ -561,15 +561,34 @@ where `<compatible-ver>` is the latest version supporting rustc {rustc_version}"
561561
}
562562

563563
if build_config.detect_antivirus {
564-
// TODO(madsmtm): Maybe only do this when we have above a certain
565-
// number of build scripts or test binaries to run?
564+
// Count the number of test binaries and build scripts we'll need to
565+
// run. This doesn't take into account the binary that will be run
566+
// if `cargo run` was specified, and doesn't handle pre-2024 `rustdoc`
567+
// tests, but that's fine, this is only a heuristic.
568+
let num_binaries = unit_graph
569+
.keys()
570+
.filter(|unit| {
571+
matches!(
572+
unit.mode,
573+
CompileMode::Test | CompileMode::Doctest | CompileMode::RunCustomBuild
574+
)
575+
})
576+
.count();
577+
578+
tracing::debug!("estimated {num_binaries} binaries that could be slowed down by antivirus");
566579

567580
// TODO(madsmtm): Consider only warning once every X days.
568581

569-
// We don't want to do this check when installing, since there might
570-
// be `cargo install` users who are not necessarily developers (and so
571-
// the note will be irrelevant to them).
572-
if build_config.intent != UserIntent::Install {
582+
// Heuristic: Only do the check if we have to run more than a specific
583+
// number of binaries. This makes it so that small beginner projects
584+
// don't hit this.
585+
//
586+
// TODO(madsmtm): What should the threshold be?
587+
//
588+
// We also don't want to do this check when installing, since there
589+
// might be `cargo install` users who are not necessarily developers
590+
// (and so the note will be irrelevant to them).
591+
if 10 < num_binaries && build_config.intent != UserIntent::Install {
573592
if let Err(err) = detect_antivirus::detect_and_report(gctx) {
574593
// Errors in this detection are not fatal.
575594
tracing::error!("failed detecting whether binaries may be slow to run: {err}");

0 commit comments

Comments
 (0)