Skip to content

Commit 27e6d86

Browse files
committed
feat: Add -Zdetect-antivirus=always for disabling heuristics
1 parent d1e5db6 commit 27e6d86

File tree

4 files changed

+48
-12
lines changed

4 files changed

+48
-12
lines changed

src/cargo/core/compiler/build_config.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::core::compiler::CompileKind;
2+
use crate::core::features::DetectAntivirus;
23
use crate::util::context::JobsConfig;
34
use crate::util::interning::InternedString;
45
use crate::util::{CargoResult, GlobalContext, RustfixDiagnosticServer};
@@ -54,7 +55,7 @@ pub struct BuildConfig {
5455
pub compile_time_deps_only: bool,
5556
/// Whether we should try to detect and notify the user when antivirus
5657
/// software might make newly created binaries slow to launch.
57-
pub detect_antivirus: bool,
58+
pub detect_antivirus: DetectAntivirus,
5859
}
5960

6061
fn default_parallelism() -> CargoResult<u32> {
@@ -131,16 +132,16 @@ impl BuildConfig {
131132
};
132133

133134
let detect_antivirus = match (cfg.detect_antivirus, gctx.cli_unstable().detect_antivirus) {
134-
// Enabled by default (for now only when the flag is set).
135-
(None, unstable_flag) => unstable_flag,
136-
// But allow overriding with configuration option.
137-
(Some(cfg_option), true) => cfg_option,
138-
(Some(_), false) => {
135+
// Warn while the config is still unstable.
136+
(Some(_), DetectAntivirus::Never) => {
139137
gctx.shell().warn(
140138
"ignoring 'build.detect-antivirus' config, pass `-Zdetect-antivirus` to enable it",
141139
)?;
142-
false
140+
DetectAntivirus::Never
143141
}
142+
// Allow overriding with config.
143+
(Some(false), _) => DetectAntivirus::Never,
144+
(_, flag) => flag,
144145
};
145146

146147
Ok(BuildConfig {

src/cargo/core/features.rs

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,36 @@ impl FromStr for FixEdition {
400400
}
401401
}
402402

403+
/// The value for `-Zdetect-antivirus`.
404+
#[derive(Debug, Deserialize, Default, PartialEq, Eq, Hash, Copy, Clone)]
405+
#[serde(rename_all = "lowercase")]
406+
pub enum DetectAntivirus {
407+
/// Always detect antivirus.
408+
///
409+
/// This is useful when testing the feature, but isn't expected to be
410+
/// useful to the general user.
411+
Always,
412+
/// Detect antivirus, but only when deemed reasonable.
413+
///
414+
/// This is intended to be the default in the future.
415+
Auto,
416+
/// Never attempt to detect antivirus.
417+
#[default]
418+
Never,
419+
}
420+
421+
impl FromStr for DetectAntivirus {
422+
type Err = anyhow::Error;
423+
fn from_str(s: &str) -> Result<Self, <Self as FromStr>::Err> {
424+
Ok(match s {
425+
"always" => Self::Always,
426+
"auto" => Self::Auto,
427+
"never" => Self::Never,
428+
_ => bail!("invalid `-Zdetect-antivirus`, expected `always`, `auto` or `never`, got `{s}`"),
429+
})
430+
}
431+
}
432+
403433
#[derive(Debug, PartialEq)]
404434
enum Status {
405435
Stable,
@@ -854,7 +884,7 @@ unstable_cli_options!(
854884
checksum_freshness: bool = ("Use a checksum to determine if output is fresh rather than filesystem mtime"),
855885
codegen_backend: bool = ("Enable the `codegen-backend` option in profiles in .cargo/config.toml file"),
856886
config_include: bool = ("Enable the `include` key in config files"),
857-
detect_antivirus: bool = ("Enable the experimental antivirus detection and the config option to disable it"),
887+
detect_antivirus: DetectAntivirus = ("Enable the experimental antivirus detection and the config option to disable it"),
858888
direct_minimal_versions: bool = ("Resolve minimal dependency versions instead of maximum (direct dependencies only)"),
859889
dual_proc_macros: bool = ("Build proc-macros for both the host and the target"),
860890
feature_unification: bool = ("Enable new feature unification modes in workspaces"),
@@ -1374,7 +1404,7 @@ impl CliUnstable {
13741404
"codegen-backend" => self.codegen_backend = parse_empty(k, v)?,
13751405
"config-include" => self.config_include = parse_empty(k, v)?,
13761406
"direct-minimal-versions" => self.direct_minimal_versions = parse_empty(k, v)?,
1377-
"detect-antivirus" => self.detect_antivirus = parse_empty(k, v)?,
1407+
"detect-antivirus" => self.detect_antivirus = v.unwrap_or("auto").parse()?,
13781408
"dual-proc-macros" => self.dual_proc_macros = parse_empty(k, v)?,
13791409
"feature-unification" => self.feature_unification = parse_empty(k, v)?,
13801410
"fix-edition" => {

src/cargo/ops/cargo_compile/mod.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ use crate::core::compiler::{BuildConfig, BuildContext, BuildRunner, Compilation}
4646
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};
49+
use crate::core::features::DetectAntivirus;
4950
use crate::core::profiles::Profiles;
5051
use crate::core::resolver::features::{self, CliFeatures, FeaturesFor};
5152
use crate::core::resolver::{HasDevUnits, Resolve};
@@ -560,7 +561,7 @@ where `<compatible-ver>` is the latest version supporting rustc {rustc_version}"
560561
}
561562
}
562563

563-
if build_config.detect_antivirus {
564+
if build_config.detect_antivirus != DetectAntivirus::Never {
564565
// Count the number of test binaries and build scripts we'll need to
565566
// run. This doesn't take into account the binary that will be run
566567
// if `cargo run` was specified, and doesn't handle pre-2024 `rustdoc`
@@ -588,7 +589,9 @@ where `<compatible-ver>` is the latest version supporting rustc {rustc_version}"
588589
// We also don't want to do this check when installing, since there
589590
// might be `cargo install` users who are not necessarily developers
590591
// (and so the note will be irrelevant to them).
591-
if 10 < num_binaries && build_config.intent != UserIntent::Install {
592+
if (10 < num_binaries && build_config.intent != UserIntent::Install)
593+
|| build_config.detect_antivirus == DetectAntivirus::Always
594+
{
592595
if let Err(err) = detect_antivirus::detect_and_report(gctx) {
593596
// Errors in this detection are not fatal.
594597
tracing::error!("failed detecting whether binaries may be slow to run: {err}");

src/doc/src/reference/unstable.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1952,7 +1952,9 @@ This layout change unblocks work towards caching and locking improvements.
19521952

19531953
* Tracking Issue: [#0](https://github.com/rust-lang/cargo/issues/0)
19541954

1955-
The `-Zdetect-antivirus` flag enables detection of antivirus software that might make launching a binary for the first time slower (which in turn makes Cargo's build scripts and tests slower), and outputs a notice to the user if this is the case.
1955+
The `-Zdetect-antivirus=auto` flag enables detection of antivirus software that might make launching a binary for the first time slower (which in turn makes Cargo's build scripts and tests slower), and outputs a notice to the user if this is the case.
1956+
1957+
This feature uses a small heuristic to avoid doing the detection when deemed unnecessary. `-Zdetect-antivirus=always` may be used to disable this heuristic.
19561958

19571959
This feature will be enabled by default in the future (the flag acts as-if this future is now).
19581960

0 commit comments

Comments
 (0)