Set opt-level for installing tool only on CI

ensure_version_or_cargo_install uses -Copt-level=0 for quicker installation.
However, the flag affects the tool's performance. For example, typos-cli with opt-level=0 takes 15 seconds for checking ./compiler, but the tool with default opt-level only takes less than 1 sec.
This commit enables the option only when the test tidy is run on CI.
This commit is contained in:
Shunpoco 2025-10-04 11:29:07 +01:00
parent a0f398e89d
commit 8a31837054

View file

@ -194,8 +194,8 @@ pub fn ensure_version_or_cargo_install(
// use --force to ensure that if the required version is bumped, we update it.
// use --target-dir to ensure we have a build cache so repeated invocations aren't slow.
// modify PATH so that cargo doesn't print a warning telling the user to modify the path.
let cargo_exit_code = Command::new(cargo)
.args(["install", "--locked", "--force", "--quiet"])
let mut cmd = Command::new(cargo);
cmd.args(["install", "--locked", "--force", "--quiet"])
.arg("--root")
.arg(&tool_root_dir)
.arg("--target-dir")
@ -208,10 +208,16 @@ pub fn ensure_version_or_cargo_install(
.chain(std::iter::once(tool_bin_dir.clone())),
)
.expect("build dir contains invalid char"),
)
.env("RUSTFLAGS", "-Copt-level=0")
.spawn()?
.wait()?;
);
// On CI, we set opt-level flag for quicker installation.
// Since lower opt-level decreases the tool's performance,
// we don't set this option on local.
if CiEnv::is_ci() {
cmd.env("RUSTFLAGS", "-Copt-level=0");
}
let cargo_exit_code = cmd.spawn()?.wait()?;
if !cargo_exit_code.success() {
return Err(io::Error::other("cargo install failed"));
}