Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

unify cargo check and clippy commands #631

Merged
merged 3 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
"rust-analyzer.check.allTargets": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.check.features": "all",
"rust-analyzer.checkOnSave": true,
"rust-analyzer.server.path": "${workspaceFolder}/bin/rust-analyzer",
"search.exclude": {
// Packages and Dependencies
Expand Down
17 changes: 2 additions & 15 deletions crates/infra/cli/src/commands/check/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anyhow::Result;
use clap::{Parser, ValueEnum};
use infra_utils::{commands::Command, github::GitHub};
use infra_utils::cargo::CargoWorkspace;

use crate::{
toolchains::{
Expand Down Expand Up @@ -45,20 +45,7 @@ impl OrderedCommand for CheckCommand {
}

fn check_cargo() -> Result<()> {
let mut command = Command::new("cargo")
.arg("check")
.flag("--offline")
.flag("--all")
.flag("--all-targets")
.flag("--all-features");

if GitHub::is_running_in_ci() {
let rustflags = serde_json::to_string(&["--deny", "warnings"])?;

command = command.property("--config", format!("build.rustflags = {rustflags}"));
}

return command.run();
return CargoWorkspace::get_command("check")?.run();
}

fn check_npm() -> Result<()> {
Expand Down
8 changes: 1 addition & 7 deletions crates/infra/cli/src/commands/lint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,7 @@ impl OrderedCommand for LintCommand {
}

fn run_clippy() -> Result<()> {
let mut clippy = Command::new("cargo").flag("clippy").flag("--");

if GitHub::is_running_in_ci() {
clippy = clippy.property("-D", "warnings");
}

clippy.run()
return CargoWorkspace::get_command("clippy")?.run();
}

fn run_cargo_fmt() -> Result<()> {
Expand Down
1 change: 0 additions & 1 deletion crates/infra/cli/src/commands/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ impl RunController {

command
.property("--bin", &crate_name)
.flag("--offline")
.arg("--")
.args(&self.args)
// Execute in the crate dir, to make use of a local './target' dir if it exists:
Expand Down
14 changes: 9 additions & 5 deletions crates/infra/cli/src/commands/setup/cargo/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ use anyhow::Result;
use infra_utils::{commands::Command, github::GitHub};

pub fn setup_cargo() -> Result<()> {
return if GitHub::is_running_in_ci() {
Command::new("cargo").arg("fetch").flag("--locked").run()
} else {
Command::new("cargo").arg("fetch").run()
};
let mut command = Command::new("cargo").arg("fetch");

if GitHub::is_running_in_ci() {
// In CI, run with '--locked' to make sure `Cargo.lock` is up to date.
// Don't use '--frozen', because the cache is rarely up to date.
command = command.flag("--locked");
}

return command.run();
}
10 changes: 2 additions & 8 deletions crates/infra/cli/src/commands/test/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::utils::{ClapExtensions, OrderedCommand, Terminal};
use anyhow::Result;
use clap::{Parser, ValueEnum};
use infra_utils::{commands::Command, github::GitHub};
use infra_utils::{cargo::CargoWorkspace, commands::Command, github::GitHub};

#[derive(Clone, Debug, Default, Parser)]
pub struct TestController {
Expand Down Expand Up @@ -35,13 +35,7 @@ impl OrderedCommand for TestCommand {
}

fn test_cargo() -> Result<()> {
let mut command = Command::new("cargo")
.arg("test")
.flag("--quiet")
.flag("--offline")
.flag("--all")
.flag("--all-targets")
.flag("--all-features");
let mut command = CargoWorkspace::get_command("test")?.flag("--quiet");

if GitHub::is_running_in_ci() {
command = command.flag("--no-fail-fast");
Expand Down
26 changes: 25 additions & 1 deletion crates/infra/utils/src/cargo/workspace.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@ use anyhow::{ensure, Context, Result};
use regex::Regex;
use semver::Version;

use crate::{cargo::manifest::WorkspaceManifest, commands::Command, paths::PathExtensions};
use crate::{
cargo::manifest::WorkspaceManifest, commands::Command, github::GitHub, paths::PathExtensions,
};

pub struct CargoWorkspace;

Expand Down Expand Up @@ -96,4 +98,26 @@ impl CargoWorkspace {

return cargo_toml.write_string(updated_contents);
}

pub fn get_command(subcommand: impl AsRef<str>) -> Result<Command> {
let mut command = Command::new("cargo")
.arg(subcommand.as_ref())
.flag("--all")
.flag("--all-targets")
.flag("--all-features");

if GitHub::is_running_in_ci() {
// Using `$RUSTFLAGS' or '--' overrides any rustflags from `.cargo/config.toml'.
// Using this syntax instead, as it is concatenated with the existing flags:
command = command.property(
"--config",
format!(
"build.rustflags = {rustflags}",
rustflags = serde_json::to_string(&["--deny", "warnings"])?,
),
);
}

return Ok(command);
}
}
Loading